Skip to content

Commit

Permalink
Fix UserDeserializer is called only once for an array of json objects… (
Browse files Browse the repository at this point in the history
  • Loading branch information
bravehorsie authored and m0mus committed Jun 26, 2019
1 parent 9efdf32 commit b6947aa
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2015, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019 Payara Foundation and/or its affiliates. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
Expand Down Expand Up @@ -125,7 +125,7 @@ public JsonbDeserializer<?> build() {
}

//Third deserializer is a supported value type to deserialize to JSON_VALUE
if (isJsonValueEvent()) {
if (isJsonValueEvent(jsonEvent)) {
final Optional<AbstractValueTypeDeserializer<?>> supportedTypeDeserializer = getSupportedTypeDeserializer(rawType);
if (!supportedTypeDeserializer.isPresent()) {
if (jsonEvent == JsonParser.Event.VALUE_NULL) {
Expand Down Expand Up @@ -178,8 +178,14 @@ public JsonbDeserializer<?> build() {
throw new JsonbException("unresolved type for deserialization: " + getRuntimeType());
}

private boolean isJsonValueEvent() {
switch (jsonEvent) {
/**
* Checks if event is a value event.
*
* @param event JSON event to check.
* @return True if one of value events.
*/
public static boolean isJsonValueEvent(JsonParser.Event event) {
switch (event) {
case VALUE_NULL:
case VALUE_FALSE:
case VALUE_TRUE:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2019 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 v1.0 and Eclipse Distribution License v. 1.0
* which accompanies this distribution.
Expand Down Expand Up @@ -63,8 +63,9 @@ public void deserializeInternal(JsonbParser parser, Unmarshaller context) {
JsonParser.Event lastEvent = parserContext.getLastEvent();
final UserDeserializerParser userDeserializerParser = new UserDeserializerParser(parser);
deserializerResult = (T) deserializerBinding.getJsonbDeserializer().deserialize(userDeserializerParser, context, getRuntimeType());
//Avoid moving parser to the end of the object, if deserializer was for one value only.
if (lastEvent == JsonParser.Event.START_ARRAY || lastEvent == JsonParser.Event.START_OBJECT) {
//In case deserialized structure is json object or array and the parser is not advanced
//after enclosing bracket of deserialized object.
if (parser.getCurrentLevel() == parserContext && !DeserializerBuilder.isJsonValueEvent(lastEvent)) {
userDeserializerParser.advanceParserToEnd();
}
}
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/org/eclipse/yasson/serializers/SerializersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
Expand All @@ -28,11 +29,15 @@
import java.util.TimeZone;
import java.util.TreeMap;

import javax.json.JsonObject;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;
import javax.json.bind.JsonbException;
import javax.json.bind.config.PropertyOrderStrategy;
import javax.json.bind.serializer.DeserializationContext;
import javax.json.bind.serializer.JsonbDeserializer;
import javax.json.stream.JsonParser;

import org.eclipse.yasson.TestTypeToken;
import org.eclipse.yasson.internal.model.ReverseTreeMap;
Expand Down Expand Up @@ -447,6 +452,37 @@ public void testSerializeMapWithNulls() {
assertEquals("{\"null\":\"value\"}", jsonb.toJson(singletonMap(null, "value")));
}

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

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

public class SimplePojo {
private String stringProperty;

public String getStringProperty() {
return stringProperty;
}

public void setStringProperty(String stringProperty) {
this.stringProperty = stringProperty;
}
}

private Box createPojoWithDates() {
Date date = getExpectedDate();
Box box = createPojo();
Expand Down

0 comments on commit b6947aa

Please sign in to comment.