Skip to content

Commit 598d3c0

Browse files
committed
Add (now failing) tests for escaping with apostrophe-quoting
1 parent 56c4371 commit 598d3c0

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

release-notes/VERSION-2.x

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ JSON library.
3939
(reported by Alex R)
4040
#548: ByteQuadsCanonicalizer: ArrayIndexOutOfBoundsException in addName
4141
(reported by Alex R)
42+
#549: Add configurability of "quote character" for JSON factory
4243

4344
2.9.10 (not yet released)
4445

src/test/java/com/fasterxml/jackson/core/json/CustomQuoteCharTest.java

+58-12
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,14 @@ public void testBasicAposWithCharBased() throws Exception
2020
// with Object
2121
w = new StringWriter();
2222
g = createGenerator(JSON_F, w);
23-
g.writeStartObject();
24-
g.writeStringField("question", "answer");
25-
g.writeEndObject();
23+
_writeObject(g, "question", "answer");
2624
g.close();
2725
assertEquals("{'question':'answer'}", w.toString());
2826

2927
// with Array
3028
w = new StringWriter();
3129
g = createGenerator(JSON_F, w);
32-
g.writeStartArray();
33-
g.writeString("hello world");
34-
g.writeEndArray();
30+
_writeArray(g, "hello world");
3531
g.close();
3632
assertEquals("['hello world']", w.toString());
3733
}
@@ -44,19 +40,69 @@ public void testBasicAposWithByteBased() throws Exception
4440
// with Object
4541
out = new ByteArrayOutputStream();
4642
g = createGenerator(JSON_F, out);
47-
g.writeStartObject();
48-
g.writeStringField("question", "answer");
49-
g.writeEndObject();
43+
_writeObject(g, "question", "answer");
5044
g.close();
5145
assertEquals("{'question':'answer'}", out.toString("UTF-8"));
5246

5347
// with Array
5448
out = new ByteArrayOutputStream();
5549
g = createGenerator(JSON_F, out);
56-
g.writeStartArray();
57-
g.writeString("hello world");
58-
g.writeEndArray();
50+
_writeArray(g, "hello world");
5951
g.close();
6052
assertEquals("['hello world']", out.toString("UTF-8"));
6153
}
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+
}
62108
}

0 commit comments

Comments
 (0)