Search in sources :

Example 1 with JsonParser

use of jakarta.json.stream.JsonParser in project JsonPath by json-path.

the class JakartaMappingProvider method mapImpl.

private Object mapImpl(Object source, final Type targetType) {
    if (source == null || source == JsonValue.NULL) {
        return null;
    }
    if (source == JsonValue.TRUE) {
        if (Boolean.class.equals(targetType)) {
            return Boolean.TRUE;
        } else {
            String className = targetType.toString();
            throw new MappingException("JSON boolean (true) cannot be mapped to " + className);
        }
    }
    if (source == JsonValue.FALSE) {
        if (Boolean.class.equals(targetType)) {
            return Boolean.FALSE;
        } else {
            String className = targetType.toString();
            throw new MappingException("JSON boolean (false) cannot be mapped to " + className);
        }
    } else if (source instanceof JsonString) {
        if (String.class.equals(targetType)) {
            return ((JsonString) source).getChars();
        } else {
            String className = targetType.toString();
            throw new MappingException("JSON string cannot be mapped to " + className);
        }
    } else if (source instanceof JsonNumber) {
        JsonNumber jsonNumber = (JsonNumber) source;
        if (jsonNumber.isIntegral()) {
            return mapIntegralJsonNumber(jsonNumber, getRawClass(targetType));
        } else {
            return mapDecimalJsonNumber(jsonNumber, getRawClass(targetType));
        }
    }
    if (source instanceof JsonArrayBuilder) {
        source = ((JsonArrayBuilder) source).build();
    } else if (source instanceof JsonObjectBuilder) {
        source = ((JsonObjectBuilder) source).build();
    }
    if (source instanceof Collection) {
        // this covers both List<JsonValue> and JsonArray from JSON-P spec
        Class<?> rawTargetType = getRawClass(targetType);
        Type targetTypeArg = getFirstTypeArgument(targetType);
        Collection<Object> result = newCollectionOfType(rawTargetType);
        for (Object srcValue : (Collection<?>) source) {
            if (srcValue instanceof JsonObject) {
                if (targetTypeArg != null) {
                    result.add(mapImpl(srcValue, targetTypeArg));
                } else {
                    result.add(srcValue);
                }
            } else {
                result.add(unwrapJsonValue(srcValue));
            }
        }
        return result;
    } else if (source instanceof JsonObject) {
        if (targetType instanceof Class) {
            if (jsonToClassMethod != null) {
                try {
                    JsonParser jsonParser = new JsonStructureToParserAdapter((JsonStructure) source);
                    return jsonToClassMethod.invoke(jsonb, jsonParser, (Class<?>) targetType);
                } catch (Exception e) {
                    throw new MappingException(e);
                }
            } else {
                try {
                    // Fallback databinding approach for JSON-B API implementations without
                    // explicit support for use of JsonParser in their public API. The approach
                    // is essentially first to serialize given value into JSON, and then bind
                    // it to data object of given class.
                    String json = source.toString();
                    return jsonb.fromJson(json, (Class<?>) targetType);
                } catch (JsonbException e) {
                    throw new MappingException(e);
                }
            }
        } else if (targetType instanceof ParameterizedType) {
            if (jsonToTypeMethod != null) {
                try {
                    JsonParser jsonParser = new JsonStructureToParserAdapter((JsonStructure) source);
                    return jsonToTypeMethod.invoke(jsonb, jsonParser, (Type) targetType);
                } catch (Exception e) {
                    throw new MappingException(e);
                }
            } else {
                try {
                    // Fallback databinding approach for JSON-B API implementations without
                    // explicit support for use of JsonParser in their public API. The approach
                    // is essentially first to serialize given value into JSON, and then bind
                    // the JSON string to data object of given type.
                    String json = source.toString();
                    return jsonb.fromJson(json, (Type) targetType);
                } catch (JsonbException e) {
                    throw new MappingException(e);
                }
            }
        } else {
            throw new MappingException("JSON object cannot be databind to " + targetType);
        }
    } else {
        return source;
    }
}
Also used : JsonObject(jakarta.json.JsonObject) JsonString(jakarta.json.JsonString) JsonbException(jakarta.json.bind.JsonbException) JsonException(jakarta.json.JsonException) NoSuchElementException(java.util.NoSuchElementException) ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonNumber(jakarta.json.JsonNumber) JsonbException(jakarta.json.bind.JsonbException) Collection(java.util.Collection) JsonObject(jakarta.json.JsonObject) JsonString(jakarta.json.JsonString) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) JsonParser(jakarta.json.stream.JsonParser) JsonStructure(jakarta.json.JsonStructure)

Example 2 with JsonParser

use of jakarta.json.stream.JsonParser in project opensearch-java by opensearch-project.

the class JsonpUtils method expectNextEvent.

/**
 * Advances the parser to the next event and checks that this even is the expected one.
 *
 * @return the expected event
 *
 * @throws jakarta.json.JsonException if an i/o error occurs (IOException would be cause of JsonException)
 * @throws JsonParsingException if the event is not the expected one, or if the parser encounters invalid
 *         JSON when advancing to next state.
 * @throws java.util.NoSuchElementException if there are no more parsing states.
 */
public static JsonParser.Event expectNextEvent(JsonParser parser, JsonParser.Event expected) {
    JsonParser.Event event = parser.next();
    expectEvent(parser, expected, event);
    return event;
}
Also used : Event(jakarta.json.stream.JsonParser.Event) JsonParser(jakarta.json.stream.JsonParser)

