Skip to content

Commit 31b832f

Browse files
committed
JOHNZON-420 use JsonBAdapter when converting back from array
1 parent f6664f8 commit 31b832f

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,23 @@ public void adaptCollectionValue() {
234234
assertEquals("42", adaptedBazCollection.collection.get(0).value);
235235
}
236236

237+
@Test
238+
public void testArrayOfClassWithTypeAdapter() {
239+
final Jsonb jsonb = JsonbBuilder.create(
240+
new JsonbConfig()
241+
.setProperty("johnzon.readAttributeBeforeWrite", true)
242+
.withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL) /* assertEquals() order */);
243+
244+
Baz[] bazs = new Baz[]{new Baz("A"), new Baz("B")};
245+
final String bazsJson = jsonb.toJson(bazs);
246+
assertEquals("[\"A\",\"B\"]", bazsJson);
247+
248+
249+
// and the other way around:
250+
final Baz[] bazs2 = jsonb.fromJson(bazsJson, Baz[].class);
251+
assertEquals(2, bazs2.length);
252+
}
253+
237254
public static class BarCollection {
238255
@JsonbTypeAdapter(BarAdapter.class)
239256
public List<Bar> collection;
@@ -336,6 +353,13 @@ public TypeInstance adaptToJson(final Bar obj) throws Exception {
336353

337354
@JsonbTypeAdapter(BazAdapter.class)
338355
public static class Baz {
356+
public Baz() {
357+
}
358+
359+
public Baz(String value) {
360+
this.value = value;
361+
}
362+
339363
public String value;
340364
}
341365

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,12 @@ private Adapter findAdapter(final Type aClass) {
12191219
}
12201220
return adapter;
12211221
}
1222+
1223+
final Mappings.ClassMapping classMapping = mappings.getClassMapping(aClass);
1224+
if (classMapping != null && classMapping.adapter != null) {
1225+
return classMapping.adapter;
1226+
}
1227+
12221228
config.getNoParserAdapterTypes().add(aClass);
12231229
return null;
12241230
}

0 commit comments

Comments
 (0)