|
| 1 | +package com.fasterxml.jackson.databind; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonIgnore; |
| 4 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 5 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | + |
| 9 | +public class RecordTest extends BaseMapTest { |
| 10 | + |
| 11 | + private JsonMapper jsonMapper; |
| 12 | + |
| 13 | + public void setUp() { |
| 14 | + jsonMapper = new JsonMapper(); |
| 15 | + } |
| 16 | + |
| 17 | + record SimpleRecord(int id, String name) { |
| 18 | + } |
| 19 | + |
| 20 | + public void testSerializeSimpleRecord() throws JsonProcessingException { |
| 21 | + SimpleRecord record = new SimpleRecord(123, "Bob"); |
| 22 | + |
| 23 | + String json = jsonMapper.writeValueAsString(record); |
| 24 | + |
| 25 | + assertEquals("{\"id\":123,\"name\":\"Bob\"}", json); |
| 26 | + } |
| 27 | + |
| 28 | + public void testDeserializeSimpleRecord() throws IOException { |
| 29 | + SimpleRecord value = jsonMapper.readValue("{\"id\":123,\"name\":\"Bob\"}", SimpleRecord.class); |
| 30 | + |
| 31 | + assertEquals(new SimpleRecord(123, "Bob"), value); |
| 32 | + } |
| 33 | + |
| 34 | + public void testSerializeSimpleRecord_DisableAnnotationIntrospector() throws JsonProcessingException { |
| 35 | + SimpleRecord record = new SimpleRecord(123, "Bob"); |
| 36 | + |
| 37 | + JsonMapper mapper = JsonMapper.builder() |
| 38 | + .configure(MapperFeature.USE_ANNOTATIONS, false) |
| 39 | + .build(); |
| 40 | + String json = mapper.writeValueAsString(record); |
| 41 | + |
| 42 | + assertEquals("{\"id\":123,\"name\":\"Bob\"}", json); |
| 43 | + } |
| 44 | + |
| 45 | + public void testDeserializeSimpleRecord_DisableAnnotationIntrospector() throws IOException { |
| 46 | + JsonMapper mapper = JsonMapper.builder() |
| 47 | + .configure(MapperFeature.USE_ANNOTATIONS, false) |
| 48 | + .build(); |
| 49 | + SimpleRecord value = mapper.readValue("{\"id\":123,\"name\":\"Bob\"}", SimpleRecord.class); |
| 50 | + |
| 51 | + assertEquals(new SimpleRecord(123, "Bob"), value); |
| 52 | + } |
| 53 | + |
| 54 | + record RecordOfRecord(SimpleRecord record) { |
| 55 | + } |
| 56 | + |
| 57 | + public void testSerializeRecordOfRecord() throws JsonProcessingException { |
| 58 | + RecordOfRecord record = new RecordOfRecord(new SimpleRecord(123, "Bob")); |
| 59 | + |
| 60 | + String json = jsonMapper.writeValueAsString(record); |
| 61 | + |
| 62 | + assertEquals("{\"record\":{\"id\":123,\"name\":\"Bob\"}}", json); |
| 63 | + } |
| 64 | + |
| 65 | + record JsonIgnoreRecord(int id, @JsonIgnore String name) { |
| 66 | + } |
| 67 | + |
| 68 | + public void testSerializeJsonIgnoreRecord() throws JsonProcessingException { |
| 69 | + JsonIgnoreRecord record = new JsonIgnoreRecord(123, "Bob"); |
| 70 | + |
| 71 | + String json = jsonMapper.writeValueAsString(record); |
| 72 | + |
| 73 | + assertEquals("{\"id\":123}", json); |
| 74 | + } |
| 75 | + |
| 76 | + record RecordWithConstructor(int id, String name) { |
| 77 | + public RecordWithConstructor(int id) { |
| 78 | + this(id, "name"); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public void testDeserializeRecordWithConstructor() throws IOException { |
| 83 | + RecordWithConstructor value = jsonMapper.readValue("{\"id\":123,\"name\":\"Bob\"}", RecordWithConstructor.class); |
| 84 | + |
| 85 | + assertEquals(new RecordWithConstructor(123, "Bob"), value); |
| 86 | + } |
| 87 | +} |
0 commit comments