@@ -20,18 +20,14 @@ public void testBasicAposWithCharBased() throws Exception
20
20
// with Object
21
21
w = new StringWriter ();
22
22
g = createGenerator (JSON_F , w );
23
- g .writeStartObject ();
24
- g .writeStringField ("question" , "answer" );
25
- g .writeEndObject ();
23
+ _writeObject (g , "question" , "answer" );
26
24
g .close ();
27
25
assertEquals ("{'question':'answer'}" , w .toString ());
28
26
29
27
// with Array
30
28
w = new StringWriter ();
31
29
g = createGenerator (JSON_F , w );
32
- g .writeStartArray ();
33
- g .writeString ("hello world" );
34
- g .writeEndArray ();
30
+ _writeArray (g , "hello world" );
35
31
g .close ();
36
32
assertEquals ("['hello world']" , w .toString ());
37
33
}
@@ -44,19 +40,69 @@ public void testBasicAposWithByteBased() throws Exception
44
40
// with Object
45
41
out = new ByteArrayOutputStream ();
46
42
g = createGenerator (JSON_F , out );
47
- g .writeStartObject ();
48
- g .writeStringField ("question" , "answer" );
49
- g .writeEndObject ();
43
+ _writeObject (g , "question" , "answer" );
50
44
g .close ();
51
45
assertEquals ("{'question':'answer'}" , out .toString ("UTF-8" ));
52
46
53
47
// with Array
54
48
out = new ByteArrayOutputStream ();
55
49
g = createGenerator (JSON_F , out );
56
- g .writeStartArray ();
57
- g .writeString ("hello world" );
58
- g .writeEndArray ();
50
+ _writeArray (g , "hello world" );
59
51
g .close ();
60
52
assertEquals ("['hello world']" , out .toString ("UTF-8" ));
61
53
}
54
+
55
+ public void testAposQuotingWithCharBased () throws Exception
56
+ {
57
+ StringWriter w ;
58
+ JsonGenerator g ;
59
+
60
+ // with Object
61
+ w = new StringWriter ();
62
+ g = createGenerator (JSON_F , w );
63
+ _writeObject (g , "key" , "It's \" fun\" " );
64
+ g .close ();
65
+ // should escape apostrophes but not quotes?
66
+ assertEquals ("{'key':'It\\ 's' \" fun\" }" , w .toString ());
67
+
68
+ // with Array
69
+ w = new StringWriter ();
70
+ g = createGenerator (JSON_F , w );
71
+ _writeArray (g , "It's a sin" );
72
+ g .close ();
73
+ assertEquals ("['It\\ 's a sin']" , w .toString ());
74
+ }
75
+
76
+ public void testAposQuotingWithByteBased () throws Exception
77
+ {
78
+ ByteArrayOutputStream out ;
79
+ JsonGenerator g ;
80
+
81
+ // with Object
82
+ out = new ByteArrayOutputStream ();
83
+ g = createGenerator (JSON_F , out );
84
+ _writeObject (g , "key" , "It's \" fun\" " );
85
+ g .close ();
86
+ // should escape apostrophes but not quotes?
87
+ assertEquals ("{'key':'It\\ 's' \" fun\" }" , out .toString ("UTF-8" ));
88
+
89
+ // with Array
90
+ out = new ByteArrayOutputStream ();
91
+ g = createGenerator (JSON_F , out );
92
+ _writeArray (g , "It's a sin" );
93
+ g .close ();
94
+ assertEquals ("['It\\ 's a sin']" , out .toString ("UTF-8" ));
95
+ }
96
+
97
+ private void _writeObject (JsonGenerator g , String key , String value ) throws Exception {
98
+ g .writeStartObject ();
99
+ g .writeStringField (key , value );
100
+ g .writeEndObject ();
101
+ }
102
+
103
+ private void _writeArray (JsonGenerator g , String value ) throws Exception {
104
+ g .writeStartArray ();
105
+ g .writeString (value );
106
+ g .writeEndArray ();
107
+ }
62
108
}
0 commit comments