Example usage for com.google.gson GsonBuilder create

List of usage examples for com.google.gson GsonBuilder create

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder create.

Prototype

public Gson create() 

Source Link

Document

Creates a Gson instance based on the current configuration.

Usage

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

License:Apache License

private void parsePostRequest(final CallType call) throws UnsupportedEncodingException, IOException {
    try {/*  w w  w. j  av a 2 s.c o m*/
        final GsonBuilder gb = createGsonBuilder();
        gb.registerTypeAdapter(ActiveCall.class, new CallDeserializer<CallType>(call, this));
        gb.create().fromJson(readBody(call), ActiveCall.class);
    } catch (JsonParseException err) {
        call.method = null;
        call.params = null;
        throw err;
    }
}

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();
                }/* w w w  . j  a v a 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();
}

From source file:com.google.mr4c.serialize.json.JsonAlgorithmBeanSerializer.java

License:Open Source License

public void serializeAlgorithmSchemaBean(AlgorithmSchemaBean algoSchema, Writer writer) throws IOException {
    GsonBuilder builder = new GsonBuilder();
    builder.setPrettyPrinting();/* w  ww . j av  a 2 s. c  o m*/
    Gson gson = builder.create();
    gson.toJson(algoSchema, writer);
}

From source file:com.google.mr4c.serialize.json.JsonAlgorithmBeanSerializer.java

License:Open Source License

public AlgorithmSchemaBean deserializeAlgorithmSchemaBean(Reader reader) throws IOException {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    return gson.fromJson(reader, AlgorithmSchemaBean.class);
}

From source file:com.google.mr4c.serialize.json.JsonConfigSerializer.java

License:Open Source License

private Gson buildGson() {
    GsonBuilder builder = new GsonBuilder();
    builder.setPrettyPrinting();/*from  w ww  . j a va 2s. c o  m*/
    builder.registerTypeAdapter(Document.class, new DocumentSerializer());
    return builder.create();
}

From source file:com.google.mr4c.serialize.json.JsonDatasetBeanSerializer.java

License:Open Source License

public void serializeDatasetBean(DatasetBean dataset, Writer writer) throws IOException {
    GsonBuilder builder = new GsonBuilder();
    builder.setPrettyPrinting();/*from   w  ww. ja v  a 2s .  c  om*/
    Gson gson = builder.create();
    gson.toJson(dataset, writer);
}

From source file:com.google.mr4c.serialize.json.JsonDatasetBeanSerializer.java

License:Open Source License

public DatasetBean deserializeDatasetBean(Reader reader) throws IOException {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(MetadataEntryBean.class, new MetadataEntryBeanDeserializer());
    Gson gson = builder.create();
    return gson.fromJson(reader, DatasetBean.class);
}

From source file:com.google.mr4c.serialize.json.JsonDatasetBeanSerializer.java

License:Open Source License

public String serializeDataFileBean(DataFileBean file) {
    GsonBuilder builder = new GsonBuilder();
    builder.setPrettyPrinting();//from   ww w.ja  va  2s  . c o  m
    Gson gson = builder.create();
    return gson.toJson(file);
}

From source file:com.google.mr4c.serialize.json.JsonDatasetBeanSerializer.java

License:Open Source License

public DataFileBean deserializeDataFileBean(String serializedFile) {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    return gson.fromJson(serializedFile, DataFileBean.class);
}

From source file:com.google.mr4c.serialize.json.JsonDatasetBeanSerializer.java

License:Open Source License

public String serializeDataKeyBean(DataKeyBean key) {
    GsonBuilder builder = new GsonBuilder();
    builder.setPrettyPrinting();/*ww  w.  ja  v  a2 s.  c  o  m*/
    Gson gson = builder.create();
    return gson.toJson(key);
}