Last active
December 23, 2015 11:39
-
-
Save alexcheng1982/6630364 to your computer and use it in GitHub Desktop.
GSON JSON writer streaming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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