Skip to content

Commit 56c4371

Browse files
committed
First sort-of working version of #549
1 parent 2b19c0b commit 56c4371

File tree

7 files changed

+131
-10
lines changed

7 files changed

+131
-10
lines changed

src/main/java/com/fasterxml/jackson/core/JsonFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1567,7 +1567,7 @@ protected JsonParser _createParser(DataInput input, IOContext ctxt) throws IOExc
15671567
protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOException
15681568
{
15691569
WriterBasedJsonGenerator gen = new WriterBasedJsonGenerator(ctxt,
1570-
_generatorFeatures, _objectCodec, out);
1570+
_generatorFeatures, _objectCodec, out, _quoteChar);
15711571
if (_maximumNonEscapedChar > 0) {
15721572
gen.setHighestNonEscapedChar(_maximumNonEscapedChar);
15731573
}
@@ -1593,7 +1593,7 @@ protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOEx
15931593
*/
15941594
protected JsonGenerator _createUTF8Generator(OutputStream out, IOContext ctxt) throws IOException {
15951595
UTF8JsonGenerator gen = new UTF8JsonGenerator(ctxt,
1596-
_generatorFeatures, _objectCodec, out);
1596+
_generatorFeatures, _objectCodec, out, _quoteChar);
15971597
if (_maximumNonEscapedChar > 0) {
15981598
gen.setHighestNonEscapedChar(_maximumNonEscapedChar);
15991599
}

src/main/java/com/fasterxml/jackson/core/json/UTF8JsonGenerator.java

+28-3
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,17 @@ public class UTF8JsonGenerator
113113
/**********************************************************
114114
*/
115115

116+
/**
117+
* @since 2.10
118+
*/
116119
@SuppressWarnings("deprecation")
117120
public UTF8JsonGenerator(IOContext ctxt, int features, ObjectCodec codec,
118-
OutputStream out)
121+
OutputStream out, char quoteChar)
119122
{
120123
super(ctxt, features, codec);
121124
_outputStream = out;
125+
_quoteChar = (byte) quoteChar; // TODO: validate/truncate
126+
122127
_bufferRecyclable = true;
123128
_outputBuffer = ctxt.allocWriteEncodingBuffer();
124129
_outputEnd = _outputBuffer.length;
@@ -137,13 +142,18 @@ public UTF8JsonGenerator(IOContext ctxt, int features, ObjectCodec codec,
137142
}
138143
}
139144

