Skip to content

Commit

Permalink
Start work on #549
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 12, 2019
1 parent a92daeb commit 2b19c0b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
47 changes: 41 additions & 6 deletions src/main/java/com/fasterxml/jackson/core/JsonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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.
Expand Down Expand Up @@ -1698,7 +1733,7 @@ protected IOContext _createNonBlockingContext(Object srcRef) {
return new IOContext(recycler, srcRef, false);
}

/*
/*
/**********************************************************
/* Internal helper methods
/**********************************************************
Expand Down
34 changes: 32 additions & 2 deletions src/main/java/com/fasterxml/jackson/core/JsonFactoryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
* {@link com.fasterxml.jackson.core.TSFBuilder}
* implementation for constructing vanilla {@link JsonFactory}
* instances for reading/writing JSON encoded content.
*<p>
* 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
*/
Expand All @@ -20,6 +24,13 @@ public class JsonFactoryBuilder extends TSFBuilder<JsonFactory, JsonFactoryBuild

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 char _quoteChar = JsonFactory.DEFAULT_QUOTE_CHAR;

public JsonFactoryBuilder() {
super();
_rootValueSeparator = JsonFactory.DEFAULT_ROOT_VALUE_SEPARATOR;
Expand Down Expand Up @@ -160,8 +171,9 @@ public JsonFactoryBuilder rootValueSeparator(SerializableString sep) {
* Default setting is "disabled", specified by passing value of {@code 0} (or
* negative numbers).
*<p>
* 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
Expand All @@ -174,13 +186,31 @@ 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.
*<p>
* 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; }
public SerializableString rootValueSeparator() { return _rootValueSeparator; }

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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class UTF8JsonGenerator
* @since 2.8
*/
protected byte _quoteChar = '"'; // TODO: make configurable

/*
/**********************************************************
/* Output buffering
Expand Down

0 comments on commit 2b19c0b

Please sign in to comment.