Skip to content

Commit

Permalink
Issue 342: Custom De-Serialization Bug (#481)
Browse files Browse the repository at this point in the history
Signed-off-by: rmartinc <rmartinc@redhat.com>
  • Loading branch information
rmartinc committed Mar 12, 2021
1 parent d5a4985 commit 41d0e6c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main/java/org/eclipse/yasson/internal/JsonbRiParser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021 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 @@ -271,7 +271,17 @@ public JsonObject getObject() {

@Override
public JsonValue getValue() {
return jsonParser.getValue();
if (level.isEmpty() || getLastEvent() == null) {
return jsonParser.getValue();
}
switch (getLastEvent()) {
case START_ARRAY:
return getArray();
case START_OBJECT:
return getObject();
default:
return jsonParser.getValue();
}
}

@Override
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/eclipse/yasson/serializers/SerializersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,25 @@ public SimplePojo deserialize(JsonParser parser, DeserializationContext ctx, Typ
}
}

@Test
public void testDeserializeArrayWithAdvancingParserAfterObjectEndUsingValue() {
String json = "[{\"stringProperty\":\"Property 1 value\"},{\"stringProperty\":\"Property 2 value\"}]";
Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withDeserializers(new SimplePojoValueDeserializer()));
SimplePojo[] result = jsonb.fromJson(json, SimplePojo[].class);
assertEquals(2, result.length);
}

public class SimplePojoValueDeserializer implements JsonbDeserializer<SimplePojo> {
@Override
public SimplePojo deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) {
//parser.getValue advances the parser to END_OBJECT in case of object.
JsonObject json = parser.getValue().asJsonObject();
SimplePojo simplePojo = new SimplePojo();
simplePojo.setStringProperty(json.getString("stringProperty"));
return simplePojo;
}
}

public class SimplePojo {
private String stringProperty;

Expand Down

0 comments on commit 41d0e6c

Please sign in to comment.