Skip to content

Instantly share code, notes, and snippets.

@toKrause
Last active March 29, 2016 09:27
Show Gist options
  • Save toKrause/95944d3069b5422ee45f to your computer and use it in GitHub Desktop.
Save toKrause/95944d3069b5422ee45f to your computer and use it in GitHub Desktop.
Helper methods to create document model consisting of JSONArrays and JSONObjects using a JsonPullParser
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import net.markenwerk.utils.json.parser.JsonPullParser;
import net.markenwerk.utils.json.parser.JsonSourcePullParser;
import net.markenwerk.utils.json.parser.JsonState;
import net.markenwerk.utils.json.parser.JsonSyntaxException;
public final class JsonUtil {
private JsonUtil() {
}
public static JSONObject readObject(Reader reader) throws JsonSyntaxException, IOException,
IllegalArgumentException, JSONException {
if (null == reader) {
throw new IllegalArgumentException("reader is null");
} else {
final JsonPullParser jsonParser = new JsonSourcePullParser(reader);
try {
jsonParser.beginDocumnet();
JSONObject jsonObject = readObject(jsonParser);
jsonParser.endDocumnet();
return jsonObject;
} finally {
jsonParser.close();
}
}
}
private static JSONObject readObject(JsonPullParser jsonParser) throws JsonSyntaxException, IOException,
IllegalArgumentException, JSONException {
final JSONObject jsonObject = new JSONObject();
jsonParser.beginObject();
while (jsonParser.hasNext()) {
String name = jsonParser.nextName();
JsonState token = jsonParser.currentState();
switch (token) {
case ARRAY_BEGIN:
jsonObject.put(name, readArray(jsonParser));
break;
case OBJECT_BEGIN:
jsonObject.put(name, readObject(jsonParser));
break;
case BOOLEAN:
jsonObject.put(name, jsonParser.nextBoolean());
break;
case NULL:
jsonParser.nextNull();
jsonObject.put(name, JSONObject.NULL);
break;
case LONG:
jsonObject.put(name, jsonParser.nextLong());
break;
case DOUBLE:
jsonObject.put(name, jsonParser.nextDouble());
break;
case STRING:
jsonObject.put(name, jsonParser.nextString());
break;
default:
throw new RuntimeException("unexpected token " + token);
}
}
jsonParser.endObject();
return jsonObject;
}
public static JSONArray readArray(Reader reader) throws JsonSyntaxException, IOException,
IllegalArgumentException, IllegalStateException, JSONException {
if (null == reader) {
throw new IllegalArgumentException("reader is null");
} else {
JsonPullParser jsonParser = new JsonSourcePullParser(reader);
try {
jsonParser.beginDocumnet();
JSONArray jsonArray = readArray(jsonParser);
jsonParser.endDocumnet();
return jsonArray;
} finally {
jsonParser.close();
}
}
}
private static JSONArray readArray(JsonPullParser jsonParser) throws JsonSyntaxException, IOException,
IllegalArgumentException, IllegalStateException, JSONException {
final JSONArray jsonArray = new JSONArray();
jsonParser.beginArray();
while (jsonParser.hasNext()) {
JsonState token = jsonParser.currentState();
switch (token) {
case ARRAY_BEGIN:
jsonArray.put(readArray(jsonParser));
break;
case OBJECT_BEGIN:
jsonArray.put(readObject(jsonParser));
break;
case BOOLEAN:
jsonArray.put(jsonParser.nextBoolean());
break;
case NULL:
jsonParser.nextNull();
jsonArray.put((Object) null);
break;
case LONG:
jsonArray.put(jsonParser.nextLong());
break;
case DOUBLE:
jsonArray.put(jsonParser.nextDouble());
break;
case STRING:
jsonArray.put(jsonParser.nextString());
break;
default:
throw new RuntimeException("unexpected token " + token);
}
}
jsonParser.endArray();
return jsonArray;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment