Example usage for com.google.gson JsonSerializationContext serialize

List of usage examples for com.google.gson JsonSerializationContext serialize

Introduction

In this page you can find the example usage for com.google.gson JsonSerializationContext serialize.

Prototype

public JsonElement serialize(Object src, Type typeOfSrc);

Source Link

Document

Invokes default serialization on the specified object passing the specific type information.

Usage

From source file:com.claresco.tinman.json.XapiStatementBatchJson.java

License:Open Source License

@Override
public JsonElement serialize(XapiStatementBatch arg0, Type arg1, JsonSerializationContext arg2) {
    JsonArray theStatementArray = new JsonArray();

    if (arg0.size() == 1) {
        JsonElement theStatement = arg2.serialize(arg0.getStatementAtIndex(0), XapiStatement.class);
        theStatementArray.add(theStatement);

        return theStatementArray;
    } else {/*  w w  w. j av  a 2s .  c  o m*/
        for (XapiStatement s : arg0) {
            theStatementArray.add(arg2.serialize(s, XapiStatement.class));
        }
        return theStatementArray;
    }
}

From source file:com.claresco.tinman.json.XapiStatementResultJson.java

License:Open Source License

@Override
public JsonElement serialize(XapiStatementResult arg0, Type arg1, JsonSerializationContext arg2) {
    JsonObject result = new JsonObject();

    if (arg0.hasStatements()) {
        result.add("statements", arg2.serialize(arg0.getStatements(), XapiStatementBatch.class));
    }/*from  w w  w .  j a va2 s  . c om*/

    if (arg0.hasMore()) {
        result.addProperty("more", arg0.getMore().toString());
    }

    return result;
}

From source file:com.contentful.java.cma.FieldTypeAdapter.java

License:Apache License

@Override
public JsonElement serialize(CMAField field, Type type, JsonSerializationContext context) {
    JsonObject json = new JsonObject();

    add(json, ATTR_ID, field.getId());/*w w w. java2s.c  o  m*/
    add(json, ATTR_NAME, field.getName());
    add(json, ATTR_TYPE, field.getType());
    add(json, ATTR_LINK_TYPE, field.getLinkType());
    add(json, ATTR_REQUIRED, field.isRequired());
    add(json, ATTR_DISABLED, field.isDisabled());

    List<Map> validations = field.getValidations();
    if (validations != null) {
        json.add(ATTR_VALIDATIONS, context.serialize(validations, new TypeToken<List<Map>>() {
        }.getType()));
    }

    Map arrayItems = field.getArrayItems();
    if (arrayItems != null) {
        json.add(ATTR_ARRAY_ITEMS, context.serialize(arrayItems, Map.class));
    }

    return json;
}

From source file:com.contentful.java.cma.gson.FieldTypeAdapter.java

License:Apache License

/**
 * Serialize all fields for content types.
 * @param field the content type field to be serialized
 * @param type the type to be used.//from ww  w . ja  va  2  s.c  om
 * @param context the json context to be used.
 * @return a json object representing the field.
 */
@Override
public JsonElement serialize(CMAField field, Type type, JsonSerializationContext context) {
    JsonObject json = new JsonObject();

    add(json, ATTR_ID, field.getId());
    add(json, ATTR_NAME, field.getName());
    add(json, ATTR_TYPE, field.getType());
    add(json, ATTR_LINK_TYPE, field.getLinkType());
    add(json, ATTR_REQUIRED, field.isRequired());
    add(json, ATTR_DISABLED, field.isDisabled());
    add(json, ATTR_OMITTED, field.isOmitted());
    add(json, ATTR_LOCALIZED, field.isLocalized());

    List<Map> validations = field.getValidations();
    if (validations != null) {
        json.add(ATTR_VALIDATIONS, context.serialize(validations, new TypeToken<List<Map>>() {
        }.getType()));
    }

    Map arrayItems = field.getArrayItems();
    if (arrayItems != null) {
        json.add(ATTR_ARRAY_ITEMS, context.serialize(arrayItems, Map.class));
    }

    return json;
}

From source file:com.continuuity.loom.codec.json.current.ConstraintsCodec.java

License:Apache License

@Override
public JsonElement serialize(Constraints constraints, Type type, JsonSerializationContext context) {
    JsonObject jsonObj = new JsonObject();

    jsonObj.add("layout", context.serialize(constraints.getLayoutConstraint(), LayoutConstraint.class));
    jsonObj.add("services", context.serialize(constraints.getServiceConstraints()));

    return jsonObj;
}

