File tree Expand file tree Collapse file tree 3 files changed +57
-5
lines changed
src/test/java16/org/eclipse/yasson/records Expand file tree Collapse file tree 3 files changed +57
-5
lines changed Original file line number Diff line number Diff line change 1+ package org .eclipse .yasson .records ;
2+
3+ import java .util .UUID ;
4+
5+ record CarId (UUID value ) {
6+ }
Original file line number Diff line number Diff line change 1+ package org .eclipse .yasson .records ;
2+
3+ import jakarta .json .bind .annotation .JsonbProperty ;
4+ import jakarta .json .bind .annotation .JsonbTypeDeserializer ;
5+ import jakarta .json .bind .serializer .DeserializationContext ;
6+ import jakarta .json .bind .serializer .JsonbDeserializer ;
7+ import jakarta .json .stream .JsonParser ;
8+
9+ import java .lang .reflect .Type ;
10+ import java .util .UUID ;
11+
12+ public record CarWithUuidDeserializer (
13+ @ JsonbProperty ("car.id" ) @ JsonbTypeDeserializer (CarUuidDeserializer .class ) CarId carId ,
14+ @ JsonbProperty ("colour" ) String colour
15+ ) {
16+ public static class CarUuidDeserializer implements JsonbDeserializer <CarId > {
17+
18+ @ Override
19+ public CarId deserialize (JsonParser parser , DeserializationContext ctx , Type rtType ) {
20+ String givenString = parser .getString ();
21+ var carUuid = UUID .fromString (givenString );
22+
23+ return new CarId (carUuid );
24+ }
25+ }
26+ }
27+
28+
Original file line number Diff line number Diff line change 1919import org .eclipse .yasson .internal .properties .Messages ;
2020import org .junit .jupiter .api .Test ;
2121
22+ import java .util .Locale ;
23+ import java .util .UUID ;
24+
2225import static org .junit .jupiter .api .Assertions .assertEquals ;
2326import static org .junit .jupiter .api .Assertions .assertThrows ;
2427
@@ -102,11 +105,26 @@ public void testRecordParameterOptionality() {
102105 String json = Jsonbs .defaultJsonb .toJson (car );
103106 assertEquals (expected , json );
104107
105- String toDeserialize = "{}" ;
106- String expectedDefaultValues = "{\" color\" :null,\" somePrimitive\" :0,\" type\" :\" typeDefaultValue\" }" ;
107- CarCreatorOptionalTest deserialized = Jsonbs .defaultJsonb .fromJson (toDeserialize , CarCreatorOptionalTest .class );
108- json = Jsonbs .nullableJsonb .toJson (deserialized );
109- assertEquals (expectedDefaultValues , json );
110108 }
111109
110+ @ Test
111+ public void record_with_type_deserializer () {
112+ UUID id = UUID .randomUUID ();
113+ String given = String .format (
114+ Locale .ROOT ,
115+ """
116+ {
117+ "car.id": "%s",
118+ "colour": "green"
119+ }
120+ """ ,
121+ id );
122+
123+ // when
124+ CarWithUuidDeserializer car = Jsonbs .defaultJsonb .fromJson (given , CarWithUuidDeserializer .class );
125+
126+ // then
127+ assertEquals (id , car .carId ().value ());
128+ assertEquals ("green" , car .colour ());
129+ }
112130}
You can’t perform that action at this time.
0 commit comments