Search in sources :

Example 1 with JsonLocation

use of jakarta.json.stream.JsonLocation in project avro-util by linkedin.

the class JsonReaderWithLocations method readJsonValueInternal.

protected JsonValueExt readJsonValueInternal() {
    JsonLocation endLocation;
    switch(currentEvent) {
        case START_ARRAY:
            return readJsonArrayInternal();
        case START_OBJECT:
            return readObjectInternal();
        case KEY_NAME:
            throw new JsonException("unexpected " + currentEvent + " at " + parser.getLocation());
        case VALUE_STRING:
            String str = parser.getString();
            endLocation = parser.getLocation();
            return new JsonStringExtImpl(source, JsonPUtil.subtract(endLocation, str.length()), endLocation, str);
        case VALUE_NUMBER:
            // we dont bother optimizing for ints/longs
            return new JsonNumberExtImpl(source, parser.getLocation(), parser.getLocation(), parser.getBigDecimal());
        case VALUE_TRUE:
            endLocation = parser.getLocation();
            return new JsonTrueExtImpl(source, JsonPUtil.subtract(endLocation, 4), endLocation);
        case VALUE_FALSE:
            endLocation = parser.getLocation();
            return new JsonFalseExtImpl(source, JsonPUtil.subtract(endLocation, 5), endLocation);
        case VALUE_NULL:
            endLocation = parser.getLocation();
            return new JsonNullExtImpl(source, JsonPUtil.subtract(endLocation, 4), endLocation);
        case END_ARRAY:
            throw new JsonException("unexpected " + currentEvent + " at " + parser.getLocation());
        case END_OBJECT:
            throw new JsonException("unexpected " + currentEvent + " at " + parser.getLocation());
        default:
            throw new JsonException("unexpected " + currentEvent + " at " + parser.getLocation());
    }
}
Also used : JsonException(jakarta.json.JsonException) JsonLocation(jakarta.json.stream.JsonLocation)

Example 2 with JsonLocation

use of jakarta.json.stream.JsonLocation 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

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