5
5
import com .fasterxml .jackson .annotation .JsonProperty ;
6
6
import com .fasterxml .jackson .annotation .JsonPropertyOrder ;
7
7
8
+ import tools .jackson .databind .MapperFeature ;
8
9
import tools .jackson .databind .ObjectMapper ;
9
10
import tools .jackson .databind .testutil .DatabindTestUtil ;
10
11
@@ -14,18 +15,20 @@ public class RecordSerializationOrderTest extends DatabindTestUtil
14
15
{
15
16
record NestedRecordOne (String id , String email , NestedRecordTwo nestedRecordTwo ) {}
16
17
record NestedRecordOneWithJsonProperty (String id , String email ,
17
- @ JsonProperty ("nestedProperty" ) NestedRecordTwo nestedRecordTwo ) {}
18
+ @ JsonProperty ("nestedProperty" ) NestedRecordTwo nestedRecordTwo ) {}
18
19
record NestedRecordOneWithJsonPropertyIndex (@ JsonProperty (index = 2 ) String id ,
19
- @ JsonProperty (index = 0 ) String email ,
20
- @ JsonProperty (value = "nestedProperty" , index = 1 ) NestedRecordTwo nestedRecordTwo ) {}
20
+ @ JsonProperty (index = 0 ) String email ,
21
+ @ JsonProperty (value = "nestedProperty" , index = 1 ) NestedRecordTwo nestedRecordTwo ) {}
21
22
22
23
@ JsonPropertyOrder ({"email" , "nestedProperty" , "id" })
23
24
record NestedRecordOneWithJsonPropertyOrder (String id ,
24
- String email ,
25
- @ JsonProperty (value = "nestedProperty" ) NestedRecordTwo nestedRecordTwo ) {}
25
+ String email ,
26
+ @ JsonProperty (value = "nestedProperty" ) NestedRecordTwo nestedRecordTwo ) {}
26
27
27
28
record NestedRecordTwo (String id , String passport ) {}
28
29
30
+ record CABRecord (String c , String a , String b ) {}
31
+
29
32
private final ObjectMapper MAPPER = newJsonMapper ();
30
33
31
34
/*
@@ -72,4 +75,25 @@ public void testSerializationOrderWithJsonPropertyOrder() throws Exception {
72
75
final String expected =
"{\" email\" :\" [email protected] \" ,\" nestedProperty\" :{\" id\" :\" 2\" ,\" passport\" :\" 111110\" },\" id\" :\" 1\" }" ;
73
76
assertEquals (expected , output );
74
77
}
78
+
79
+ // [databind#4580]
80
+ @ Test
81
+ public void testSerializationOrderWrtCreatorAlphabetic () throws Exception {
82
+ // In 3.0, sorting by Alphabetic enabled by default so
83
+ assertEquals (a2q ("{'a':'a','b':'b','c':'c'}" ),
84
+ MAPPER .writeValueAsString (new CABRecord ("c" , "a" , "b" )));
85
+ // But can disable
86
+ assertEquals (a2q ("{'c':'c','a':'a','b':'b'}" ),
87
+ jsonMapperBuilder ()
88
+ .disable (MapperFeature .SORT_PROPERTIES_ALPHABETICALLY )
89
+ .build ()
90
+ .writeValueAsString (new CABRecord ("c" , "a" , "b" )));
91
+ // Except if we tell it not to:
92
+ assertEquals (a2q ("{'c':'c','a':'a','b':'b'}" ),
93
+ jsonMapperBuilder ()
94
+ .enable (MapperFeature .SORT_PROPERTIES_ALPHABETICALLY )
95
+ .enable (MapperFeature .SORT_CREATOR_PROPERTIES_BY_DECLARATION_ORDER )
96
+ .build ()
97
+ .writeValueAsString (new CABRecord ("c" , "a" , "b" )));
98
+ }
75
99
}
0 commit comments