Search in sources :

Example 1 with JsonReaderWithLocations

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

the class AvscParser method parse.

public AvscParseResult parse(String avsc) {
    JsonReaderExt jsonReader = new JsonReaderWithLocations(new StringReader(avsc), null);
    JsonObjectExt root;
    AvscFileParseContext context = new AvscFileParseContext(avsc);
    AvscParseResult result = new AvscParseResult();
    try {
        root = jsonReader.readObject();
    } catch (JsonParsingException e) {
        Throwable rootCause = Util.rootCause(e);
        String message = rootCause.getMessage();
        if (message != null && message.startsWith("Unexpected char 47")) {
            // 47 is ascii for '/' (utf-8 matches ascii on "low" characters). we are going to assume this means
            // someone tried putting a //comment into an avsc source
            result.recordError(new JsonParseException("comments not supported in json at" + e.getLocation(), e));
        } else {
            result.recordError(new JsonParseException("json parse error at " + e.getLocation(), e));
        }
        return result;
    } catch (Exception e) {
        result.recordError(new JsonParseException("unknown json parse error", e));
        return result;
    }
    try {
        parseSchemaDeclOrRef(root, context, true);
        context.resolveReferences();
        result.recordParseComplete(context);
    } catch (Exception parseIssue) {
        result.recordError(parseIssue);
    }
    return result;
}
Also used : JsonObjectExt(com.linkedin.avroutil1.parser.jsonpext.JsonObjectExt) JsonReaderWithLocations(com.linkedin.avroutil1.parser.jsonpext.JsonReaderWithLocations) StringReader(java.io.StringReader) JsonReaderExt(com.linkedin.avroutil1.parser.jsonpext.JsonReaderExt) JsonParseException(com.linkedin.avroutil1.parser.exceptions.JsonParseException) ParseException(com.linkedin.avroutil1.parser.exceptions.ParseException) JsonParsingException(jakarta.json.stream.JsonParsingException) AvroSyntaxException(com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException) JsonParseException(com.linkedin.avroutil1.parser.exceptions.JsonParseException) JsonParsingException(jakarta.json.stream.JsonParsingException)

Example 2 with JsonReaderWithLocations

use of com.linkedin.avroutil1.parser.jsonpext.JsonReaderWithLocations 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)

Aggregations

JsonObjectExt (com.linkedin.avroutil1.parser.jsonpext.JsonObjectExt)2 JsonReaderExt (com.linkedin.avroutil1.parser.jsonpext.JsonReaderExt)2 JsonReaderWithLocations (com.linkedin.avroutil1.parser.jsonpext.JsonReaderWithLocations)2 StringReader (java.io.StringReader)2 AvroSyntaxException (com.linkedin.avroutil1.parser.exceptions.AvroSyntaxException)1 JsonParseException (com.linkedin.avroutil1.parser.exceptions.JsonParseException)1 ParseException (com.linkedin.avroutil1.parser.exceptions.ParseException)1 JsonValueExt (com.linkedin.avroutil1.parser.jsonpext.JsonValueExt)1 JsonObject (jakarta.json.JsonObject)1 JsonReader (jakarta.json.JsonReader)1 JsonLocation (jakarta.json.stream.JsonLocation)1 JsonParsingException (jakarta.json.stream.JsonParsingException)1 Test (org.testng.annotations.Test)1