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);

Source Link

Document

Invokes default serialization on the specified object.

Usage

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

License:Apache License

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

    jsonObj.add("requires", context.serialize(serviceStageDependencies.getRequires()));
    jsonObj.add("uses", context.serialize(serviceStageDependencies.getUses()));

    return jsonObj;
}

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

License:Open Source License

@Override
public JsonElement serialize(Arguments src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject json = new JsonObject();
    json.add("arguments", context.serialize(src.getArguments()));
    json.add("runnableArguments", context.serialize(src.getRunnableArguments().asMap()));

    return json;/*from ww  w.  j  a  v a2  s .  com*/
}

From source file:com.dmdirc.addons.ui_web2.serialisers.WindowModelSerialiser.java

License:Open Source License

@Override
public JsonElement serialize(final WindowModel src, final Type typeOfSrc,
        final JsonSerializationContext context) {
    final JsonObject res = new JsonObject();
    res.addProperty("id", src.getId());
    res.addProperty("name", src.getName());
    res.addProperty("icon", src.getIcon());
    res.addProperty("title", src.getTitle());
    res.addProperty("writable", src.getInputModel().isPresent());
    res.add("children", context.serialize(windowManager.getChildren(src)));
    res.add("components", context.serialize(src.getComponents()));
    res.add("backbuffer", context.serialize(src.getBackBuffer()));
    return res;/*from   ww w .j  a va2s  .c om*/
}

From source file:com.exsoloscript.challonge.gson.ParticipantQueryListAdapter.java

License:Apache License

@Override
public JsonElement serialize(List<ParticipantQuery> participantQueryList, Type type,
        JsonSerializationContext jsonSerializationContext) {
    JsonObject base = new JsonObject();
    JsonArray participantArray = new JsonArray();

    for (ParticipantQuery query : participantQueryList) {
        participantArray.add(jsonSerializationContext.serialize(query));
    }/*from w  ww .  j  a va  2s.  com*/

    base.add("participants", participantArray);
    return base;
}

From source file:com.github.autermann.matlab.json.MatlabExceptionSerializer.java

License:Open Source License

@Override
public JsonElement serialize(MatlabException e, Type type, JsonSerializationContext ctx) {
    JsonObject object = new JsonObject();
    object.add(MatlabJSONConstants.EXCEPTION, ctx.serialize(e.getMessage()));
    return object;
}

From source file:com.github.autermann.matlab.json.MatlabRequestSerializer.java

License:Open Source License

private JsonArray serializeParameters(MatlabRequest req, JsonSerializationContext ctx) {
    JsonArray parameters = new JsonArray();
    for (MatlabValue parameter : req.getParameters()) {
        parameters.add(ctx.serialize(parameter));
    }/* w w w  .j  av  a2  s  . c o  m*/
    return parameters;

}

From source file:com.github.autermann.matlab.json.MatlabResultSerializer.java

License:Open Source License

@Override
public JsonElement serialize(MatlabResult src, Type typeOfSrc, JsonSerializationContext context) {

    JsonObject o = new JsonObject();
    JsonObject results = new JsonObject();
    for (Entry<String, MatlabValue> result : src.getResults().entrySet()) {
        results.add(result.getKey(), context.serialize(result.getValue()));
    }//from www .  jav a 2 s . c  o  m
    o.add(MatlabJSONConstants.RESULTS, results);

    return o;
}

From source file:com.google.api.ads.adwords.keywordoptimizer.api.JsonUtil.java

License:Open Source License

/**
 * Initializes Gson to convert objects to and from JSON. This method customizes a "plain" Gson by
 * adding appropriate exclusions strategies / adapters as needed in this project for a "pretty"
 * output. /*from  w  w  w  . j a  v a  2  s  .  c o  m*/
 */
