Skip to content

Instantly share code, notes, and snippets.

@jhorstmann
Created November 12, 2012 22:43
Show Gist options
  • Save jhorstmann/4062559 to your computer and use it in GitHub Desktop.
Save jhorstmann/4062559 to your computer and use it in GitHub Desktop.
Using java collections instead of JsonBuilder
package net.jhorstmann.jsonp;
import java.io.IOException;
import java.io.PushbackReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Trying to get familiar with the proposed API of JSR353,
* this code uses the Java collection classes instead of JsonBuilder.
*/
public class RecurseiveParsingWithCollections {
public static void readObject(PushbackReader reader, Map object) 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 == '[') {
List child = new ArrayList();
object.put(key, child);
readArray(reader, child);
} else if (ch == '{') {
Map child = new LinkedHashMap();
object.put(key, child);
readObject(reader, child);
} else if (ch >= '0' && ch <= '9') {
object.put(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);
}
}
}
}
public static void readArray(PushbackReader reader, List list) 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 == '[') {
List child = new ArrayList();
list.add(child);
readArray(reader, child);
} else if (ch == '{') {
Map child = new LinkedHashMap();
list.add(child);
readObject(reader, child);
} else if (ch >= '0' && ch <= '9') {
list.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 Object readRoot(PushbackReader reader) throws IOException {
int ch = reader.read();
if (ch == '[') {
List result = new ArrayList();
readArray(reader, result);
return result;
} else if (ch == '{') {
Map result = new LinkedHashMap();
readObject(reader, result);
return result;
} else {
throw new IOException("unexpected char " + (char) ch);
}
}
public static Object 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