Skip to content

Commit

Permalink
[eclipse-ee4j#625]Forward JsonProvider to JsonStructureToParserAdapte…
Browse files Browse the repository at this point in the history
…r so it can be used for creation of JsonString for key in the getValue()

Signed-off-by: Anton Pinsky <[email protected]>
  • Loading branch information
api-from-the-ion committed Nov 4, 2023
1 parent 40db71e commit 339043a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 62 deletions.
49 changes: 16 additions & 33 deletions src/main/java/org/eclipse/yasson/internal/JsonBinding.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -51,70 +53,51 @@ public class JsonBinding implements YassonJsonb {
}
}

private <T> T deserialize(final Type type, final JsonParser parser, final DeserializationContextImpl unmarshaller) {
return unmarshaller.deserialize(type, parser);
private <T> T deserialize(final Type type, final Supplier<JsonParser> parserSupplier) {
Objects.requireNonNull(parserSupplier);
try (JsonParser parser = parserSupplier.get()) {
return new DeserializationContextImpl(jsonbContext).deserialize(type, parser);
}
}

@Override
public <T> T fromJson(String str, Class<T> 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> 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> T fromJson(Reader reader, Class<T> 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> 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> T fromJson(InputStream stream, Class<T> 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> 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> T fromJsonStructure(JsonStructure jsonStructure, Class<T> 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> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -48,15 +48,13 @@ class InheritanceInstanceCreator implements ModelDeserializer<JsonParser> {

@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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -119,7 +119,7 @@ public JsonValue getValue() {

@Override
String getString() {
if (state == JsonObjectIterator.State.KEY) {
if (state == State.KEY) {
return currentKey;
}
return super.getString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -53,16 +54,19 @@ public class JsonStructureToParserAdapter implements JsonParser {
private final Deque<JsonStructureIterator> 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;
}

/**
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<String> values = new ArrayList<>();
parser.next();
while (parser.hasNext()) {
Expand All @@ -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<String> values = new ArrayList<>();
parser.next();
while (parser.hasNext()) {
Expand All @@ -388,20 +388,15 @@ 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);
}
}

@Test
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();
Expand All @@ -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();
Expand All @@ -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)
Expand All @@ -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<String> values = parser.getValueStream().map(value -> Objects.toString(value, "null")).collect(Collectors.toList());

assertThat(values, contains(TestData.JSON_FAMILY_STRING));
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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)
Expand All @@ -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<String> values = parser.getObjectStream().collect(MAP_TO_LIST_COLLECTOR);

Expand Down

0 comments on commit 339043a

Please sign in to comment.