diff --git a/src/test/java16/org/eclipse/yasson/records/CarId.java b/src/test/java16/org/eclipse/yasson/records/CarId.java new file mode 100644 index 000000000..960dd3925 --- /dev/null +++ b/src/test/java16/org/eclipse/yasson/records/CarId.java @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2023, 2023 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 v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, + * or the Eclipse Distribution License v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + */ + +package org.eclipse.yasson.records; + +import java.util.UUID; + +record CarId(UUID value) { +} diff --git a/src/test/java16/org/eclipse/yasson/records/CarWithUuidDeserializer.java b/src/test/java16/org/eclipse/yasson/records/CarWithUuidDeserializer.java new file mode 100644 index 000000000..09f7df588 --- /dev/null +++ b/src/test/java16/org/eclipse/yasson/records/CarWithUuidDeserializer.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023, 2023 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 v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, + * or the Eclipse Distribution License v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + */ + +package org.eclipse.yasson.records; + +import jakarta.json.bind.annotation.JsonbProperty; +import jakarta.json.bind.annotation.JsonbTypeDeserializer; +import jakarta.json.bind.serializer.DeserializationContext; +import jakarta.json.bind.serializer.JsonbDeserializer; +import jakarta.json.stream.JsonParser; + +import java.lang.reflect.Type; +import java.util.UUID; + +public record CarWithUuidDeserializer( + @JsonbProperty("car.id") @JsonbTypeDeserializer(CarUuidDeserializer.class) CarId carId, + @JsonbProperty("colour") String colour +) { + public static class CarUuidDeserializer implements JsonbDeserializer { + + @Override + public CarId deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) { + String givenString = parser.getString(); + var carUuid = UUID.fromString(givenString); + + return new CarId(carUuid); + } + } +} + + diff --git a/src/test/java16/org/eclipse/yasson/records/RecordTest.java b/src/test/java16/org/eclipse/yasson/records/RecordTest.java index 80854de6f..6ff54e318 100644 --- a/src/test/java16/org/eclipse/yasson/records/RecordTest.java +++ b/src/test/java16/org/eclipse/yasson/records/RecordTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2023 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 v. 2.0 which is available at @@ -19,6 +19,9 @@ import org.eclipse.yasson.internal.properties.Messages; import org.junit.jupiter.api.Test; +import java.util.Locale; +import java.util.UUID; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -102,11 +105,26 @@ public void testRecordParameterOptionality() { String json = Jsonbs.defaultJsonb.toJson(car); assertEquals(expected, json); - String toDeserialize = "{}"; - String expectedDefaultValues = "{\"color\":null,\"somePrimitive\":0,\"type\":\"typeDefaultValue\"}"; - CarCreatorOptionalTest deserialized = Jsonbs.defaultJsonb.fromJson(toDeserialize, CarCreatorOptionalTest.class); - json = Jsonbs.nullableJsonb.toJson(deserialized); - assertEquals(expectedDefaultValues, json); } + @Test + public void record_with_type_deserializer() { + UUID id = UUID.randomUUID(); + String given = String.format( + Locale.ROOT, + """ + { + "car.id": "%s", + "colour": "green" + } + """, + id); + + // when + CarWithUuidDeserializer car = Jsonbs.defaultJsonb.fromJson(given, CarWithUuidDeserializer.class); + + // then + assertEquals(id, car.carId().value()); + assertEquals("green", car.colour()); + } }