Skip to content

Instantly share code, notes, and snippets.

@alexcheng1982
Last active December 23, 2015 11:39
Show Gist options
  • Save alexcheng1982/6630364 to your computer and use it in GitHub Desktop.
Save alexcheng1982/6630364 to your computer and use it in GitHub Desktop.
GSON JSON writer streaming
Object result = getResult();
if (result instanceof List) {
StringWriter output = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(output);
List list = (List) result;
jsonWriter.beginArray();
for (int i = 0, n = list.size(); i < n; i++) {
Object obj = list.get(i);
GSON.toJson(obj, obj.getClass(), jsonWriter);
}
jsonWriter.endArray();
jsonWriter.close();
useJson(output.toString());
}
else {
useJson(GSON.toJson(result));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment