Search in sources :

Example 6 with JsonObjectExt

use of com.linkedin.avroutil1.parser.jsonpext.JsonObjectExt in project avro-util by linkedin.

the class AvscParser method parseExtraProps.

private LinkedHashMap<String, JsonValueExt> parseExtraProps(JsonObjectExt field, Set<String> toIgnore) {
    LinkedHashMap<String, JsonValueExt> results = new LinkedHashMap<>(3);
    for (Map.Entry<String, JsonValue> entry : field.entrySet()) {
        // doc says there are in the order they are in json
        String propName = entry.getKey();
        if (toIgnore.contains(propName)) {
            continue;
        }
        JsonValueExt propValue = (JsonValueExt) entry.getValue();
        results.put(propName, propValue);
    }
    return results;
}
Also used : JsonValue(jakarta.json.JsonValue) JsonValueExt(com.linkedin.avroutil1.parser.jsonpext.JsonValueExt) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with JsonObjectExt

use of com.linkedin.avroutil1.parser.jsonpext.JsonObjectExt in project avro-util by linkedin.

the class TestJsonParsing method testSimpleSchema.

@Test
public void testSimpleSchema() throws Exception {
    String avsc = TestUtil.load("schemas/TestRecord.avsc");
    JsonReader referenceReader = Json.createReader(new StringReader(avsc));
    JsonObject referenceObject = referenceReader.readObject();
    JsonReaderExt ourReader = new JsonReaderWithLocations(new StringReader(avsc), null);
    JsonObjectExt ourObject = ourReader.readObject();
    // assert we read everything correctly. our equals() implementations are not
    // commutative with regular json-p objects (yet?)
    // noinspection SimplifiableAssertion
    Assert.assertTrue(referenceObject.equals(ourObject));
    // see that we have text locations for json values
    JsonValueExt fields = ourObject.get("fields");
    JsonLocation startLocation = fields.getStartLocation();
    JsonLocation endLocation = fields.getEndLocation();
    Assert.assertEquals(startLocation.getLineNumber(), 6);
    Assert.assertEquals(endLocation.getLineNumber(), 70);
}
Also used : JsonLocation(jakarta.json.stream.JsonLocation) JsonObjectExt(com.linkedin.avroutil1.parser.jsonpext.JsonObjectExt) StringReader(java.io.StringReader) JsonReaderWithLocations(com.linkedin.avroutil1.parser.jsonpext.JsonReaderWithLocations) JsonReader(jakarta.json.JsonReader) JsonObject(jakarta.json.JsonObject) JsonReaderExt(com.linkedin.avroutil1.parser.jsonpext.JsonReaderExt) JsonValueExt(com.linkedin.avroutil1.parser.jsonpext.JsonValueExt) Test(org.testng.annotations.Test)

Example 8 with JsonObjectExt

use of com.linkedin.avroutil1.parser.jsonpext.JsonObjectExt in project avro-util by linkedin.

the class AvscParser method parseCollectionSchema.

private AvroCollectionSchema parseCollectionSchema(JsonObjectExt objectNode, AvscFileParseContext context, AvroType avroType, CodeLocation codeLocation, JsonPropertiesContainer props) {
    switch(avroType) {
        case ARRAY:
            JsonValueExt arrayItemsNode = getRequiredNode(objectNode, "items", () -> "array declarations must have an items property");
            SchemaOrRef arrayItemSchema = parseSchemaDeclOrRef(arrayItemsNode, context, false);
            return new AvroArraySchema(codeLocation, arrayItemSchema, props);
        case MAP:
            JsonValueExt mapValuesNode = getRequiredNode(objectNode, "values", () -> "map declarations must have a values property");
            SchemaOrRef mapValueSchema = parseSchemaDeclOrRef(mapValuesNode, context, false);
            return new AvroMapSchema(codeLocation, mapValueSchema, props);
        default:
            throw new IllegalStateException("unhandled: " + avroType + " for object at " + codeLocation.getStart());
    }
}
Also used : AvroArraySchema(com.linkedin.avroutil1.model.AvroArraySchema) SchemaOrRef(com.linkedin.avroutil1.model.SchemaOrRef) AvroMapSchema(com.linkedin.avroutil1.model.AvroMapSchema) JsonValueExt(com.linkedin.avroutil1.parser.jsonpext.JsonValueExt)

Example 9 with JsonObjectExt

use of com.linkedin.avroutil1.parser.jsonpext.JsonObjectExt in project avro-util by linkedin.

the class AvscParser method parseStringRepresentation.

private Parsed<AvroJavaStringRepresentation> parseStringRepresentation(// type declaration node
JsonObjectExt objectNode, AvscFileParseContext context, AvroType avroType, CodeLocation codeLocation) {
    Parsed<AvroJavaStringRepresentation> result = new Parsed<>();
    Located<String> repStr = getOptionalString(objectNode, "avro.java.string");
    AvroJavaStringRepresentation representation;
    if (repStr != null) {
        representation = AvroJavaStringRepresentation.fromJson(repStr.getValue());
        if (representation != null) {
            result.setData(representation);
        } else {
            result.recordIssue(AvscIssues.unknownJavaStringRepresentation(locationOf(context.getUri(), repStr), repStr.getValue()));
        }
        // does string rep even make sense here? is type even a string?
        if (!AvroType.STRING.equals(avroType)) {
            result.recordIssue(AvscIssues.stringRepresentationOnNonString(locationOf(context.getUri(), repStr), repStr.getValue(), avroType));
        }
    }
    return result;
}
Also used : AvroJavaStringRepresentation(com.linkedin.avroutil1.model.AvroJavaStringRepresentation)

Example 10 with JsonObjectExt

use of com.linkedin.avroutil1.parser.jsonpext.JsonObjectExt in project avro-util by linkedin.

the class AvscParser method getOptionalInteger.

private Located<Integer> getOptionalInteger(JsonObjectExt node, String prop, AvscFileParseContext context) {
    JsonValueExt val = node.get(prop);
    if (val == null) {
        return null;
    }
    JsonValue.ValueType valType = val.getValueType();
    if (valType != JsonValue.ValueType.NUMBER) {
        context.addIssue(AvscIssues.badPropertyType(prop, locationOf(context.getUri(), val), val, valType.name(), JsonValue.ValueType.NUMBER.name()));
        return null;
    }
    JsonNumberExt numberNode = (JsonNumberExt) val;
    if (!numberNode.isIntegral()) {
        context.addIssue(AvscIssues.badPropertyType(prop, locationOf(context.getUri(), val), val, "floating point value", "integer"));
        return null;
    }
    return new Located<>(numberNode.intValueExact(), Util.convertLocation(val.getStartLocation()));
}
Also used : Located(com.linkedin.avroutil1.parser.Located) JsonValue(jakarta.json.JsonValue) JsonValueExt(com.linkedin.avroutil1.parser.jsonpext.JsonValueExt) 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