9
9
import java .util .Map ;
10
10
import java .util .TreeSet ;
11
11
12
+ import com .fasterxml .jackson .annotation .JsonPropertyOrder ;
12
13
import com .fasterxml .jackson .databind .ObjectMapper ;
13
14
import com .fasterxml .jackson .databind .node .ObjectNode ;
14
15
import com .fasterxml .jackson .dataformat .yaml .ModuleTestBase ;
15
16
16
17
public class DatabindWriteTest extends ModuleTestBase
17
18
{
19
+ final ObjectMapper MAPPER = newObjectMapper ();
20
+
21
+ @ JsonPropertyOrder (alphabetic = true )
22
+ static class Point {
23
+ public int x , y ;
24
+
25
+ protected Point () { }
26
+ public Point (int x , int y ) {
27
+ this .x = x ;
28
+ this .y = y ;
29
+ }
30
+ }
31
+
18
32
public void testBasicPOJO () throws Exception
19
33
{
20
- ObjectMapper mapper = newObjectMapper ();
21
34
FiveMinuteUser user = new FiveMinuteUser ("Bob" , "Dabolito" , false ,
22
35
FiveMinuteUser .Gender .MALE , new byte [] { 1 , 3 , 13 , 79 });
23
- String yaml = mapper .writeValueAsString (user ).trim ();
36
+ String yaml = MAPPER .writeValueAsString (user ).trim ();
24
37
String [] parts = yaml .split ("\n " );
25
38
// unify ordering, need to use TreeSets to get alphabetic ordering
26
39
TreeSet <String > exp = new TreeSet <String >();
@@ -45,14 +58,27 @@ public void testBasicPOJO() throws Exception
45
58
assertFalse (it .hasNext ());
46
59
}
47
60
61
+ // Related to [dataformats-test#68], escaping of "reserved" names
62
+ public void testBasicDatabind2 () throws Exception
63
+ {
64
+ String yaml = trimDocMarker (MAPPER .writeValueAsString (new Point (1 , 2 )));
65
+
66
+ // Just verify 'y' will NOT be escaped
67
+ assertEquals ("x: 1\n y: 2" , yaml );
68
+
69
+ // Actually let's try reading back, too
70
+ Point p = MAPPER .readValue (yaml , Point .class );
71
+ assertEquals (1 , p .x );
72
+ assertEquals (2 , p .y );
73
+ }
74
+
48
75
public void testWithFile () throws Exception
49
76
{
50
77
File f = File .createTempFile ("test" , ".yml" );
51
78
f .deleteOnExit ();
52
- ObjectMapper mapper = newObjectMapper ();
53
79
Map <String ,Integer > map = new HashMap <String ,Integer >();
54
80
map .put ("a" , 3 );
55
- mapper .writeValue (f , map );
81
+ MAPPER .writeValue (f , map );
56
82
assertTrue (f .canRead ());
57
83
BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream (
58
84
f ), "UTF-8" ));
@@ -71,13 +97,12 @@ public void testWithFile2() throws Exception
71
97
{
72
98
File f = File .createTempFile ("test" , ".yml" );
73
99
f .deleteOnExit ();
74
- ObjectMapper mapper = newObjectMapper ();
75
- ObjectNode root = mapper .createObjectNode ();
100
+ ObjectNode root = MAPPER .createObjectNode ();
76
101
root .put ("name" , "Foobar" );
77
- mapper .writeValue (f , root );
102
+ MAPPER .writeValue (f , root );
78
103
79
104
// and get it back
80
- Map <?,?> result = mapper .readValue (f , Map .class );
105
+ Map <?,?> result = MAPPER .readValue (f , Map .class );
81
106
assertEquals (1 , result .size ());
82
107
assertEquals ("Foobar" , result .get ("name" ));
83
108
}
0 commit comments