Example 3 with JsonParser

use of jakarta.json.stream.JsonParser in project opensearch-java by opensearch-project.

the class JsonpMapperTest method testDeserialize.

private void testDeserialize(JsonpMapper mapper, String json) {
    JsonParser parser = mapper.jsonProvider().createParser(new StringReader(json));
    SomeClass parsed = mapper.deserialize(parser, SomeClass.class);
    assertEquals(1, parsed.getIntValue());
    assertEquals(2.1, parsed.getDoubleValue(), 0.0);
    assertEquals("foo", parsed.getStringValue());
    List<SomeClass> children = parsed.getChildren();
    assertEquals(1, children.size());
    SomeClass child = children.get(0);
    assertEquals(2, child.getIntValue());
    assertEquals(3.2, child.getDoubleValue(), 0.0);
    assertNull(child.getStringValue());
    assertNull(child.getChildren());
}
Also used : StringReader(java.io.StringReader) JsonParser(jakarta.json.stream.JsonParser)

Example 4 with JsonParser

use of jakarta.json.stream.JsonParser in project opensearch-java by opensearch-project.

the class JsonDataTest method testParsing.

@Test
public void testParsing() {
    JsonpMapper mapper = new JsonbJsonpMapper();
    String json = "{\"children\":[{\"doubleValue\":3.2,\"intValue\":2}],\"doubleValue\":2.1,\"intValue\":1," + "\"stringValue\":\"foo\"}";
    JsonParser parser = mapper.jsonProvider().createParser(new StringReader(json));
    JsonData data = JsonData.from(parser, mapper);
    assertEquals("foo", data.toJson().asJsonObject().getString("stringValue"));
    JsonpMapperTest.SomeClass to = data.to(JsonpMapperTest.SomeClass.class);
    assertEquals("foo", to.getStringValue());
}
Also used : JsonbJsonpMapper(org.opensearch.client.json.jsonb.JsonbJsonpMapper) JsonpMapper(org.opensearch.client.json.JsonpMapper) StringReader(java.io.StringReader) JsonbJsonpMapper(org.opensearch.client.json.jsonb.JsonbJsonpMapper) JsonString(jakarta.json.JsonString) JsonData(org.opensearch.client.json.JsonData) JsonParser(jakarta.json.stream.JsonParser) Test(org.junit.Test)

Example 5 with JsonParser

use of jakarta.json.stream.JsonParser in project opensearch-java by opensearch-project.

the class RestClientTransport method decodeResponse.

private <ResponseT> ResponseT decodeResponse(int statusCode, @Nullable HttpEntity entity, Response clientResp, Endpoint<?, ResponseT, ?> endpoint) throws IOException {
    if (endpoint instanceof BooleanEndpoint) {
        BooleanEndpoint<?> bep = (BooleanEndpoint<?>) endpoint;
        @SuppressWarnings("unchecked") ResponseT response = (ResponseT) new BooleanResponse(bep.getResult(statusCode));
        return response;
    } else if (endpoint instanceof JsonEndpoint) {
        @SuppressWarnings("unchecked") JsonEndpoint<?, ResponseT, ?> jsonEndpoint = (JsonEndpoint<?, ResponseT, ?>) endpoint;
        // Successful response
        ResponseT response = null;
        JsonpDeserializer<ResponseT> responseParser = jsonEndpoint.responseDeserializer();
        if (responseParser != null) {
            // Expecting a body
            if (entity == null) {
                throw new TransportException("Expecting a response body, but none was sent", new ResponseException(clientResp));
            }
            InputStream content = entity.getContent();
            try (JsonParser parser = mapper.jsonProvider().createParser(content)) {
                response = responseParser.deserialize(parser, mapper);
            }
            ;
        }
        return response;
    } else {
        throw new TransportException("Unhandled endpoint type: '" + endpoint.getClass().getName() + "'");
    }
}
Also used : JsonpDeserializer(org.opensearch.client.json.JsonpDeserializer) ResponseException(org.opensearch.client.ResponseException) InputStream(java.io.InputStream) BooleanEndpoint(org.opensearch.client.transport.endpoints.BooleanEndpoint) JsonEndpoint(org.opensearch.client.transport.JsonEndpoint) BooleanResponse(org.opensearch.client.transport.endpoints.BooleanResponse) TransportException(org.opensearch.client.transport.TransportException) JsonParser(jakarta.json.stream.JsonParser)

Aggregations

JsonParser (jakarta.json.stream.JsonParser)18 StringReader (java.io.StringReader)8 Test (org.junit.Test)8 JsonbJsonpMapper (org.opensearch.client.json.jsonb.JsonbJsonpMapper)5 JsonString (jakarta.json.JsonString)4 JsonProvider (jakarta.json.spi.JsonProvider)3 JsonArrayBuilder (jakarta.json.JsonArrayBuilder)2 JsonException (jakarta.json.JsonException)2 JsonNumber (jakarta.json.JsonNumber)2 JsonObject (jakarta.json.JsonObject)2 JsonObjectBuilder (jakarta.json.JsonObjectBuilder)2 JsonStructure (jakarta.json.JsonStructure)2 JsonbException (jakarta.json.bind.JsonbException)2 JsonGenerator (jakarta.json.stream.JsonGenerator)2 Event (jakarta.json.stream.JsonParser.Event)2 JsonParsingException (jakarta.json.stream.JsonParsingException)2 InputStream (java.io.InputStream)2 GenericArrayType (java.lang.reflect.GenericArrayType)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2