Skip to content

Instantly share code, notes, and snippets.

@toKrause
Last active March 20, 2016 13:01
Show Gist options
  • Save toKrause/ef6b3f3f060640f8620c to your computer and use it in GitHub Desktop.
Save toKrause/ef6b3f3f060640f8620c to your computer and use it in GitHub Desktop.
JsonHandlers to create document model consisting of JSONArrays and JSONObjects using a JsonPushParser
import java.util.ArrayDeque;
import java.util.Deque;
import org.json.JSONArray;
import org.json.JSONObject;
import net.markenwerk.utils.json.parser.JsonHandler;
/**
* JsonHandler that creates a document model consisting of JSONArrays and
* JSONObjects
*/
public abstract class AbstractHandler<Result> implements JsonHandler<Result> {
/**
* Simplified handler interface, that is used to fill JSONArrays and
* JSONObjects
*/
private static interface Handler<Result> {
public void onName(String name);
public void onNull();
public void onValue(Object value);
public Result getResult();
}
// stack that contains handlers for the commenced JSONArrays and JSONObjects
private Deque<Handler<?>> stack = new ArrayDeque<Handler<?>>();
// handler for the current JSONArray or JSONobject
private Handler<?> handler;
@Override
public void onDocumentBegin() {
// create a root handler, that holds the root JSONArray or JSONObject
handler = new Handler<Object>() {
private Object result;
@Override
public void onName(String name) {
}
@Override
public void onNull() {
}
@Override
public void onValue(Object value) {
result = value;
}
@Override
public Object getResult() {
return result;
}
};
}
@Override
public void onDocumentEnd() {
}
@Override
public void onArrayBegin() {
// add a new JSONArray to the last JSONArray of JSONObject
final JSONArray array = new JSONArray();
handler.onValue(array);
// create a new handler that adds the following values to array
stack.push(handler);
handler = new Handler<JSONArray>() {
@Override
public void onName(String name) {
}
@Override
public void onNull() {
array.put((Object) null);
}
@Override
public void onValue(Object value) {
array.put(value);
}
@Override
public JSONArray getResult() {
return array;
}
};
}
@Override
public void onArrayEnd() {
// add the following values to the previous JSONArray or JSONObject
handler = stack.pop();
}
@Override
public void onObjectBegin() {
// add a new JSONObject to the last JSONArray of JSONObject
final JSONObject object = new JSONObject();
handler.onValue(object);
// create a new handler that adds the following values to object
stack.push(handler);
handler = new Handler<JSONObject>() {
private String name;
@Override
public void onName(String name) {
this.name = name;
}
@Override
public void onNull() {
object.put(name, JSONObject.NULL);
}
@Override
public void onValue(Object value) {
object.put(name, value);
}
@Override
public JSONObject getResult() {
return object;
}
};
}
@Override
public void onName(String name) {
handler.onName(name);
}
@Override
public void onEndObject() {
// add the following values to the previous JSONArray or JSONObject
handler = stack.pop();
}
@Override
public void onNull() {
handler.onNull();
}
@Override
public void onBoolean(boolean value) {
handler.onValue(value);
}
@Override
public void onLong(long value) {
handler.onValue(value);
}
@Override
public void onDouble(double value) {
handler.onValue(value);
}
@Override
public void onString(String value) {
handler.onValue(value);
}
/**
* Returns the root object and ensures that it is a JSONObject
*/
protected JSONObject getRootObject() {
try {
return (JSONObject) handler.getResult();
} catch (ClassCastException e) {
throw new IllegalStateException("JSON document contains a JSON array, not a JSON object");
}
}
/**
* Returns the root object and ensures that it is a JSONArray
*/
protected JSONArray getRootArray() {
try {
return (JSONArray) handler.getResult();
} catch (ClassCastException e) {
throw new IllegalStateException("JSON document contains a JSON object, not a JSON array");
} }
}
import org.json.JSONArray;
public class ArrayHandler extends AbstractHandler<JSONArray> {
@Override
public JSONArray getResult() {
return getRootArray();
}
}
import org.json.JSONObject;
public class ObjectHandler extends AbstractHandler<JSONObject> {
@Override
public JSONObject getResult() {
return getRootObject();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment