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