145+
/**
146+
* @since 2.10
147+
*/
140148
public UTF8JsonGenerator(IOContext ctxt, int features, ObjectCodec codec,
141-
OutputStream out,
149+
OutputStream out, char quoteChar,
142150
byte[] outputBuffer, int outputOffset, boolean bufferRecyclable)
143151
{
144152

145153
super(ctxt, features, codec);
146154
_outputStream = out;
155+
_quoteChar = (byte) quoteChar; // TODO: validate/truncate
156+
147157
_bufferRecyclable = bufferRecyclable;
148158
_outputTail = outputOffset;
149159
_outputBuffer = outputBuffer;
@@ -154,12 +164,27 @@ public UTF8JsonGenerator(IOContext ctxt, int features, ObjectCodec codec,
154164
_charBufferLength = _charBuffer.length;
155165
}
156166

167+
@Deprecated // since 2.10
168+
public UTF8JsonGenerator(IOContext ctxt, int features, ObjectCodec codec,
169+
OutputStream out) {
170+
this(ctxt, features, codec, out, JsonFactory.DEFAULT_QUOTE_CHAR);
171+
}
172+
173+
@Deprecated // since 2.10
174+
public UTF8JsonGenerator(IOContext ctxt, int features, ObjectCodec codec,
175+
OutputStream out,
176+
byte[] outputBuffer, int outputOffset, boolean bufferRecyclable)
177+
{
178+
this(ctxt, features, codec, out, JsonFactory.DEFAULT_QUOTE_CHAR,
179+
outputBuffer, outputOffset, bufferRecyclable);
180+
}
181+
157182
/*
158183
/**********************************************************
159184
/* Overridden configuration methods
160185
/**********************************************************
161186
*/
162-
187+
163188
@Override
164189
public Object getOutputTarget() {
165190
return _outputStream;

src/main/java/com/fasterxml/jackson/core/json/WriterBasedJsonGenerator.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,25 @@ public class WriterBasedJsonGenerator
9292
/**********************************************************
9393
*/
9494

95+
@Deprecated // since 2.10
9596
public WriterBasedJsonGenerator(IOContext ctxt, int features,
96-
ObjectCodec codec, Writer w)
97+
ObjectCodec codec, Writer w) {
98+
this(ctxt, features, codec, w, JsonFactory.DEFAULT_QUOTE_CHAR);
99+
}
100+
101+
/**
102+
* @since 2.10
103+
*/
104+
public WriterBasedJsonGenerator(IOContext ctxt, int features,
105+
ObjectCodec codec, Writer w,
106+
char quoteChar)
107+
97108
{
98109
super(ctxt, features, codec);
99110
_writer = w;
100111
_outputBuffer = ctxt.allocConcatBuffer();
101112
_outputEnd = _outputBuffer.length;
113+
_quoteChar = quoteChar;
102114
}
103115

104116
/*

src/test/java/com/fasterxml/jackson/core/BaseTest.java

+24-2
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ protected void verifyIntValue(JsonParser p, long expValue)
302302

303303
/*
304304
/**********************************************************
305-
/* Parser/generator construction
305+
/* Parser construction
306306
/**********************************************************
307307
*/
308308

@@ -396,12 +396,34 @@ protected JsonParser createParserForDataInput(JsonFactory f,
396396
return f.createParser(input);
397397
}
398398

399+
/*
400+
/**********************************************************
401+
/* Generator construction
402+
/**********************************************************
403+
*/
404+
405+
protected JsonGenerator createGenerator(OutputStream out) throws IOException {
406+
return createGenerator(JSON_FACTORY, out);
407+
}
408+
409+
protected JsonGenerator createGenerator(TokenStreamFactory f, OutputStream out) throws IOException {
410+
return f.createGenerator(out);
411+
}
412+
413+
protected JsonGenerator createGenerator(Writer w) throws IOException {
414+
return createGenerator(JSON_FACTORY, w);
415+
}
416+
417+
protected JsonGenerator createGenerator(TokenStreamFactory f, Writer w) throws IOException {
418+
return f.createGenerator(w);
419+
}
420+
399421
/*
400422
/**********************************************************
401423
/* Helper read/write methods
402424
/**********************************************************
403425
*/
404-
426+
405427
protected void writeJsonDoc(JsonFactory f, String doc, Writer w) throws IOException
406428
{
407429
writeJsonDoc(f, doc, f.createGenerator(w));

src/test/java/com/fasterxml/jackson/core/TestVersions.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void testCoreVersions() throws Exception
1717
CharsToNameCanonicalizer.createRoot());
1818
assertVersion(jp.version());
1919
jp.close();
20-
JsonGenerator jgen = new WriterBasedJsonGenerator(getIOContext(), 0, null, null);
20+
JsonGenerator jgen = new WriterBasedJsonGenerator(getIOContext(), 0, null, null, '"');
2121
assertVersion(jgen.version());
2222
jgen.close();
2323
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.fasterxml.jackson.core.json;
2+
3+
import java.io.*;
4+
5+
import com.fasterxml.jackson.core.*;
6+
7+
// For [core#549], ability to use alternate quote characters
8+
public class CustomQuoteCharTest
9+
extends com.fasterxml.jackson.core.BaseTest
10+
{
11+
final JsonFactory JSON_F = streamFactoryBuilder()
12+
.quoteChar('\'')
13+
.build();
14+
15+
public void testBasicAposWithCharBased() throws Exception
16+
{
17+
StringWriter w;
18+
JsonGenerator g;
19+
20+
// with Object
21+
w = new StringWriter();
22+
g = createGenerator(JSON_F, w);
23+
g.writeStartObject();
24+
g.writeStringField("question", "answer");
25+
g.writeEndObject();
26+
g.close();
27+
assertEquals("{'question':'answer'}", w.toString());
28+
29+
// with Array
30+
w = new StringWriter();
31+
g = createGenerator(JSON_F, w);
32+
g.writeStartArray();
33+
g.writeString("hello world");
34+
g.writeEndArray();
35+
g.close();
36+
assertEquals("['hello world']", w.toString());
37+
}
38+
39+
public void testBasicAposWithByteBased() throws Exception
40+
{
41+
ByteArrayOutputStream out;
42+
JsonGenerator g;
43+
44+
// with Object
45+
out = new ByteArrayOutputStream();
46+
g = createGenerator(JSON_F, out);
47+
g.writeStartObject();
48+
g.writeStringField("question", "answer");
49+
g.writeEndObject();
50+
g.close();
51+
assertEquals("{'question':'answer'}", out.toString("UTF-8"));
52+
53+
// with Array
54+
out = new ByteArrayOutputStream();
55+
g = createGenerator(JSON_F, out);
56+
g.writeStartArray();
57+
g.writeString("hello world");
58+
g.writeEndArray();
59+
g.close();
60+
assertEquals("['hello world']", out.toString("UTF-8"));
61+
}
62+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void testUtf8Issue462() throws Exception
2020
{
2121
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
2222
IOContext ioc = new IOContext(new BufferRecycler(), bytes, true);
23-
JsonGenerator gen = new UTF8JsonGenerator(ioc, 0, null, bytes);
23+
JsonGenerator gen = new UTF8JsonGenerator(ioc, 0, null, bytes, '"');
2424
String str = "Natuurlijk is alles gelukt en weer een tevreden klant\uD83D\uDE04";
2525
int length = 4000 - 38;
2626

0 commit comments

Comments
 (0)