Search in sources :

Example 76 with JsonObject

use of jakarta.json.JsonObject in project elasticsearch-java by elastic.

the class JsonpUtils method lookAheadFieldValue.

/**
 * Looks ahead a field value in the Json object from the upcoming object in a parser, which should be on the
 * START_OBJECT event.
 *
 * Returns a pair containing that value and a parser that should be used to actually parse the object
 * (the object has been consumed from the original one).
 */
public static Map.Entry<String, JsonParser> lookAheadFieldValue(String name, String defaultValue, JsonParser parser, JsonpMapper mapper) {
    // FIXME: need a buffering parser wrapper so that we don't roundtrip through a JsonObject and a String
    // FIXME: resulting parser should return locations that are offset with the original parser's location
    JsonObject object = parser.getObject();
    String result = object.getString(name, null);
    if (result == null) {
        result = defaultValue;
    }
    if (result == null) {
        throw new JsonParsingException("Property '" + name + "' not found", parser.getLocation());
    }
    return new AbstractMap.SimpleImmutableEntry<>(result, objectParser(object, mapper));
}
Also used : JsonObject(jakarta.json.JsonObject) JsonString(jakarta.json.JsonString) JsonParsingException(jakarta.json.stream.JsonParsingException)

Example 77 with JsonObject

use of jakarta.json.JsonObject 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 78 with JsonObject

use of jakarta.json.JsonObject in project opensearch-java by opensearch-project.

the class UnionDeserializer method deserialize.

@Override
public Union deserialize(JsonParser parser, JsonpMapper mapper, Event event) {
    EventHandler<Union, Kind, Member> member = otherMembers.get(event);
    if (member == null && event == Event.START_OBJECT && !objectMembers.isEmpty()) {
        // Parse as an object to find matching field names
        JsonObject object = parser.getObject();
        for (String field : object.keySet()) {
            member = objectMembers.get(field);
            if (member != null) {
                break;
            }
        }
        if (member == null) {
            member = fallbackObjectMember;
        }
        if (member != null) {
            // Traverse the object we have inspected
            parser = JsonpUtils.objectParser(object, mapper);
            event = parser.next();
        }
    }
    if (member == null) {
        throw new JsonParsingException("Cannot determine what union member to deserialize", parser.getLocation());
    }
    return member.deserialize(parser, mapper, event, buildFn);
}
Also used : JsonObject(jakarta.json.JsonObject) JsonParsingException(jakarta.json.stream.JsonParsingException)

Example 79 with JsonObject

use of jakarta.json.JsonObject in project opensearch-java by opensearch-project.

the class JsonpUtils method lookAheadFieldValue.

/**
 * Looks ahead a field value in the Json object from the upcoming object in a parser, which should be on the
 * START_OBJECT event.
 *
 * Returns a pair containing that value and a parser that should be used to actually parse the object
 * (the object has been consumed from the original one).
 */
public static Map.Entry<String, JsonParser> lookAheadFieldValue(String name, String defaultValue, JsonParser parser, JsonpMapper mapper) {
    // FIXME: need a buffering parser wrapper so that we don't roundtrip through a JsonObject and a String
    // FIXME: resulting parser should return locations that are offset with the original parser's location
    JsonObject object = parser.getObject();
    String result = object.getString(name, null);
    if (result == null) {
        result = defaultValue;
    }
    if (result == null) {
        throw new JsonParsingException("Property '" + name + "' not found", parser.getLocation());
    }
    return new AbstractMap.SimpleImmutableEntry<>(result, objectParser(object, mapper));
}
Also used : JsonObject(jakarta.json.JsonObject) JsonString(jakarta.json.JsonString) JsonParsingException(jakarta.json.stream.JsonParsingException)

Example 80 with JsonObject

use of jakarta.json.JsonObject in project helidon by oracle.

the class MainTest method personalizedGet.

private static JsonObject personalizedGet(String name) {
    JsonObject result = get("/greet/" + name);
    expectedPersonalizedGets++;
    return result;
}
Also used : JsonObject(jakarta.json.JsonObject)

Aggregations

JsonObject (jakarta.json.JsonObject)356 Test (org.junit.Test)125 Test (org.junit.jupiter.api.Test)92 JsonArray (jakarta.json.JsonArray)29 Response (jakarta.ws.rs.core.Response)29 JsonObjectBuilder (jakarta.json.JsonObjectBuilder)24 JsonString (jakarta.json.JsonString)24 JsonValue (jakarta.json.JsonValue)20 WebClientResponse (io.helidon.webclient.WebClientResponse)19 Client (jakarta.ws.rs.client.Client)18 StringReader (java.io.StringReader)17 HelidonTest (io.helidon.microprofile.tests.junit5.HelidonTest)15 JsonReader (jakarta.json.JsonReader)14 List (java.util.List)14 JsonException (jakarta.json.JsonException)13 Map (java.util.Map)13 Logger (java.util.logging.Logger)13 Routing (io.helidon.webserver.Routing)11 Json (jakarta.json.Json)11 Produces (jakarta.ws.rs.Produces)11