Skip to content

Commit 3a5b85a

Browse files
authored
User defined deserializer/serializer in collection (#588)
User defined deserializer/serializer in collection Signed-off-by: David Kral <[email protected]>
1 parent 6e7ec79 commit 3a5b85a

File tree

7 files changed

+196
-5
lines changed

7 files changed

+196
-5
lines changed

src/main/java/org/eclipse/yasson/internal/deserializer/DeserializationModelCreator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2023 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
@@ -269,9 +269,10 @@ private ModelDeserializer<JsonParser> createCollectionDeserializer(CachedItem ca
269269
? ((ParameterizedType) type).getActualTypeArguments()[0]
270270
: Object.class;
271271
colType = ReflectionUtils.resolveType(chain, colType);
272+
ClassModel classModel = jsonbContext.getMappingContext().getOrCreateClassModel(ReflectionUtils.getRawType(colType));
272273
ModelDeserializer<JsonParser> typeProcessor = typeProcessor(chain,
273274
colType,
274-
propertyCustomization,
275+
classModel.getClassCustomization(),
275276
JustReturn.instance());
276277
CollectionDeserializer collectionDeserializer = new CollectionDeserializer(typeProcessor);
277278
CollectionInstanceCreator instanceDeserializer = new CollectionInstanceCreator(collectionDeserializer, type);

src/main/java/org/eclipse/yasson/internal/serializer/SerializationModelCreator.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2023 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
@@ -286,7 +286,10 @@ private ModelSerializer createCollectionSerializer(LinkedList<Type> chain,
286286
Type colType = type instanceof ParameterizedType
287287
? ((ParameterizedType) type).getActualTypeArguments()[0]
288288
: Object.class;
289-
ModelSerializer typeSerializer = memberSerializer(chain, colType, customization, false);
289+
Type resolvedKey = ReflectionUtils.resolveType(chain, colType);
290+
Class<?> rawClass = ReflectionUtils.getRawType(resolvedKey);
291+
ClassModel classModel = jsonbContext.getMappingContext().getOrCreateClassModel(rawClass);
292+
ModelSerializer typeSerializer = memberSerializer(chain, colType, classModel.getClassCustomization(), false);
290293
CollectionSerializer collectionSerializer = new CollectionSerializer(typeSerializer);
291294
KeyWriter keyWriter = new KeyWriter(collectionSerializer);
292295
NullVisibilitySwitcher nullVisibilitySwitcher = new NullVisibilitySwitcher(true, keyWriter);

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2022 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2023 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
@@ -51,6 +51,8 @@
5151
import org.eclipse.yasson.serializers.model.Author;
5252
import org.eclipse.yasson.serializers.model.Box;
5353
import org.eclipse.yasson.serializers.model.BoxWithAnnotations;
54+
import org.eclipse.yasson.serializers.model.Containee;
55+
import org.eclipse.yasson.serializers.model.Container;
5456
import org.eclipse.yasson.serializers.model.Crate;
5557
import org.eclipse.yasson.serializers.model.CrateDeserializer;
5658
import org.eclipse.yasson.serializers.model.CrateDeserializerWithConversion;
@@ -797,4 +799,17 @@ public void testBoxToArray() {
797799
assertThat(jsonb.toJson(box), is(expected));
798800
}
799801

802+
@Test
803+
public void testCustomSerializersInContainer(){
804+
Jsonb jsonb = JsonbBuilder.create();
805+
806+
Container expected = new Container(List.of(new Containee("k", "v")));
807+
808+
String expectedJson = jsonb.toJson(expected);
809+
System.out.println(expectedJson);
810+
811+
assertEquals(expected, jsonb.fromJson(expectedJson, Container.class));
812+
813+
}
814+
800815
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0,
7+
* or the Eclipse Distribution License v. 1.0 which is available at
8+
* http://www.eclipse.org/org/documents/edl-v10.php.
9+
*
10+
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
*/
12+
13+
package org.eclipse.yasson.serializers.model;
14+
15+
import java.util.Objects;
16+
17+
import jakarta.json.bind.annotation.JsonbTypeDeserializer;
18+
import jakarta.json.bind.annotation.JsonbTypeSerializer;
19+
20+
@JsonbTypeDeserializer(ContaineeDeserializer.class)
21+
@JsonbTypeSerializer(ContaineeSerializer.class)
22+
public class Containee {
23+
final String key;
24+
final String value;
25+
26+
public Containee(String key, String value) {
27+
this.key = key;
28+
this.value = value;
29+
}
30+
31+
@Override
32+
public boolean equals(Object o) {
33+
if (this == o) {
34+
return true;
35+
}
36+
if (o == null || getClass() != o.getClass()) {
37+
return false;
38+
}
39+
Containee containee = (Containee) o;
40+
return Objects.equals(key, containee.key) && Objects.equals(value, containee.value);
41+
}
42+
43+
@Override
44+
public int hashCode() {
45+
return Objects.hash(key, value);
46+
}
47+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0,
7+
* or the Eclipse Distribution License v. 1.0 which is available at
8+
* http://www.eclipse.org/org/documents/edl-v10.php.
9+
*
10+
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
*/
12+
13+
package org.eclipse.yasson.serializers.model;
14+
15+
import java.lang.reflect.Type;
16+
17+
import jakarta.json.bind.serializer.DeserializationContext;
18+
import jakarta.json.bind.serializer.JsonbDeserializer;
19+
import jakarta.json.stream.JsonParser;
20+
21+
public class ContaineeDeserializer implements JsonbDeserializer<Containee> {
22+
23+
@Override
24+
public Containee deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) {
25+
String key = null;
26+
String value = "";
27+
while (parser.hasNext()) {
28+
var event = parser.next();
29+
if (event == JsonParser.Event.KEY_NAME && parser.getString().equals("key")) {
30+
parser.next(); // move to VALUE
31+
key = parser.getString();
32+
} else if (event == JsonParser.Event.KEY_NAME && parser.getString().equals("value")) {
33+
parser.next(); // move to VALUE
34+
value = parser.getString();
35+
}
36+
}
37+
assert key != null;
38+
return new Containee(key, value);
39+
}
40+
41+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0,
7+
* or the Eclipse Distribution License v. 1.0 which is available at
8+
* http://www.eclipse.org/org/documents/edl-v10.php.
9+
*
10+
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
*/
12+
13+
package org.eclipse.yasson.serializers.model;
14+
15+
import jakarta.json.bind.serializer.JsonbSerializer;
16+
import jakarta.json.bind.serializer.SerializationContext;
17+
import jakarta.json.stream.JsonGenerator;
18+
19+
public class ContaineeSerializer implements JsonbSerializer<Containee> {
20+
21+
@Override
22+
public void serialize(Containee obj, JsonGenerator generator, SerializationContext ctx) {
23+
generator.writeStartObject();
24+
generator.write("key", obj.key);
25+
generator.write("value", obj.value);
26+
generator.writeEnd();
27+
}
28+
29+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0,
7+
* or the Eclipse Distribution License v. 1.0 which is available at
8+
* http://www.eclipse.org/org/documents/edl-v10.php.
9+
*
10+
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
*/
12+
13+
package org.eclipse.yasson.serializers.model;
14+
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
import java.util.Objects;
18+
19+
public class Container {
20+
21+
private List<Containee> containees;
22+
23+
public Container() {
24+
containees = new ArrayList<>();
25+
}
26+
27+
public Container(List<Containee> containees) {
28+
this.containees = containees;
29+
}
30+
31+
public void setContainees(List<Containee> containees) {
32+
this.containees = containees;
33+
}
34+
35+
public List<Containee> getContainees() {
36+
return containees;
37+
}
38+
39+
@Override
40+
public boolean equals(Object o) {
41+
if (this == o) {
42+
return true;
43+
}
44+
if (o == null || getClass() != o.getClass()) {
45+
return false;
46+
}
47+
Container container = (Container) o;
48+
return Objects.equals(containees, container.containees);
49+
}
50+
51+
@Override
52+
public int hashCode() {
53+
return Objects.hash(containees);
54+
}
55+
}

0 commit comments

Comments
 (0)