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