Search in sources :

Example 1 with JsonGeneratorWriter

use of org.immutables.gson.stream.JsonGeneratorWriter in project immutables by immutables.

the class Marshaling method toJson.

@SuppressWarnings({ "resource", "unchecked" })
public static String toJson(Object object) {
    TypeAdapter<Object> adapter = GSON.getAdapter((TypeToken<Object>) TypeToken.get(object.getClass()));
    try {
        StringWriter stringWriter = new StringWriter();
        JsonGeneratorWriter writer = new JsonGeneratorWriter(JSON_FACTORY.createGenerator(stringWriter));
        if (adapter instanceof com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.Adapter) {
            throw new IllegalStateException("Immutable adapters not registered");
        }
        GSON.toJson(object, object.getClass(), writer);
        writer.close();
        return stringWriter.toString();
    } catch (IOException ex) {
        throw Throwables.propagate(ex);
    }
}
Also used : JsonGeneratorWriter(org.immutables.gson.stream.JsonGeneratorWriter) StringWriter(java.io.StringWriter) TypeAdapter(com.google.gson.TypeAdapter) IOException(java.io.IOException)

Aggregations

TypeAdapter (com.google.gson.TypeAdapter)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 JsonGeneratorWriter (org.immutables.gson.stream.JsonGeneratorWriter)1