use of com.linkedin.avroutil1.parser.jsonpext.JsonValueExt in project avro-util by linkedin.
the class AvscParser method locationOf.
private CodeLocation locationOf(URI uri, JsonValueExt node) {
TextLocation startLocation = Util.convertLocation(node.getStartLocation());
TextLocation endLocation = Util.convertLocation(node.getEndLocation());
return new CodeLocation(uri, startLocation, endLocation);
}
use of com.linkedin.avroutil1.parser.jsonpext.JsonValueExt in project avro-util by linkedin.
the class JsonPropertiesContainerImpl method getPropertyAsJsonLiteral.
@Override
public String getPropertyAsJsonLiteral(String key) {
JsonValueExt value = props.get(key);
if (value == null) {
return null;
}
StringWriter sw = new StringWriter();
try (JsonWriter writer = JSON_FACTORY.createWriter(sw)) {
writer.write(value);
}
return sw.toString();
}
use of com.linkedin.avroutil1.parser.jsonpext.JsonValueExt in project avro-util by linkedin.
the class Util method convertJsonValue.
/**
* converts json values to:
* <ul>
* <li>{@link com.linkedin.avroutil1.model.JsonPropertiesContainer#NULL_VALUE} for json nulls</li>
* <li>Booleans for json boolean values</li>
* <li>BigDecimals for json numeric values</li>
* <li>java.lang.Strings for json strings</li>
* <li>
* java.util.List<Object>s for json arrays, where individual members
* are anything on this list.
* </li>
* <li>
* java.util.LinkedHashMap<String,Object>s for json objects, where
* keys are strings, values are anything on this list, and property order
* is preserved
* </li>
* </ul>
* @param jsonValue a json value
* @return value converted according to the doc
*/
public static Object convertJsonValue(JsonValueExt jsonValue) {
if (jsonValue == null) {
throw new IllegalArgumentException("jsonValue cannot be null");
}
JsonValue.ValueType jsonType = jsonValue.getValueType();
switch(jsonType) {
case NULL:
return JsonPropertiesContainer.NULL_VALUE;
case TRUE:
return Boolean.TRUE;
case FALSE:
return Boolean.FALSE;
case NUMBER:
return ((JsonNumberExt) jsonValue).bigDecimalValue();
case STRING:
return ((JsonStringExt) jsonValue).getString();
case ARRAY:
JsonArrayExt jsonArray = (JsonArrayExt) jsonValue;
List<Object> list = new ArrayList<>(jsonArray.size());
for (JsonValue val : jsonArray) {
list.add(convertJsonValue((JsonValueExt) val));
}
return list;
case OBJECT:
JsonObjectExt jsonObj = (JsonObjectExt) jsonValue;
Set<Map.Entry<String, JsonValue>> entries = jsonObj.entrySet();
LinkedHashMap<String, Object> map = new LinkedHashMap<>(entries.size());
for (Map.Entry<String, JsonValue> entry : entries) {
String key = entry.getKey();
JsonValueExt val = (JsonValueExt) entry.getValue();
map.put(key, convertJsonValue(val));
}
return map;
default:
throw new IllegalStateException("unhandled json type " + jsonType + " for " + jsonValue + " at " + jsonValue.getStartLocation());
}
}
Aggregations