private static Gson initGson(boolean prettyPrint) {
    GsonBuilder builder = new GsonBuilder();

    // Exclude superclasses.
    ExclusionStrategy superclassExclusionStrategy = new SuperclassExclusionStrategy();
    builder.addDeserializationExclusionStrategy(superclassExclusionStrategy);
    builder.addSerializationExclusionStrategy(superclassExclusionStrategy);

    // Exclude underscore fields in client lib objects.
    ExclusionStrategy underscoreExclusionStrategy = new ExclusionStrategy() {
        @Override
        public boolean shouldSkipField(FieldAttributes field) {
            if (field.getName().startsWith("_")) {
                return true;
            }

            return false;
        }

        @Override
        public boolean shouldSkipClass(Class<?> clazz) {
            return false;
        }
    };
    builder.addDeserializationExclusionStrategy(underscoreExclusionStrategy);
    builder.addSerializationExclusionStrategy(underscoreExclusionStrategy);

    // Render KeywordCollection as an array of KeywordInfos.
    builder.registerTypeAdapter(KeywordCollection.class, new JsonSerializer<KeywordCollection>() {
        @Override
        public JsonElement serialize(KeywordCollection src, java.lang.reflect.Type typeOfSrc,
                JsonSerializationContext context) {
            JsonArray out = new JsonArray();
            for (KeywordInfo info : src.getListSortedByScore()) {
                out.add(context.serialize(info));
            }
            return out;
        }
    });
    // Render Money as a primitive.
    builder.registerTypeAdapter(Money.class, new JsonSerializer<Money>() {
        @Override
        public JsonElement serialize(Money src, java.lang.reflect.Type typeOfSrc,
                JsonSerializationContext context) {
            JsonElement out = new JsonPrimitive(src.getMicroAmount() / 1000000);
            return out;
        }
    });
    // Render Keyword in a simple way.
    builder.registerTypeAdapter(Keyword.class, new JsonSerializer<Keyword>() {
        @Override
        public JsonElement serialize(Keyword src, java.lang.reflect.Type typeOfSrc,
                JsonSerializationContext context) {
            JsonObject out = new JsonObject();
            out.addProperty("text", src.getText());
            out.addProperty("matchtype", src.getMatchType().toString());
            return out;
        }
    });
    // Render Throwable in a simple way (for all subclasses).
    builder.registerTypeHierarchyAdapter(Throwable.class, new JsonSerializer<Throwable>() {
        @Override
        public JsonElement serialize(Throwable src, java.lang.reflect.Type typeOfSrc,
                JsonSerializationContext context) {
            JsonObject out = new JsonObject();
            out.addProperty("message", src.getMessage());
            out.addProperty("type", src.getClass().getName());

            JsonArray stack = new JsonArray();
            for (StackTraceElement stackTraceElement : src.getStackTrace()) {
                JsonObject stackElem = new JsonObject();
                stackElem.addProperty("file", stackTraceElement.getFileName());
                stackElem.addProperty("line", stackTraceElement.getLineNumber());
                stackElem.addProperty("method", stackTraceElement.getMethodName());
                stackElem.addProperty("class", stackTraceElement.getClassName());
                stack.add(stackElem);
            }
            out.add("stack", stack);

            if (src.getCause() != null) {
                out.add("cause", context.serialize(src.getCause()));
            }
            return out;
        }
    });

    if (prettyPrint) {
        builder.setPrettyPrinting();
    }

    return builder.create();
}

From source file:com.google.gerrit.server.events.SupplierSerializer.java

License:Apache License

@Override
public JsonElement serialize(Supplier<?> src, Type typeOfSrc, JsonSerializationContext context) {
    return context.serialize(src.get());
}

From source file:com.google.gwtjsonrpc.server.JsonServlet.java

License:Apache License

private String formatResult(final ActiveCall call) throws UnsupportedEncodingException, IOException {
    final GsonBuilder gb = createGsonBuilder();
    gb.registerTypeAdapter(call.getClass(), new JsonSerializer<ActiveCall>() {
        public JsonElement serialize(final ActiveCall src, final Type typeOfSrc,
                final JsonSerializationContext context) {
            if (call.callback != null) {
                if (src.externalFailure != null) {
                    return new JsonNull();
                }//ww w  . java  2  s  .  c om
                return context.serialize(src.result);
            }

            final JsonObject r = new JsonObject();
            r.add(src.versionName, src.versionValue);
            if (src.id != null) {
                r.add("id", src.id);
            }
            if (src.xsrfKeyOut != null) {
                r.addProperty("xsrfKey", src.xsrfKeyOut);
            }
            if (src.externalFailure != null) {
                final JsonObject error = new JsonObject();
                if ("jsonrpc".equals(src.versionName)) {
                    final int code = to2_0ErrorCode(src);

                    error.addProperty("code", code);
                    error.addProperty("message", src.externalFailure.getMessage());
                } else {
                    error.addProperty("name", "JSONRPCError");
                    error.addProperty("code", 999);
                    error.addProperty("message", src.externalFailure.getMessage());
                }
                r.add("error", error);
            } else {
                r.add("result", context.serialize(src.result));
            }
            return r;
        }
    });

    final StringWriter o = new StringWriter();
    if (call.callback != null) {
        o.write(call.callback);
        o.write("(");
    }
    gb.create().toJson(call, o);
    if (call.callback != null) {
        o.write(");");
    }
    o.close();
    return o.toString();
}