Skip to content

Commit e75e87e

Browse files
authored
Merge pull request #130 from dblevins/jsonbcreator-adater-nullvalue
Tolerate null @JsonbCreator values when adapters are used
2 parents a94c3d0 + 0f031c7 commit e75e87e

File tree

2 files changed

+80
-14
lines changed

2 files changed

+80
-14
lines changed

johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/AdapterTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*/
2020

2121

22+
import jakarta.json.bind.annotation.JsonbCreator;
23+
import jakarta.json.bind.annotation.JsonbProperty;
2224
import org.junit.Test;
2325

2426
import jakarta.json.Json;
@@ -39,6 +41,7 @@
3941
import static org.hamcrest.CoreMatchers.endsWith;
4042
import static org.hamcrest.CoreMatchers.startsWith;
4143
import static org.junit.Assert.assertEquals;
44+
import static org.junit.Assert.assertNull;
4245
import static org.junit.Assert.assertThat;
4346
import static org.junit.Assert.assertTrue;
4447

@@ -136,6 +139,39 @@ public void adaptJson() throws Exception {
136139
}
137140
}
138141

142+
@Test
143+
public void adaptJsonWithCreator() throws Exception {
144+
try (final Jsonb jsonb = JsonbBuilder.create()) {
145+
final String json = "{\"value\":\"orange\", \"dummy\":\"2\"}";
146+
147+
final Foo3 foo3 = jsonb.fromJson(json, Foo3.class);
148+
assertEquals("orange", foo3.value);
149+
assertEquals(2, foo3.dummy.value);
150+
}
151+
}
152+
153+
@Test
154+
public void adaptJsonWithCreatorNull() throws Exception {
155+
try (final Jsonb jsonb = JsonbBuilder.create()) {
156+
final String json = "{\"value\":\"orange\"}";
157+
158+
final Foo3 foo3 = jsonb.fromJson(json, Foo3.class);
159+
assertEquals("orange", foo3.value);
160+
assertNull( foo3.dummy);
161+
}
162+
}
163+
164+
@Test
165+
public void adaptJsonWithFieldNull() throws Exception {
166+
try (final Jsonb jsonb = JsonbBuilder.create()) {
167+
final String json = "{\"value\":\"orange\"}";
168+
169+
final Foo4 foo4 = jsonb.fromJson(json, Foo4.class);
170+
assertEquals("orange", foo4.value);
171+
assertNull( foo4.dummy);
172+
}
173+
}
174+
139175
@Test
140176
public void notYetPloymorphism() { // we run it since it checked list/item conversion
141177
final Bar bar = new Bar();
@@ -239,6 +275,26 @@ public void setValue(final Bar value) {
239275
}
240276
}
241277

278+
public static class Foo3 {
279+
public String value;
280+
281+
@JsonbTypeAdapter(DummyAdapter.class)
282+
public Dummy dummy;
283+
284+
@JsonbCreator
285+
public Foo3(@JsonbProperty("dummy") @JsonbTypeAdapter(DummyAdapter.class) final Dummy dummy,
286+
@JsonbProperty("value") final String value) {
287+
this.dummy = dummy;
288+
this.value = value;
289+
}
290+
}
291+
292+
public static class Foo4 {
293+
public String value;
294+
295+
@JsonbTypeAdapter(DummyAdapter.class)
296+
public Dummy dummy;
297+
}
242298

243299
public static class Foo2 {
244300
@JsonbTypeAdapter(Dummy2Adapter.class)
@@ -391,4 +447,6 @@ public DoorStatus adaptFromJson(Integer obj) {
391447
}
392448

393449
}
450+
451+
394452
}

johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingParserImpl.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,10 @@ private Object convertTo(final Adapter converter, final JsonValue jsonValue, fin
498498
return converter.to(jsonValue);
499499
}
500500

501+
if (jsonValue == null) {
502+
return getNullValue(targetType);
503+
}
504+
501505
if (JsonValue.ValueType.OBJECT == valueType) {
502506
if (JsonObject.class == key.getTo() || JsonStructure.class == key.getTo()) {
503507
return converter.to(jsonValue.asJsonObject());
@@ -598,20 +602,7 @@ private Object toObject(final Object baseInstance, final JsonValue jsonValue,
598602
final Type type, final Adapter itemConverter, final JsonPointerTracker jsonPointer,
599603
final Type rootType) {
600604
if (jsonValue == null) {
601-
if (OptionalInt.class == type) {
602-
return OptionalInt.empty();
603-
}
604-
if (OptionalDouble.class == type) {
605-
return OptionalDouble.empty();
606-
}
607-
if (OptionalLong.class == type) {
608-
return OptionalLong.empty();
609-
}
610-
if (type instanceof ParameterizedType && Optional.class == ((ParameterizedType)type).getRawType()) {
611-
return Optional.empty();
612-
}
613-
614-
return null;
605+
return getNullValue(type);
615606
}
616607

617608
JsonValue.ValueType valueType = jsonValue.getValueType();
@@ -752,6 +743,23 @@ private Object toObject(final Object baseInstance, final JsonValue jsonValue,
752743
throw new MapperException("Unable to parse " + description + " to " + type + ": " + snippet);
753744
}
754745

746+
private static Object getNullValue(final Type type) {
747+
if (OptionalInt.class == type) {
748+
return OptionalInt.empty();
749+
}
750+
if (OptionalDouble.class == type) {
751+
return OptionalDouble.empty();
752+
}
753+
if (OptionalLong.class == type) {
754+
return OptionalLong.empty();
755+
}
756+
if (type instanceof ParameterizedType && Optional.class == ((ParameterizedType) type).getRawType()) {
757+
return Optional.empty();
758+
}
759+
760+
return null;
761+
}
762+
755763
private Object buildArray(final Type type, final JsonArray jsonArray, final Adapter itemConverter,
756764
final ObjectConverter.Reader objectConverter,
757765
final JsonPointerTracker jsonPointer, final Type rootType) {

0 commit comments

Comments
 (0)