use of com.linkedin.avroutil1.parser.jsonpext.JsonObjectExt in project avro-util by linkedin.
the class AvscParser method getOptionalArray.
private JsonArrayExt getOptionalArray(JsonObjectExt node, String prop) {
JsonValueExt val = node.get(prop);
if (val == null) {
return null;
}
JsonValue.ValueType valType = val.getValueType();
if (valType != JsonValue.ValueType.ARRAY) {
throw new AvroSyntaxException("\"" + prop + "\" property at " + val.getStartLocation() + " is expected to be a string, not a " + JsonPUtil.describe(valType) + " (" + val + ")");
}
return (JsonArrayExt) val;
}
use of com.linkedin.avroutil1.parser.jsonpext.JsonObjectExt in project avro-util by linkedin.
the class AvscParser method getOptionalString.
private Located<String> getOptionalString(JsonObjectExt node, String prop) {
JsonValueExt val = node.get(prop);
if (val == null) {
return null;
}
JsonValue.ValueType valType = val.getValueType();
if (valType != JsonValue.ValueType.STRING) {
throw new AvroSyntaxException("\"" + prop + "\" property at " + val.getStartLocation() + " is expected to be a string, not a " + JsonPUtil.describe(valType) + " (" + val + ")");
}
return new Located<>(((JsonStringExt) val).getString(), Util.convertLocation(val.getStartLocation()));
}
use of com.linkedin.avroutil1.parser.jsonpext.JsonObjectExt 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