Search in sources :

Example 11 with JsonValueExt

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);
}
Also used : CodeLocation(com.linkedin.avroutil1.model.CodeLocation) TextLocation(com.linkedin.avroutil1.model.TextLocation)

Example 12 with JsonValueExt

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();
}
Also used : StringWriter(java.io.StringWriter) JsonValueExt(com.linkedin.avroutil1.parser.jsonpext.JsonValueExt) JsonWriter(jakarta.json.JsonWriter)

Example 13 with JsonValueExt

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&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)12 JsonValue (jakarta.json.JsonValue)8 CodeLocation (com.linkedin.avroutil1.model.CodeLocation)4 SchemaOrRef (com.linkedin.avroutil1.model.SchemaOrRef)4 AvroSyntaxException (com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException)4 JsonArrayExt (com.linkedin.avroutil1.parser.jsonpext.JsonArrayExt)4 JsonNumberExt (com.linkedin.avroutil1.parser.jsonpext.JsonNumberExt)4 ArrayList (java.util.ArrayList)4 JsonObjectExt (com.linkedin.avroutil1.parser.jsonpext.JsonObjectExt)3 AvroArraySchema (com.linkedin.avroutil1.model.AvroArraySchema)2 AvroEnumSchema (com.linkedin.avroutil1.model.AvroEnumSchema)2 AvroFixedSchema (com.linkedin.avroutil1.model.AvroFixedSchema)2 AvroLiteral (com.linkedin.avroutil1.model.AvroLiteral)2 AvroSchema (com.linkedin.avroutil1.model.AvroSchema)2 AvroType (com.linkedin.avroutil1.model.AvroType)2 JsonPropertiesContainer (com.linkedin.avroutil1.model.JsonPropertiesContainer)2 TextLocation (com.linkedin.avroutil1.model.TextLocation)2 Located (com.linkedin.avroutil1.parser.Located)2 JsonStringExt (com.linkedin.avroutil1.parser.jsonpext.JsonStringExt)2 LinkedHashMap (java.util.LinkedHashMap)2