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:org.apache.twill.internal.json.RuntimeSpecificationCodec.java

License:Apache 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(), TwillRunnableSpecification.class));
    json.add("resources", context.serialize(src.getResourceSpecification(), ResourceSpecification.class));
    json.add("files", context.serialize(src.getLocalFiles(), new TypeToken<Collection<LocalFile>>() {
    }.getType()));/*from   ww  w. j av a  2s .  c om*/

    return json;
}

From source file:org.apache.twill.internal.json.StateNodeCodec.java

License:Apache License

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

From source file:org.apache.twill.internal.json.TwillRunnableSpecificationCodec.java

License:Apache License

@Override
public JsonElement serialize(TwillRunnableSpecification 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   www .  j a  va  2s.co m

    return json;
}

From source file:org.apache.twill.internal.json.TwillRuntimeSpecificationCodec.java

License:Apache License

@Override
public JsonElement serialize(TwillRuntimeSpecification src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject json = new JsonObject();
    json.addProperty(FS_USER, src.getFsUser());
    json.addProperty(TWILL_APP_DIR, src.getTwillAppDir().toASCIIString());
    json.addProperty(ZK_CONNECT_STR, src.getZkConnectStr());
    json.addProperty(TWILL_RUNID, src.getTwillAppRunId().getId());
    json.addProperty(TWILL_APP_NAME, src.getTwillAppName());
    json.addProperty(RESERVED_MEMORY, src.getReservedMemory());
    json.addProperty(HEAP_RESERVED_MIN_RATIO, src.getMinHeapRatio());
    if (src.getRmSchedulerAddr() != null) {
        json.addProperty(RM_SCHEDULER_ADDR, src.getRmSchedulerAddr());
    }//www. j a v  a  2 s.  c o  m
    json.add(TWILL_SPEC, context.serialize(src.getTwillSpecification(), new TypeToken<TwillSpecification>() {
    }.getType()));
    json.add(LOG_LEVELS,
            context.serialize(src.getLogLevels(), new TypeToken<Map<String, Map<String, String>>>() {
            }.getType()));
    json.add(MAX_RETRIES, context.serialize(src.getMaxRetries(), new TypeToken<Map<String, Integer>>() {
    }.getType()));
    json.addProperty(LOG_COLLECTION_ENABLED, src.isLogCollectionEnabled());

    return json;
}

From source file:org.apache.twill.internal.json.TwillSpecificationCodec.java

License:Apache License

@Override
public JsonElement serialize(TwillSpecification src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject json = new JsonObject();
    json.addProperty("name", src.getName());
    json.add("runnables",
            context.serialize(src.getRunnables(), new TypeToken<Map<String, RuntimeSpecification>>() {
            }.getType()));//from  w  w w .j a v a  2s  . c  om
    json.add("orders", context.serialize(src.getOrders(), new TypeToken<List<TwillSpecification.Order>>() {
    }.getType()));
    json.add("placementPolicies", context.serialize(src.getPlacementPolicies(),
            new TypeToken<List<TwillSpecification.PlacementPolicy>>() {
            }.getType()));
    EventHandlerSpecification eventHandler = src.getEventHandler();
    if (eventHandler != null) {
        json.add("handler", context.serialize(eventHandler, EventHandlerSpecification.class));
    }

    return json;
}

From source file:org.catrobat.catroid.io.BackpackBrickSerializerAndDeserializer.java

License:Open Source License

@Override
public JsonElement serialize(Brick brick, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject jsonObject = new JsonObject();
    String packageName = brick.getClass().getPackage().getName();
    String className = brick.getClass().getSimpleName();
    jsonObject.add(TYPE, new JsonPrimitive(packageName + '.' + className));
    jsonObject.add(PROPERTY, context.serialize(brick, brick.getClass()));
    return jsonObject;
}

From source file:org.catrobat.catroid.io.BackpackScriptSerializerAndDeserializer.java

License:Open Source License

@Override
public JsonElement serialize(Script script, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject jsonObject = new JsonObject();
    jsonObject.add(TYPE, new JsonPrimitive(script.getClass().getSimpleName()));
    jsonObject.add(PROPERTY, context.serialize(script, script.getClass()));
    return jsonObject;
}

From source file:org.cmg.jresp.json.GroupPredicateSerializer.java

License:Open Source License

@Override
public JsonElement serialize(GroupPredicate src, Type typeOfSrc, JsonSerializationContext context) {
    GroupPredicate.PredicateType gType = src.getType();
    JsonObject json = new JsonObject();
    json.add("type", context.serialize(gType, GroupPredicate.PredicateType.class));
    switch (gType) {
    case TRUE://from w  w w . j a v  a2s. c o  m
        return json;
    case ISEQUAL:
        return doSerializeHasValuePredicate((HasValue) src, json, context);
    case ISGTR:
        return doSerializeIsGreaterThanPredicate((IsGreaterThan) src, json, context);
    case ISGEQ:
        return doSerializeIsGreaterOrEqualThanPredicate((IsGreaterOrEqualThan) src, json, context);
    case ISLEQ:
        return doSerializeIsLessOrEqualThanPredicate((IsLessOrEqualThan) src, json, context);
    case ISLES:
        return doSerializeIsLessThanPredicate((IsLessThan) src, json, context);
    case AND:
        return doSerializeAndPredicate((And) src, json, context);
    case OR:
        return doSerializeOrPredicate((Or) src, json, context);
    case NOT:
        return doSerializeNotPredicate((Not) src, json, context);
    }
    return json;
}

From source file:org.cmg.jresp.json.GroupPredicateSerializer.java

License:Open Source License

private JsonElement doSerializeNotPredicate(Not src, JsonObject json, JsonSerializationContext context) {
    json.add("arg", context.serialize(src.getArgument(), GroupPredicate.class));
    return json;//from   w w w . ja v a  2  s . c om
}

From source file:org.cmg.jresp.json.GroupPredicateSerializer.java

License:Open Source License

private JsonElement doSerializeAndPredicate(And src, JsonObject json, JsonSerializationContext context) {
    json.add("left", context.serialize(src.getLeft(), GroupPredicate.class));
    json.add("right", context.serialize(src.getRight(), GroupPredicate.class));
    return json;// www . j  a  v a  2  s .c  o m
}