Skip to content

Commit 41d0e6c

Browse files
authored
Issue 342: Custom De-Serialization Bug (#481)
Signed-off-by: rmartinc <[email protected]>
1 parent d5a4985 commit 41d0e6c

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0 which is available at
@@ -271,7 +271,17 @@ public JsonObject getObject() {
271271

272272
@Override
273273
public JsonValue getValue() {
274-
return jsonParser.getValue();
274+
if (level.isEmpty() || getLastEvent() == null) {
275+
return jsonParser.getValue();
276+
}
277+
switch (getLastEvent()) {
278+
case START_ARRAY:
279+
return getArray();
280+
case START_OBJECT:
281+
return getObject();
282+
default:
283+
return jsonParser.getValue();
284+
}
275285
}
276286

277287
@Override

src/test/java/org/eclipse/yasson/serializers/SerializersTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,25 @@ public SimplePojo deserialize(JsonParser parser, DeserializationContext ctx, Typ
485485
}
486486
}
487487

488+
@Test
489+
public void testDeserializeArrayWithAdvancingParserAfterObjectEndUsingValue() {
490+
String json = "[{\"stringProperty\":\"Property 1 value\"},{\"stringProperty\":\"Property 2 value\"}]";
491+
Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withDeserializers(new SimplePojoValueDeserializer()));
492+
SimplePojo[] result = jsonb.fromJson(json, SimplePojo[].class);
493+
assertEquals(2, result.length);
494+
}
495+
496+
public class SimplePojoValueDeserializer implements JsonbDeserializer<SimplePojo> {
497+
@Override
498+
public SimplePojo deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) {
499+
//parser.getValue advances the parser to END_OBJECT in case of object.
500+
JsonObject json = parser.getValue().asJsonObject();
501+
SimplePojo simplePojo = new SimplePojo();
502+
simplePojo.setStringProperty(json.getString("stringProperty"));
503+
return simplePojo;
504+
}
505+
}
506+
488507
public class SimplePojo {
489508
private String stringProperty;
490509

0 commit comments

Comments
 (0)