Search in sources :

Example 11 with JsonObjectExt

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;
}
Also used : JsonArrayExt(com.linkedin.avroutil1.parser.jsonpext.JsonArrayExt) AvroSyntaxException(com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException) JsonValue(jakarta.json.JsonValue) JsonValueExt(com.linkedin.avroutil1.parser.jsonpext.JsonValueExt)

Example 12 with JsonObjectExt

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()));
}
Also used : AvroSyntaxException(com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException) Located(com.linkedin.avroutil1.parser.Located) JsonValue(jakarta.json.JsonValue) JsonValueExt(com.linkedin.avroutil1.parser.jsonpext.JsonValueExt)

Example 13 with JsonObjectExt

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&lt;Object&gt;s for json arrays, where individual members
 *         are anything on this list.
 *     </li>
 *     <li>
 *         java.util.LinkedHashMap&lt;String,Object&gt;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());
    }
}
Also used : JsonStringExt(com.linkedin.avroutil1.parser.jsonpext.JsonStringExt) JsonValue(jakarta.json.JsonValue) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) JsonArrayExt(com.linkedin.avroutil1.parser.jsonpext.JsonArrayExt) JsonObjectExt(com.linkedin.avroutil1.parser.jsonpext.JsonObjectExt) JsonValueExt(com.linkedin.avroutil1.parser.jsonpext.JsonValueExt) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JsonNumberExt(com.linkedin.avroutil1.parser.jsonpext.JsonNumberExt)

Aggregations

JsonValueExt (com.linkedin.avroutil1.parser.jsonpext.JsonValueExt)9 JsonValue (jakarta.json.JsonValue)6 AvroSyntaxException (com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException)5 JsonObjectExt (com.linkedin.avroutil1.parser.jsonpext.JsonObjectExt)4 AvroLogicalType (com.linkedin.avroutil1.model.AvroLogicalType)3 SchemaOrRef (com.linkedin.avroutil1.model.SchemaOrRef)3 JsonArrayExt (com.linkedin.avroutil1.parser.jsonpext.JsonArrayExt)3 JsonNumberExt (com.linkedin.avroutil1.parser.jsonpext.JsonNumberExt)3 AvroJavaStringRepresentation (com.linkedin.avroutil1.model.AvroJavaStringRepresentation)2 CodeLocation (com.linkedin.avroutil1.model.CodeLocation)2 JsonPropertiesContainer (com.linkedin.avroutil1.model.JsonPropertiesContainer)2 Located (com.linkedin.avroutil1.parser.Located)2 JsonReaderExt (com.linkedin.avroutil1.parser.jsonpext.JsonReaderExt)2 JsonReaderWithLocations (com.linkedin.avroutil1.parser.jsonpext.JsonReaderWithLocations)2 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 AvroArraySchema (com.linkedin.avroutil1.model.AvroArraySchema)1 AvroEnumSchema (com.linkedin.avroutil1.model.AvroEnumSchema)1