From source file:com.continuuity.loom.codec.json.current.TaskConfigCodec.java

License:Apache License

@Override
public JsonElement serialize(TaskConfig taskConfig, Type type, JsonSerializationContext context) {
    JsonObject jsonObj = new JsonObject();

    // this is sent directly to provisioners. It's a mess right now but can't change unless the provisioner changes too.
    jsonObj.add("cluster", taskConfig.getClusterConfig());
    jsonObj.add("service", context.serialize(taskConfig.getTaskServiceAction(), TaskServiceAction.class));
    jsonObj.addProperty("hostname", taskConfig.getNodeProperties().getHostname());
    jsonObj.addProperty("ipaddress", taskConfig.getNodeProperties().getIpaddress());
    jsonObj.addProperty("nodenum", taskConfig.getNodeProperties().getNodenum());
    jsonObj.addProperty("ssh-user", taskConfig.getNodeProperties().getSshUser());
    // these should be changed to be the full object instead of the name
    jsonObj.add("hardwaretype",
            context.serialize(taskConfig.getNodeProperties().getHardwaretype(), String.class));
    jsonObj.add("imagetype", context.serialize(taskConfig.getNodeProperties().getImagetype(), String.class));
    // these should go away once the full hardwaretype/imagetype is included
    jsonObj.addProperty("flavor", taskConfig.getNodeProperties().getFlavor());
    jsonObj.addProperty("image", taskConfig.getNodeProperties().getImage());
    jsonObj.add("automators", context.serialize(taskConfig.getNodeProperties().getAutomators()));
    jsonObj.add("services", context.serialize(taskConfig.getNodeProperties().getServices()));
    jsonObj.add("provider", context.serialize(taskConfig.getProvider()));
    jsonObj.add("nodes", context.serialize(taskConfig.getNodes()));
    jsonObj.add("service", context.serialize(taskConfig.getTaskServiceAction()));

    // gross.../*from   w  w  w . j  av  a 2s .  co m*/
    for (Map.Entry<String, JsonElement> entry : taskConfig.getProvisionerResults().entrySet()) {
        jsonObj.add(entry.getKey(), entry.getValue());
    }

    return jsonObj;
}

From source file:com.continuuity.weave.internal.json.ResourceReportCodec.java

License:Apache License

@Override
public JsonElement serialize(ResourceReport src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject json = new JsonObject();

    json.addProperty("appMasterId", src.getApplicationId());
    json.add("appMasterResources",
            context.serialize(src.getAppMasterResources(), new TypeToken<WeaveRunResources>() {
            }.getType()));/*from w  w  w . j  av  a 2  s . c om*/
    json.add("runnableResources",
            context.serialize(src.getResources(), new TypeToken<Map<String, Collection<WeaveRunResources>>>() {
            }.getType()));

    return json;
}

From source file:com.continuuity.weave.internal.json.RuntimeSpecificationCodec.java

License:Open Source License

@Override
public JsonElement serialize(RuntimeSpecification src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject json = new JsonObject();
    json.addProperty("name", src.getName());
    json.add("runnable", context.serialize(src.getRunnableSpecification(), WeaveRunnableSpecification.class));
    json.add("resources", context.serialize(src.getResourceSpecification(), ResourceSpecification.class));
    json.add("files", context.serialize(src.getLocalFiles(), new TypeToken<Collection<LocalFile>>() {
    }.getType()));/*from  w  ww.  j  av  a  2 s  .  c om*/

    return json;
}

From source file:com.continuuity.weave.internal.json.StateNodeCodec.java

License:Open Source License

@Override
public JsonElement serialize(StateNode src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject jsonObj = new JsonObject();
    jsonObj.addProperty("state", src.getState().name());
    if (src.getStackTraces() != null) {
        jsonObj.add("stackTraces", context.serialize(src.getStackTraces(), StackTraceElement[].class));
    }//from ww w  .  ja v  a  2  s.c  o  m
    return jsonObj;
}

From source file:com.continuuity.weave.internal.json.WeaveRunnableSpecificationCodec.java

License:Open Source License

@Override
public JsonElement serialize(WeaveRunnableSpecification src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject json = new JsonObject();

    json.addProperty("classname", src.getClassName());
    json.addProperty("name", src.getName());
    json.add("arguments", context.serialize(src.getConfigs(), new TypeToken<Map<String, String>>() {
    }.getType()));//from w  ww  .jav  a  2s  .c o  m

    return json;
}