Skip to content

Commit 2b19c0b

Browse files
committed
Start work on #549
1 parent a92daeb commit 2b19c0b

File tree

3 files changed

+74
-9
lines changed

3 files changed

+74
-9
lines changed

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

+41-6
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ public static int collectDefaults() {
178178

179179
public final static SerializableString DEFAULT_ROOT_VALUE_SEPARATOR = DefaultPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR;
180180

181+
/**
182+
* @since 2.10
183+
*/
184+
public final static char DEFAULT_QUOTE_CHAR = '"';
185+
181186
/*
182187
/**********************************************************
183188
/* Buffer, symbol table management
@@ -276,6 +281,13 @@ public static int collectDefaults() {
276281
*/
277282
protected int _maximumNonEscapedChar;
278283

284+
/**
285+
* Character used for quoting field names (if field name quoting has not
286+
* been disabled with {@link JsonWriteFeature#QUOTE_FIELD_NAMES})
287+
* and JSON String values.
288+
*/
289+
protected final char _quoteChar;
290+
279291
/*
280292
/**********************************************************
281293
/* Construction
@@ -294,7 +306,10 @@ public static int collectDefaults() {
294306
*/
295307
public JsonFactory() { this((ObjectCodec) null); }
296308

297-
public JsonFactory(ObjectCodec oc) { _objectCodec = oc; }
309+
public JsonFactory(ObjectCodec oc) {
310+
_objectCodec = oc;
311+
_quoteChar = DEFAULT_QUOTE_CHAR;
312+
}
298313

299314
/**
300315
* Constructor used when copy()ing a factory instance.
@@ -304,14 +319,19 @@ public static int collectDefaults() {
304319
protected JsonFactory(JsonFactory src, ObjectCodec codec)
305320
{
306321
_objectCodec = codec;
322+
323+
// General
307324
_factoryFeatures = src._factoryFeatures;
308325
_parserFeatures = src._parserFeatures;
309326
_generatorFeatures = src._generatorFeatures;
310-
_characterEscapes = src._characterEscapes;
311327
_inputDecorator = src._inputDecorator;
312328
_outputDecorator = src._outputDecorator;
329+
330+
// JSON-specific
331+
_characterEscapes = src._characterEscapes;
313332
_rootValueSeparator = src._rootValueSeparator;
314333
_maximumNonEscapedChar = src._maximumNonEscapedChar;
334+
_quoteChar = src._quoteChar;
315335
}
316336

317337
/**
@@ -320,10 +340,20 @@ protected JsonFactory(JsonFactory src, ObjectCodec codec)
320340
* @since 2.10
321341
*/
322342
public JsonFactory(JsonFactoryBuilder b) {
323-
this(b, false);
343+
_objectCodec = null;
344+
345+
// General
346+
_factoryFeatures = b._factoryFeatures;
347+
_parserFeatures = b._streamReadFeatures;
348+
_generatorFeatures = b._streamWriteFeatures;
349+
_inputDecorator = b._inputDecorator;
350+
_outputDecorator = b._outputDecorator;
351+
352+
// JSON-specific
324353
_characterEscapes = b._characterEscapes;
325354
_rootValueSeparator = b._rootValueSeparator;
326355
_maximumNonEscapedChar = b._maximumNonEscapedChar;
356+
_quoteChar = b._quoteChar;
327357
}
328358

329359
/**
@@ -336,15 +366,20 @@ public JsonFactory(JsonFactoryBuilder b) {
336366
*/
337367
protected JsonFactory(TSFBuilder<?,?> b, boolean bogus) {
338368
_objectCodec = null;
369+
339370
_factoryFeatures = b._factoryFeatures;
340371
_parserFeatures = b._streamReadFeatures;
341372
_generatorFeatures = b._streamWriteFeatures;
342373
_inputDecorator = b._inputDecorator;
343374
_outputDecorator = b._outputDecorator;
344-
// NOTE: missing _maximumNonEscapedChar since that's only in JsonFactoryBuilder
375+
376+
// JSON-specific: need to assign even if not really used
377+
_characterEscapes = null;
378+
_rootValueSeparator = null;
345379
_maximumNonEscapedChar = 0;
380+
_quoteChar = DEFAULT_QUOTE_CHAR;
346381
}
347-
382+
348383
/**
349384
* Method that allows construction of differently configured factory, starting
350385
* with settings of this factory.
@@ -1698,7 +1733,7 @@ protected IOContext _createNonBlockingContext(Object srcRef) {
16981733
return new IOContext(recycler, srcRef, false);
16991734
}
17001735

1701-
/*
1736+
/*
17021737
/**********************************************************
17031738
/* Internal helper methods
17041739
/**********************************************************

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

+32-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
* {@link com.fasterxml.jackson.core.TSFBuilder}
1010
* implementation for constructing vanilla {@link JsonFactory}
1111
* instances for reading/writing JSON encoded content.
12+
*<p>
13+
* NOTE: as of Jackson 2.x, use of JSON-specific builder is bit cumbersome
14+
* since {@link JsonFactory} serves dual duty of base class AND actual
15+
* implementation for JSON backend. This will be fixed in Jackson 3.0.
1216
*
1317
* @since 2.10
1418
*/
@@ -20,6 +24,13 @@ public class JsonFactoryBuilder extends TSFBuilder<JsonFactory, JsonFactoryBuild
2024

2125
protected int _maximumNonEscapedChar;
2226

27+
/**
28+
* Character used for quoting field names (if field name quoting has not
29+
* been disabled with {@link JsonWriteFeature#QUOTE_FIELD_NAMES})
30+
* and JSON String values.
31+
*/
32+
protected char _quoteChar = JsonFactory.DEFAULT_QUOTE_CHAR;
33+
2334
public JsonFactoryBuilder() {
2435
super();
2536
_rootValueSeparator = JsonFactory.DEFAULT_ROOT_VALUE_SEPARATOR;
@@ -160,8 +171,9 @@ public JsonFactoryBuilder rootValueSeparator(SerializableString sep) {
160171
* Default setting is "disabled", specified by passing value of {@code 0} (or
161172
* negative numbers).
162173
*<p>
163-
* NOTE! Lowest value (aside from marker 0) is 127: for ASCII range, other checks apply
164-
* and this threshold is ignored.
174+
* NOTE! Lowest legal value (aside from marker 0) is 127: for ASCII range, other checks apply
175+
* and this threshold is ignored. If value between [1, 126] is specified, 127 will be
176+
* used instead.
165177
*
166178
* @param maxNonEscaped Highest character code that is NOT automatically escaped; if
167179
* positive value above 0, or 0 to indicate that no automatic escaping is applied
@@ -174,13 +186,31 @@ public JsonFactoryBuilder highestNonEscapedChar(int maxNonEscaped) {
174186
return this;
175187
}
176188

189+
/**
190+
* Method that allows specifying an alternate
191+
* character used for quoting field names (if field name quoting has not
192+
* been disabled with {@link JsonWriteFeature#QUOTE_FIELD_NAMES})
193+
* and JSON String values.
194+
*<p>
195+
* Default value is double-quote ({@code "}); typical alternative is
196+
* single-quote/apostrophe ({@code '}).
197+
*
198+
* @param ch Character to use for quoting field names and JSON String values.
199+
*/
200+
public JsonFactoryBuilder quoteChar(char ch) {
201+
_quoteChar = ch;
202+
return this;
203+
}
204+
177205
// // // Accessors for JSON-specific settings
178206

179207
public CharacterEscapes characterEscapes() { return _characterEscapes; }
180208
public SerializableString rootValueSeparator() { return _rootValueSeparator; }
181209

182210
public int highestNonEscapedChar() { return _maximumNonEscapedChar; }
183211

212+
public char quoteChar() { return _quoteChar; }
213+
184214
@Override
185215
public JsonFactory build() {
186216
// 28-Dec-2017, tatu: No special settings beyond base class ones, so:

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class UTF8JsonGenerator
5353
* @since 2.8
5454
*/
5555
protected byte _quoteChar = '"'; // TODO: make configurable
56-
56+
5757
/*
5858
/**********************************************************
5959
/* Output buffering

0 commit comments

Comments
 (0)