From 339043ac283afe1f4921f091b4a41424dfac766b Mon Sep 17 00:00:00 2001 From: Anton Pinsky Date: Sat, 4 Nov 2023 21:34:21 +0100 Subject: [PATCH] [#625]Forward JsonProvider to JsonStructureToParserAdapter so it can be used for creation of JsonString for key in the getValue() Signed-off-by: Anton Pinsky --- .../eclipse/yasson/internal/JsonBinding.java | 49 ++++++------------- .../InheritanceInstanceCreator.java | 8 ++- .../jsonstructure/JsonObjectIterator.java | 10 ++-- .../JsonStructureToParserAdapter.java | 10 +++- .../JsonStructureToParserAdapterTest.java | 29 +++++------ 5 files changed, 44 insertions(+), 62 deletions(-) diff --git a/src/main/java/org/eclipse/yasson/internal/JsonBinding.java b/src/main/java/org/eclipse/yasson/internal/JsonBinding.java index cd4764a3a..c994a0f5a 100644 --- a/src/main/java/org/eclipse/yasson/internal/JsonBinding.java +++ b/src/main/java/org/eclipse/yasson/internal/JsonBinding.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at @@ -21,7 +21,9 @@ import java.lang.reflect.Type; import java.nio.charset.Charset; import java.util.Map; +import java.util.Objects; import java.util.Set; +import java.util.function.Supplier; import jakarta.json.JsonStructure; import jakarta.json.bind.JsonbConfig; @@ -51,70 +53,51 @@ public class JsonBinding implements YassonJsonb { } } - private T deserialize(final Type type, final JsonParser parser, final DeserializationContextImpl unmarshaller) { - return unmarshaller.deserialize(type, parser); + private T deserialize(final Type type, final Supplier parserSupplier) { + Objects.requireNonNull(parserSupplier); + try (JsonParser parser = parserSupplier.get()) { + return new DeserializationContextImpl(jsonbContext).deserialize(type, parser); + } } @Override public T fromJson(String str, Class type) throws JsonbException { - try (JsonParser parser = jsonbContext.getJsonProvider().createParser(new StringReader(str))) { - final DeserializationContextImpl unmarshaller = new DeserializationContextImpl(jsonbContext); - return deserialize(type, parser, unmarshaller); - } + return deserialize(type, () -> jsonbContext.getJsonProvider().createParser(new StringReader(str))); } @Override public T fromJson(String str, Type type) throws JsonbException { - try (JsonParser parser = jsonbContext.getJsonProvider().createParser(new StringReader(str))) { - DeserializationContextImpl unmarshaller = new DeserializationContextImpl(jsonbContext); - return deserialize(type, parser, unmarshaller); - } + return deserialize(type, () -> jsonbContext.getJsonProvider().createParser(new StringReader(str))); } @Override public T fromJson(Reader reader, Class type) throws JsonbException { - try (JsonParser parser = jsonbContext.getJsonProvider().createParser(reader)) { - DeserializationContextImpl unmarshaller = new DeserializationContextImpl(jsonbContext); - return deserialize(type, parser, unmarshaller); - } + return deserialize(type, () -> jsonbContext.getJsonProvider().createParser(reader)); } @Override public T fromJson(Reader reader, Type type) throws JsonbException { - try (JsonParser parser = jsonbContext.getJsonProvider().createParser(reader)) { - DeserializationContextImpl unmarshaller = new DeserializationContextImpl(jsonbContext); - return deserialize(type, parser, unmarshaller); - } + return deserialize(type, () -> jsonbContext.getJsonProvider().createParser(reader)); } @Override public T fromJson(InputStream stream, Class clazz) throws JsonbException { - DeserializationContextImpl unmarshaller = new DeserializationContextImpl(jsonbContext); - try (JsonParser parser = inputStreamParser(stream)) { - return deserialize(clazz, inputStreamParser(stream), unmarshaller); - } + return deserialize(clazz, () -> inputStreamParser(stream)); } @Override public T fromJson(InputStream stream, Type type) throws JsonbException { - DeserializationContextImpl unmarshaller = new DeserializationContextImpl(jsonbContext); - try (JsonParser parser = inputStreamParser(stream)) { - return deserialize(type, inputStreamParser(stream), unmarshaller); - } + return deserialize(type, () -> inputStreamParser(stream)); } @Override public T fromJsonStructure(JsonStructure jsonStructure, Class type) throws JsonbException { - try (JsonParser parser = new JsonStructureToParserAdapter(jsonStructure)) { - return deserialize(type, parser, new DeserializationContextImpl(jsonbContext)); - } + return deserialize(type, () -> new JsonStructureToParserAdapter(jsonStructure, jsonbContext.getJsonProvider())); } @Override public T fromJsonStructure(JsonStructure jsonStructure, Type runtimeType) throws JsonbException { - try (JsonParser parser = new JsonStructureToParserAdapter(jsonStructure)) { - return deserialize(runtimeType, parser, new DeserializationContextImpl(jsonbContext)); - } + return deserialize(runtimeType, () -> new JsonStructureToParserAdapter(jsonStructure, jsonbContext.getJsonProvider())); } private JsonParser inputStreamParser(InputStream stream) { diff --git a/src/main/java/org/eclipse/yasson/internal/deserializer/InheritanceInstanceCreator.java b/src/main/java/org/eclipse/yasson/internal/deserializer/InheritanceInstanceCreator.java index efcc6c54e..6379ce927 100644 --- a/src/main/java/org/eclipse/yasson/internal/deserializer/InheritanceInstanceCreator.java +++ b/src/main/java/org/eclipse/yasson/internal/deserializer/InheritanceInstanceCreator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at @@ -48,15 +48,13 @@ class InheritanceInstanceCreator implements ModelDeserializer { @Override public Object deserialize(JsonParser parser, DeserializationContextImpl context) { - String alias; - JsonParser jsonParser; String polymorphismKeyName = typeInheritanceConfiguration.getFieldName(); JsonObject object = parser.getObject(); - alias = object.getString(polymorphismKeyName, null); + String alias = object.getString(polymorphismKeyName, null); JsonObject newJsonObject = context.getJsonbContext().getJsonProvider().createObjectBuilder(object) .remove(polymorphismKeyName) .build(); - jsonParser = new JsonStructureToParserAdapter(newJsonObject); + JsonParser jsonParser = new JsonStructureToParserAdapter(newJsonObject, context.getJsonbContext().getJsonProvider()); //To get to the first event Event event = jsonParser.next(); context.setLastValueEvent(event); diff --git a/src/main/java/org/eclipse/yasson/internal/jsonstructure/JsonObjectIterator.java b/src/main/java/org/eclipse/yasson/internal/jsonstructure/JsonObjectIterator.java index 4ba94c7dc..e1b983b3f 100644 --- a/src/main/java/org/eclipse/yasson/internal/jsonstructure/JsonObjectIterator.java +++ b/src/main/java/org/eclipse/yasson/internal/jsonstructure/JsonObjectIterator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at @@ -75,20 +75,20 @@ public JsonParser.Event next() { case START: if (keyIterator.hasNext()) { nextKey(); - setState(JsonObjectIterator.State.KEY); + setState(State.KEY); return JsonParser.Event.KEY_NAME; } else { setState(State.END); return JsonParser.Event.END_OBJECT; } case KEY: - setState(JsonObjectIterator.State.VALUE); + setState(State.VALUE); JsonValue value = getValue(); return getValueEvent(value); case VALUE: if (keyIterator.hasNext()) { nextKey(); - setState(JsonObjectIterator.State.KEY); + setState(State.KEY); return JsonParser.Event.KEY_NAME; } setState(State.END); @@ -119,7 +119,7 @@ public JsonValue getValue() { @Override String getString() { - if (state == JsonObjectIterator.State.KEY) { + if (state == State.KEY) { return currentKey; } return super.getString(); diff --git a/src/main/java/org/eclipse/yasson/internal/jsonstructure/JsonStructureToParserAdapter.java b/src/main/java/org/eclipse/yasson/internal/jsonstructure/JsonStructureToParserAdapter.java index 4a73a00f3..ab8e9d86f 100644 --- a/src/main/java/org/eclipse/yasson/internal/jsonstructure/JsonStructureToParserAdapter.java +++ b/src/main/java/org/eclipse/yasson/internal/jsonstructure/JsonStructureToParserAdapter.java @@ -31,6 +31,7 @@ import jakarta.json.JsonStructure; import jakarta.json.JsonValue; import jakarta.json.bind.JsonbException; +import jakarta.json.spi.JsonProvider; import jakarta.json.stream.JsonLocation; import jakarta.json.stream.JsonParser; @@ -53,16 +54,19 @@ public class JsonStructureToParserAdapter implements JsonParser { private final Deque iterators = new ArrayDeque<>(); private final JsonStructure rootStructure; + private final JsonProvider jsonProvider; private Event currentEvent; /** * Creates new {@link JsonStructure} parser. * - * @param structure json structure + * @param structure json structure + * @param jsonProvider json provider for creation of {@link JsonValue} for keys */ - public JsonStructureToParserAdapter(JsonStructure structure) { + public JsonStructureToParserAdapter(JsonStructure structure, JsonProvider jsonProvider) { this.rootStructure = structure; + this.jsonProvider = jsonProvider; } /** @@ -189,6 +193,8 @@ public JsonValue getValue() { return getObject(); case START_ARRAY: return getArray(); + case KEY_NAME: + return jsonProvider.createValue(iterator.getString()); default: return iterator.getValue(); } diff --git a/src/test/java/org/eclipse/yasson/jsonstructure/JsonStructureToParserAdapterTest.java b/src/test/java/org/eclipse/yasson/jsonstructure/JsonStructureToParserAdapterTest.java index 0137d7951..8f47d3627 100644 --- a/src/test/java/org/eclipse/yasson/jsonstructure/JsonStructureToParserAdapterTest.java +++ b/src/test/java/org/eclipse/yasson/jsonstructure/JsonStructureToParserAdapterTest.java @@ -320,7 +320,7 @@ public void testNumbers() { .add("BigDecimal", BigDecimal.TEN) .build(); - try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject)) { + try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject, jsonProvider)) { parser.next(); parser.next(); parser.getString(); @@ -358,7 +358,7 @@ public void testNumbers() { public void testParser_getString(){ JsonObject jsonObject = TestData.createFamilyPerson(); - try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject)) { + try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject, jsonProvider)) { List values = new ArrayList<>(); parser.next(); while (parser.hasNext()) { @@ -377,7 +377,7 @@ public void testParser_getString(){ public void testParser_getValue(){ JsonObject jsonObject = TestData.createFamilyPerson(); - try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject)) { + try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject, jsonProvider)) { List values = new ArrayList<>(); parser.next(); while (parser.hasNext()) { @@ -388,12 +388,7 @@ public void testParser_getValue(){ } } - //should look like this with a correct implementation -> FAMILY_MATCHER_KEYS_WITH_QUOTATION - /*assertThat(values, contains("\"name\"", "\"John\"", "\"surname\"", "\"Smith\"", "\"age\"", "30", "\"married\"", "true", - "\"wife\"", "{\"name\":\"Deborah\",\"surname\":\"Harris\"}", "\"children\"", "[\"Jack\",\"Mike\"]"));*/ - assertThat(values, contains("\"John\"", "\"John\"", "\"Smith\"", "\"Smith\"", "30", "30", "true", "true", - "{\"name\":\"Deborah\",\"surname\":\"Harris\"}", "{\"name\":\"Deborah\",\"surname\":\"Harris\"}", - "[\"Jack\",\"Mike\"]", "[\"Jack\",\"Mike\"]")); + assertThat(values, TestData.FAMILY_MATCHER_KEYS_WITH_QUOTATION); } } @@ -401,7 +396,7 @@ public void testParser_getValue(){ public void testSkipArray() { JsonObject jsonObject = TestData.createObjectWithArrays(); - try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject)) { + try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject, jsonProvider)) { parser.next(); parser.next(); parser.getString(); @@ -418,7 +413,7 @@ public void testSkipArray() { public void testSkipObject() { JsonObject jsonObject = TestData.createJsonObject(); - try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject)) { + try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject, jsonProvider)) { parser.next(); parser.next(); parser.getString(); @@ -438,7 +433,7 @@ public class StreamTests { public void testGetValueStream_GetOneElement() { JsonObject jsonObject = TestData.createFamilyPerson(); - try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject)) { + try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject, jsonProvider)) { JsonString name = (JsonString) parser.getValueStream() .map(JsonValue::asJsonObject) .map(JsonObject::values) @@ -457,7 +452,7 @@ public void testGetValueStream_GetOneElement() { public void testGetValueStream_GetList() { JsonObject jsonObject = TestData.createFamilyPerson(); - try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject)) { + try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject, jsonProvider)) { List values = parser.getValueStream().map(value -> Objects.toString(value, "null")).collect(Collectors.toList()); assertThat(values, contains(TestData.JSON_FAMILY_STRING)); @@ -468,7 +463,7 @@ public void testGetValueStream_GetList() { public void testGetArrayStream_GetOneElement() { JsonObject jsonObject = TestData.createObjectWithArrays(); - try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject)) { + try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject, jsonProvider)) { parser.next(); parser.next(); String key = parser.getString(); @@ -486,7 +481,7 @@ public void testGetArrayStream_GetOneElement() { public void testGetArrayStream_GetList() { JsonObject jsonObject = TestData.createObjectWithArrays(); - try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject)) { + try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject, jsonProvider)) { parser.next(); parser.next(); String key = parser.getString(); @@ -502,7 +497,7 @@ public void testGetArrayStream_GetList() { public void testGetObjectStream_GetOneElement() { JsonObject jsonObject = TestData.createJsonObject(); - try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject)) { + try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject, jsonProvider)) { parser.next(); String surname = parser.getObjectStream().filter(e -> e.getKey().equals("firstPerson")) .map(Map.Entry::getValue) @@ -519,7 +514,7 @@ public void testGetObjectStream_GetOneElement() { public void testGetObjectStream_GetList() { JsonObject jsonObject = TestData.createFamilyPerson(); - try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject)) { + try (JsonStructureToParserAdapter parser = new JsonStructureToParserAdapter(jsonObject, jsonProvider)) { parser.next(); List values = parser.getObjectStream().collect(MAP_TO_LIST_COLLECTOR);