de.undercouch.citeproc.helper.json.MapJsonBuilder Maven / Gradle / Ivy
package de.undercouch.citeproc.helper.json;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* A JSON builder that creates maps that represent JSON objects
* @author Michel Kraemer
*/
public class MapJsonBuilder implements JsonBuilder {
private final JsonBuilderFactory factory;
private final Map m;
/**
* Creates a JSON builder
* @param factory the factory that created this builder
*/
public MapJsonBuilder(JsonBuilderFactory factory) {
this.factory = factory;
m = new LinkedHashMap<>();
}
@Override
public JsonBuilder add(String name, Object o) {
m.put(name, toJson(o, factory));
return this;
}
@Override
public Map build() {
return m;
}
@Override
public Object toJson(Object arr) {
return toJson(arr, factory);
}
/**
* Converts an object to a JSON object
* @param obj the object to convert
* @param factory a factory used to create JSON builders
* @return the JSON object
*/
private static Object toJson(Object obj, JsonBuilderFactory factory) {
if (obj instanceof JsonObject) {
return ((JsonObject)obj).toJson(factory.createJsonBuilder());
} else if (obj.getClass().isArray()) {
List