Created
November 12, 2012 22:41
-
-
Save jhorstmann/4062550 to your computer and use it in GitHub Desktop.
Getting familiar with the JsonBuilder API of JSR 353
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
package net.jhorstmann.jsonp; | |
import java.io.IOException; | |
import java.io.PushbackReader; | |
import java.io.StringReader; | |
import javax.json.JsonArray; | |
import javax.json.JsonArrayBuilder; | |
import javax.json.JsonBuilder; | |
import javax.json.JsonBuilder.JsonBuildable; | |
import javax.json.JsonObject; | |
import javax.json.JsonObjectBuilder; | |
import javax.json.JsonStructure; | |
/** | |
* Trying to get familiar with the proposed API of JSR353, | |
* this code uses JsonBuilder inside a recursive descent parser | |
* for a simple JSON-like format that consists of arrays, | |
* objects with single-character keys and single digit numbers. | |
*/ | |
public class RecurseiveParsingWithJsonBuilder { | |
/** | |
* Parses an object with an object as parent. | |
*/ | |
public static void readObject(PushbackReader reader, JsonObjectBuilder parentBuilder, String key) throws IOException { | |
JsonObjectBuilder objectBuilder = parentBuilder.beginObject(key); | |
readObject0(reader, objectBuilder); | |
objectBuilder.endObject(); | |
} | |
/** | |
* Parses an object with an array as parent. | |
*/ | |
public static void readObject(PushbackReader reader, JsonArrayBuilder parentBuilder) throws IOException { | |
JsonObjectBuilder objectBuilder = parentBuilder.beginObject(); | |
readObject0(reader, objectBuilder); | |
objectBuilder.endObject(); | |
} | |
private static void readObject0(PushbackReader reader, JsonObjectBuilder objectBuilder) throws IOException { | |
int ch = reader.read(); | |
if (ch == -1) { | |
throw new IOException("unexpected eof"); | |
} else if (ch != '}') { | |
reader.unread(ch); | |
while (true) { | |
ch = reader.read(); | |
if (ch >= 'a' || ch <= '9') { | |
String key = "" + (char) ch; | |
ch = reader.read(); | |
if (ch == -1) { | |
throw new IOException("unexpected eof"); | |
} else if (ch != ':') { | |
throw new IOException("unexpected char " + (char) ch); | |
} else { | |
ch = reader.read(); | |
if (ch == -1) { | |
throw new IOException("unexpected eof"); | |
} else if (ch == '[') { | |
readArray(reader, objectBuilder, key); | |
} else if (ch == '{') { | |
readObject(reader, objectBuilder, key); | |
} else if (ch >= '0' && ch <= '9') { | |
objectBuilder.add(key, ch - '0'); | |
} else { | |
throw new IOException("unexpected char " + (char) ch); | |
} | |
} | |
} else if (ch == -1) { | |
throw new IOException("unexpected eof"); | |
} | |
ch = reader.read(); | |
if (ch == '}') { | |
break; | |
} else if (ch == -1) { | |
throw new IOException("unexpected eof"); | |
} else if (ch != ',') { | |
throw new IOException("unexpected char " + (char) ch); | |
} | |
} | |
} | |
} | |
/** | |
* Parses an array with an object as parent. | |
*/ | |
public static void readArray(PushbackReader reader, JsonObjectBuilder parentBuilder, String key) throws IOException { | |
JsonArrayBuilder arrayBuilder = parentBuilder.beginArray(key); | |
readArray0(reader, arrayBuilder); | |
arrayBuilder.endArray(); | |
} | |
/** | |
* Parses an array with an array as parent. | |
*/ | |
public static void readArray(PushbackReader reader, JsonArrayBuilder parentBuilder) throws IOException { | |
JsonArrayBuilder arrayBuilder = parentBuilder.beginArray(); | |
readArray0(reader, arrayBuilder); | |
arrayBuilder.endArray(); | |
} | |
private static void readArray0(PushbackReader reader, JsonArrayBuilder arrayBuilder) throws IOException { | |
int ch = reader.read(); | |
if (ch == -1) { | |
throw new IOException("unexpected eof"); | |
} else if (ch != ']') { | |
reader.unread(ch); | |
while (true) { | |
ch = reader.read(); | |
if (ch == '[') { | |
readArray(reader, arrayBuilder); | |
} else if (ch == '{') { | |
readObject(reader, arrayBuilder); | |
} else if (ch >= '0' && ch <= '9') { | |
arrayBuilder.add(ch - '0'); | |
} else if (ch == -1) { | |
throw new IOException("unexpected eof"); | |
} else { | |
throw new IOException("unexpected char " + (char) ch); | |
} | |
ch = reader.read(); | |
if (ch == ']') { | |
break; | |
} else if (ch == -1) { | |
throw new IOException("unexpected eof"); | |
} else if (ch != ',') { | |
throw new IOException("unexpected char " + (char) ch); | |
} | |
} | |
} | |
} | |
/* | |
* Parses the root element, either an object or an array. | |
*/ | |
public static JsonStructure readRoot(PushbackReader reader) throws IOException { | |
int ch = reader.read(); | |
if (ch == '[') { | |
JsonBuilder builder = new JsonBuilder(); | |
JsonArrayBuilder<JsonBuildable<JsonArray>> arrayBuilder = builder.beginArray(); | |
readArray0(reader, arrayBuilder); | |
return arrayBuilder.endArray().build(); | |
} else if (ch == '{') { | |
JsonBuilder builder = new JsonBuilder(); | |
JsonObjectBuilder<JsonBuildable<JsonObject>> objectBuilder = builder.beginObject(); | |
readObject0(reader, objectBuilder); | |
return objectBuilder.endObject().build(); | |
} else { | |
throw new IOException("unexpected char " + (char) ch); | |
} | |
} | |
public static JsonStructure readRoot(String data) throws IOException { | |
PushbackReader reader = new PushbackReader(new StringReader(data)); | |
return readRoot(reader); | |
} | |
public static void main(String[] args) throws IOException { | |
System.out.println(readRoot("[1,2,3,[4],{a:1,b:2,c:{},d:[]},[5,6,{}]]")); | |
System.out.println(readRoot("{a:[1,2,3,[4],{a:1,b:2,c:{},d:[]},[5,6,{}]]}")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment