|
| 1 | +package com.fasterxml.jackson.databind.records; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 8 | +import com.fasterxml.jackson.core.JsonParser; |
| 9 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | +import com.fasterxml.jackson.databind.SerializerProvider; |
| 12 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 13 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| 14 | +import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer; |
| 15 | +import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer; |
| 16 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | + |
| 20 | +/** |
| 21 | + * Test to verify that both {@link JsonSerialize} and {@link com.fasterxml.jackson.databind.annotation.JsonDeserialize} |
| 22 | + * work on records, as opposed to |
| 23 | + * [jackson#188 discussions](https://github.com/FasterXML/jackson/discussions/188#discussioncomment-11082943) |
| 24 | + */ |
| 25 | +public class RecordJsonSerDeser188Test |
| 26 | + extends DatabindTestUtil |
| 27 | +{ |
| 28 | + |
| 29 | + record Animal( |
| 30 | + @JsonDeserialize(using = PrefixStringDeserializer.class) |
| 31 | + @JsonSerialize(using = PrefixStringSerializer.class) |
| 32 | + String name, |
| 33 | + Integer age |
| 34 | + ) { } |
| 35 | + |
| 36 | + @SuppressWarnings("serial") |
| 37 | + static class PrefixStringSerializer extends StdScalarSerializer<String> { |
| 38 | + |
| 39 | + protected PrefixStringSerializer() { |
| 40 | + super(String.class); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void serialize(String value, JsonGenerator jgen, SerializerProvider provider) |
| 45 | + throws IOException |
| 46 | + { |
| 47 | + jgen.writeString("custom " + value); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + static class PrefixStringDeserializer extends StdScalarDeserializer<String> |
| 52 | + { |
| 53 | + protected PrefixStringDeserializer() { |
| 54 | + super(String.class); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public String deserialize(JsonParser jp, DeserializationContext ctxt) |
| 59 | + throws IOException |
| 60 | + { |
| 61 | + return "custom-deser" + jp.getText(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 66 | + |
| 67 | + @Test |
| 68 | + void testJsonSerializeOnRecord() |
| 69 | + throws Exception |
| 70 | + { |
| 71 | + Animal input = new Animal("dog", 3); |
| 72 | + |
| 73 | + String JSON = MAPPER.writeValueAsString(input); |
| 74 | + |
| 75 | + assertEquals(a2q("{'name':'custom dog','age':3}"), JSON); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void testJsonDeserializeOnRecord() |
| 80 | + throws Exception |
| 81 | + { |
| 82 | + String JSON = a2q("{'name':'cat','age':4}"); |
| 83 | + |
| 84 | + Animal result = MAPPER.readValue(JSON, Animal.class); |
| 85 | + |
| 86 | + assertEquals("custom-desercat", result.name()); |
| 87 | + assertEquals(4, result.age()); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments