Skip to content

Commit 639b116

Browse files
committed
another fix
Signed-off-by: David Kral <[email protected]>
1 parent 54484db commit 639b116

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

src/main/java/org/eclipse/yasson/internal/DeserializationContextImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ public <T> T deserialize(Type type, JsonParser parser) {
110110
@SuppressWarnings("unchecked")
111111
private <T> T deserializeItem(Type type, JsonParser parser) {
112112
try {
113-
if (parser.currentEvent() == null) {
113+
JsonParser.Event startingEvent = parser.currentEvent();
114+
if (startingEvent == null || startingEvent == JsonParser.Event.KEY_NAME) {
114115
parser.next();
115116
checkState(parser);
116117
}

src/main/java/org/eclipse/yasson/internal/deserializer/JsonbCreatorDeserializer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
import org.eclipse.yasson.internal.properties.MessageKeys;
3030
import org.eclipse.yasson.internal.properties.Messages;
3131

32+
import static org.eclipse.yasson.internal.deserializer.ObjectDeserializer.DEFAULT_SKIPPER;
33+
import static org.eclipse.yasson.internal.deserializer.ObjectDeserializer.VALUE_SKIPPER;
34+
3235
/**
3336
* Creator of the Object instance with the usage of the {@link JsonbCreator}.
3437
*/
@@ -87,6 +90,8 @@ public Object deserialize(JsonParser parser, DeserializationContextImpl context)
8790
}
8891
} else if (failOnUnknownProperties && !ignoredProperties.contains(key)) {
8992
throw new JsonbException(Messages.getMessage(MessageKeys.UNKNOWN_JSON_PROPERTY, key, clazz));
93+
} else {
94+
VALUE_SKIPPER.getOrDefault(parser.currentEvent(), DEFAULT_SKIPPER).accept(parser);
9095
}
9196
break;
9297
case END_OBJECT:

src/main/java/org/eclipse/yasson/internal/deserializer/ObjectDeserializer.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import java.util.Map;
1616
import java.util.Set;
17+
import java.util.function.Consumer;
1718
import java.util.function.Function;
1819

1920
import jakarta.json.bind.JsonbException;
@@ -28,6 +29,14 @@
2829
*/
2930
class ObjectDeserializer implements ModelDeserializer<JsonParser> {
3031

32+
static final Map<JsonParser.Event, Consumer<JsonParser>> VALUE_SKIPPER;
33+
static final Consumer<JsonParser> DEFAULT_SKIPPER = jsonParser -> {}; //NO-OP
34+
35+
static {
36+
VALUE_SKIPPER = Map.of(JsonParser.Event.START_OBJECT, JsonParser::skipObject,
37+
JsonParser.Event.START_ARRAY, JsonParser::skipArray);
38+
}
39+
3140
private final Map<String, ModelDeserializer<JsonParser>> propertyDeserializerChains;
3241
private final Function<String, String> renamer;
3342
private final Class<?> rawClass;
@@ -69,6 +78,8 @@ public Object deserialize(JsonParser parser, DeserializationContextImpl context)
6978
}
7079
} else if (failOnUnknownProperty && !ignoredProperties.contains(key)) {
7180
throw new JsonbException(Messages.getMessage(MessageKeys.UNKNOWN_JSON_PROPERTY, key, rawClass));
81+
} else {
82+
VALUE_SKIPPER.getOrDefault(parser.currentEvent(), DEFAULT_SKIPPER).accept(parser);
7283
}
7384
break;
7485
case END_ARRAY:
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0,
7+
* or the Eclipse Distribution License v. 1.0 which is available at
8+
* http://www.eclipse.org/org/documents/edl-v10.php.
9+
*
10+
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
*/
12+
13+
package org.eclipse.yasson.defaultmapping;
14+
15+
import jakarta.json.bind.annotation.JsonbCreator;
16+
17+
import org.eclipse.yasson.Jsonbs;
18+
import org.junit.jupiter.api.Test;
19+
20+
import static org.hamcrest.MatcherAssert.assertThat;
21+
import static org.hamcrest.Matchers.is;
22+
import static org.hamcrest.Matchers.nullValue;
23+
24+
public class WrappedDeserialization {
25+
26+
private static final String TO_DESERIALIZE = "{\"inner\":{\"color\":\"yellow\"}}";
27+
28+
@Test
29+
public void testDeserializationOfInnerType() {
30+
Car deserialized = Jsonbs.defaultJsonb.fromJson(TO_DESERIALIZE, Car.class);
31+
assertThat(deserialized.color, is("red"));
32+
}
33+
34+
@Test
35+
public void testDeserializationOfInnerTypeWithCreator() {
36+
CarWithCreator deserialized = Jsonbs.defaultJsonb.fromJson(TO_DESERIALIZE, CarWithCreator.class);
37+
assertThat(deserialized.color, nullValue());
38+
}
39+
40+
public static class Car {
41+
42+
public String color = "red";
43+
44+
}
45+
46+
public static class CarWithCreator {
47+
48+
public String color;
49+
50+
@JsonbCreator
51+
public CarWithCreator(String color) {
52+
this.color = color;
53+
}
54+
55+
}
56+
57+
}

src/test/java/org/eclipse/yasson/jsonpsubstitution/PreinstantiatedJsonpTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ public Dog(String name, int age) {
4444

4545
@Override
4646
public String toString() {
47-
return "Dog:\nname: " + name + "\nage: " + age + "\ngoodDog: " + goodDog;
47+
return "Dog{"
48+
+ "name='" + name + "', "
49+
+ "age=" + age + ", "
50+
+ "goodDog=" + goodDog
51+
+ '}';
4852
}
4953
}
5054

@@ -125,6 +129,8 @@ public void testGeneratorWrappedWithUserInteraction() {
125129
assertEquals(WRAPPED_JSON, new String(out.toByteArray()));
126130
}
127131

132+
@Disabled("JsonParser now provides access to current event. "
133+
+ "This test passes since Yasson 3.0.0 due to addition of JsonParser.currentEvent()")
128134
@Test
129135
public void testInvalidJsonParserAdvancedToCustomPosition() {
130136
ByteArrayInputStream in = new ByteArrayInputStream(WRAPPED_JSON.getBytes());

0 commit comments

Comments
 (0)