diff --git a/src/main/java/com/fasterxml/jackson/core/JsonFactory.java b/src/main/java/com/fasterxml/jackson/core/JsonFactory.java index 8719d93540..3afcf624bb 100644 --- a/src/main/java/com/fasterxml/jackson/core/JsonFactory.java +++ b/src/main/java/com/fasterxml/jackson/core/JsonFactory.java @@ -178,6 +178,11 @@ public static int collectDefaults() { public final static SerializableString DEFAULT_ROOT_VALUE_SEPARATOR = DefaultPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR; + /** + * @since 2.10 + */ + public final static char DEFAULT_QUOTE_CHAR = '"'; + /* /********************************************************** /* Buffer, symbol table management @@ -276,6 +281,13 @@ public static int collectDefaults() { */ protected int _maximumNonEscapedChar; + /** + * Character used for quoting field names (if field name quoting has not + * been disabled with {@link JsonWriteFeature#QUOTE_FIELD_NAMES}) + * and JSON String values. + */ + protected final char _quoteChar; + /* /********************************************************** /* Construction @@ -294,7 +306,10 @@ public static int collectDefaults() { */ public JsonFactory() { this((ObjectCodec) null); } - public JsonFactory(ObjectCodec oc) { _objectCodec = oc; } + public JsonFactory(ObjectCodec oc) { + _objectCodec = oc; + _quoteChar = DEFAULT_QUOTE_CHAR; + } /** * Constructor used when copy()ing a factory instance. @@ -304,14 +319,19 @@ public static int collectDefaults() { protected JsonFactory(JsonFactory src, ObjectCodec codec) { _objectCodec = codec; + + // General _factoryFeatures = src._factoryFeatures; _parserFeatures = src._parserFeatures; _generatorFeatures = src._generatorFeatures; - _characterEscapes = src._characterEscapes; _inputDecorator = src._inputDecorator; _outputDecorator = src._outputDecorator; + + // JSON-specific + _characterEscapes = src._characterEscapes; _rootValueSeparator = src._rootValueSeparator; _maximumNonEscapedChar = src._maximumNonEscapedChar; + _quoteChar = src._quoteChar; } /** @@ -320,10 +340,20 @@ protected JsonFactory(JsonFactory src, ObjectCodec codec) * @since 2.10 */ public JsonFactory(JsonFactoryBuilder b) { - this(b, false); + _objectCodec = null; + + // General + _factoryFeatures = b._factoryFeatures; + _parserFeatures = b._streamReadFeatures; + _generatorFeatures = b._streamWriteFeatures; + _inputDecorator = b._inputDecorator; + _outputDecorator = b._outputDecorator; + + // JSON-specific _characterEscapes = b._characterEscapes; _rootValueSeparator = b._rootValueSeparator; _maximumNonEscapedChar = b._maximumNonEscapedChar; + _quoteChar = b._quoteChar; } /** @@ -336,15 +366,20 @@ public JsonFactory(JsonFactoryBuilder b) { */ protected JsonFactory(TSFBuilder b, boolean bogus) { _objectCodec = null; + _factoryFeatures = b._factoryFeatures; _parserFeatures = b._streamReadFeatures; _generatorFeatures = b._streamWriteFeatures; _inputDecorator = b._inputDecorator; _outputDecorator = b._outputDecorator; - // NOTE: missing _maximumNonEscapedChar since that's only in JsonFactoryBuilder + + // JSON-specific: need to assign even if not really used + _characterEscapes = null; + _rootValueSeparator = null; _maximumNonEscapedChar = 0; + _quoteChar = DEFAULT_QUOTE_CHAR; } - + /** * Method that allows construction of differently configured factory, starting * with settings of this factory. @@ -1698,7 +1733,7 @@ protected IOContext _createNonBlockingContext(Object srcRef) { return new IOContext(recycler, srcRef, false); } -/* + /* /********************************************************** /* Internal helper methods /********************************************************** diff --git a/src/main/java/com/fasterxml/jackson/core/JsonFactoryBuilder.java b/src/main/java/com/fasterxml/jackson/core/JsonFactoryBuilder.java index 76151416c7..39fc691436 100644 --- a/src/main/java/com/fasterxml/jackson/core/JsonFactoryBuilder.java +++ b/src/main/java/com/fasterxml/jackson/core/JsonFactoryBuilder.java @@ -9,6 +9,10 @@ * {@link com.fasterxml.jackson.core.TSFBuilder} * implementation for constructing vanilla {@link JsonFactory} * instances for reading/writing JSON encoded content. + *

+ * NOTE: as of Jackson 2.x, use of JSON-specific builder is bit cumbersome + * since {@link JsonFactory} serves dual duty of base class AND actual + * implementation for JSON backend. This will be fixed in Jackson 3.0. * * @since 2.10 */ @@ -20,6 +24,13 @@ public class JsonFactoryBuilder extends TSFBuilder - * NOTE! Lowest value (aside from marker 0) is 127: for ASCII range, other checks apply - * and this threshold is ignored. + * NOTE! Lowest legal value (aside from marker 0) is 127: for ASCII range, other checks apply + * and this threshold is ignored. If value between [1, 126] is specified, 127 will be + * used instead. * * @param maxNonEscaped Highest character code that is NOT automatically escaped; if * positive value above 0, or 0 to indicate that no automatic escaping is applied @@ -174,6 +186,22 @@ public JsonFactoryBuilder highestNonEscapedChar(int maxNonEscaped) { return this; } + /** + * Method that allows specifying an alternate + * character used for quoting field names (if field name quoting has not + * been disabled with {@link JsonWriteFeature#QUOTE_FIELD_NAMES}) + * and JSON String values. + *

+ * Default value is double-quote ({@code "}); typical alternative is + * single-quote/apostrophe ({@code '}). + * + * @param ch Character to use for quoting field names and JSON String values. + */ + public JsonFactoryBuilder quoteChar(char ch) { + _quoteChar = ch; + return this; + } + // // // Accessors for JSON-specific settings public CharacterEscapes characterEscapes() { return _characterEscapes; } @@ -181,6 +209,8 @@ public JsonFactoryBuilder highestNonEscapedChar(int maxNonEscaped) { public int highestNonEscapedChar() { return _maximumNonEscapedChar; } + public char quoteChar() { return _quoteChar; } + @Override public JsonFactory build() { // 28-Dec-2017, tatu: No special settings beyond base class ones, so: diff --git a/src/main/java/com/fasterxml/jackson/core/json/UTF8JsonGenerator.java b/src/main/java/com/fasterxml/jackson/core/json/UTF8JsonGenerator.java index 34e8736414..3d71a308fe 100644 --- a/src/main/java/com/fasterxml/jackson/core/json/UTF8JsonGenerator.java +++ b/src/main/java/com/fasterxml/jackson/core/json/UTF8JsonGenerator.java @@ -53,7 +53,7 @@ public class UTF8JsonGenerator * @since 2.8 */ protected byte _quoteChar = '"'; // TODO: make configurable - + /* /********************************************************** /* Output buffering