
de.undercouch.citeproc.helper.json.JsonParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of citeproc-java Show documentation
Show all versions of citeproc-java Show documentation
A Citation Style Language (CSL) Processor for Java.
package de.undercouch.citeproc.helper.json;
import de.undercouch.citeproc.helper.json.JsonLexer.Type;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Parses JSON tokens to maps
* @author Michel Kraemer
*/
public class JsonParser {
private final JsonLexer lexer;
/**
* Constructs a new JSON parser
* @param lexer a JSON lexer to read from
*/
public JsonParser(JsonLexer lexer) {
this.lexer = lexer;
}
/**
* Parses an object into a map
* @return the parsed object
* @throws IOException if the input stream could not be read or if
* the input stream contained an unexpected token
*/
public Map parseObject() throws IOException {
Type t = lexer.readNextToken();
if (t != Type.START_OBJECT) {
throw new IOException("Unexpected token: " + t);
}
return parseObjectInternal();
}
/**
* Parses an object into a map without reading the
* {@link Type#START_OBJECT} token
* @return the parsed object
* @throws IOException if the input stream could not be read or if
* the input stream contained an unexpected token
*/
private Map parseObjectInternal() throws IOException {
Map result = new HashMap<>();
Type t;
while (true) {
t = lexer.readNextToken();
if (t == Type.END_OBJECT) {
break;
}
if (!result.isEmpty()) {
// skip comma and read next token
if (t != Type.COMMA) {
throw new IOException("Unexpected token: " + t);
}
t = lexer.readNextToken();
}
// first token must be the name
if (t != Type.STRING) {
throw new IOException("Unexpected token: " + t);
}
String name = lexer.readString();
// skip colon
t = lexer.readNextToken();
if (t != Type.COLON) {
throw new IOException("Unexpected token: " + t);
}
// next token must be the value
t = lexer.readNextToken();
Object value = readValue(t);
result.put(name, value);
}
return result;
}
/**
* Parses an array
* @return the parsed array
* @throws IOException if the input stream could not be read or if
* the input stream contained an unexpected token
*/
public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy