in jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/json/JsonParser.java [81:188]
public void parse(Reader reader) throws IOException {
//StringBuffer key = new StringBuffer();
StringBuffer value = new StringBuffer();
int state;
Stack complexVStack = new Stack();
int next = reader.read();
if (next == '{') {
handler.object();
complexVStack.push(OBJECT);
state = KEY_START;
next = readIgnoreWhitespace(reader);
} else {
throw new IOException("JSON object must start with a '{'");
}
while (next != EOF) {
switch (state) {
case KEY_START:
if (next == '"') {
String key = nextString(reader, '\"');
next = readIgnoreWhitespace(reader);
if (next == ':') {
handler.key(key);
state = VALUE_START;
} else {
throw new IOException("Key-Value pairs must be separated by ':'");
}
next = readIgnoreWhitespace(reader);
} else if (next == '}') {
// empty object
state = VALUE;
} else {
throw new IOException("Key must be in String format (double quotes)");
}
break;
case VALUE_START:
if (next == '[') {
handler.array();
complexVStack.push(ARRAY);
// status still value_start
next = readIgnoreWhitespace(reader);
} else if (next == '{') {
handler.object();
complexVStack.push(OBJECT);
state = KEY_START;
next = readIgnoreWhitespace(reader);
} else if (next == '\"') {
handler.value(nextString(reader, '\"'));
next = readIgnoreWhitespace(reader);
if (!(next == ',' || next == ']' || next == '}')) {
throw new IOException("Invalid json format");
}
} else {
// start of boolean/long/double/null value
// will be notified as key-value pair
state = VALUE;
}
break;
case VALUE:
if (next == '"') {
throw new IOException("Invalid json format");
} else if (next == ',') {
state = (complexVStack.peek() == OBJECT) ? KEY_START : VALUE_START;
value = resetValue(value);
next = readIgnoreWhitespace(reader);
} else if (next == ']') {
if (complexVStack.pop() != ARRAY) {
throw new IOException("Invalid json format: Unexpected array termination.");
}
value = resetValue(value);
handler.endArray();
next = readIgnoreWhitespace(reader);
if (!(next == ',' || next == '}' || next == ']')) {
throw new IOException("Invalid json format");
}
} else if (next == '}') {
if (complexVStack.pop() != OBJECT) {
throw new IOException("Invalid json format: Unexpected object termination.");
}
value = resetValue(value);
handler.endObject();
next = readIgnoreWhitespace(reader);
if (!(next == ',' || next == '}' || next == ']' || next == EOF)) {
throw new IOException("Invalid json format");
}
} else {
// simple value
value.append((char) next);
next = reader.read();
}
break;
}
}
// EOF reached -> minimal validation check
if (value.length() != 0) {
throw new IOException("Invalid json format");
}
}