Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JsonGenerator.Feature to use single quotation marks instead of double quotes #235

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/JsonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ public enum Feature {
* @since 2.5
*/
IGNORE_UNKNOWN(false),

/**
* Features that determines whether generator will write strings with single quotes
* (apostrophe, character '\'') instead of double quotes.
* <p>
* Since JSON specification requires use of double quotes for field names, this is a
* non-standard feature, and as such is disabled by default.
*/
WRITE_SINGLE_QUOTES(false),
;

private final boolean _defaultState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ public JsonGenerator enable(Feature f) {
if (_writeContext.getDupDetector() == null) { // but only if disabled currently
_writeContext = _writeContext.withDupDetector(DupDetector.rootDetector(this));
}
} else if (f == Feature.WRITE_SINGLE_QUOTES) {
setCharacterEscapes(getCharacterEscapes());
}
}
return this;
Expand All @@ -166,6 +168,8 @@ public JsonGenerator disable(Feature f) {
setHighestNonEscapedChar(0);
} else if (f == Feature.STRICT_DUPLICATE_DETECTION) {
_writeContext = _writeContext.withDupDetector(null);
} else if (f == Feature.WRITE_SINGLE_QUOTES) {
setCharacterEscapes(getCharacterEscapes());
}
}
return this;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/io/CharTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public final class CharTypes
* 7-bit ASCII range need to be quoted.
*/
final static int[] sOutputEscapes128;
final static int[] sOutputEscapes128SingleQuoted;
static {
int[] table = new int[128];
// Control chars need generic escape sequence
Expand All @@ -173,6 +174,9 @@ public final class CharTypes
table[0x0A] = 'n';
table[0x0D] = 'r';
sOutputEscapes128 = table;
sOutputEscapes128SingleQuoted = Arrays.copyOf(table, 128);
sOutputEscapes128SingleQuoted['"'] = 0;
sOutputEscapes128SingleQuoted['\''] = '\'';
}

/**
Expand Down Expand Up @@ -210,6 +214,15 @@ public final class CharTypes
*/
public static int[] get7BitOutputEscapes() { return sOutputEscapes128; }

/**
* Accessor for getting a read-only encoding table for first 128 Unicode
* code points (single-byte UTF-8 characters) in single quoted strings.
* Value of 0 means "no escaping"; other positive values that value is character
* to use after backslash; and negative values that generic (backslash - u)
* escpaing is to be used.
*/
public static int[] get7BitOutputEscapesSingleQuoted() { return sOutputEscapes128SingleQuoted; }

public static int charToHex(int ch)
{
return (ch > 127) ? -1 : sHexValues[ch];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public abstract class JsonGeneratorImpl extends GeneratorBase
* (first 128 character codes), used for single-byte UTF-8 characters.
*/
protected final static int[] sOutputEscapes = CharTypes.get7BitOutputEscapes();

protected final static int[] sOutputEscapesSingleQuoted
= CharTypes.get7BitOutputEscapesSingleQuoted();

/*
/**********************************************************
/* Configuration, basic I/O
Expand All @@ -50,7 +52,7 @@ public abstract class JsonGeneratorImpl extends GeneratorBase
* character codes). Defined separately to make potentially
* customizable
*/
protected int[] _outputEscapes = sOutputEscapes;
protected int[] _outputEscapes;

/**
* Value between 128 (0x80) and 65535 (0xFFFF) that indicates highest
Expand Down Expand Up @@ -97,6 +99,11 @@ public JsonGeneratorImpl(IOContext ctxt, int features, ObjectCodec codec)
if (isEnabled(Feature.ESCAPE_NON_ASCII)) {
setHighestNonEscapedChar(127);
}
if (isEnabled(Feature.WRITE_SINGLE_QUOTES)) {
_outputEscapes = sOutputEscapesSingleQuoted;
} else {
_outputEscapes = sOutputEscapes;
}
}

/*
Expand All @@ -121,7 +128,11 @@ public JsonGenerator setCharacterEscapes(CharacterEscapes esc)
{
_characterEscapes = esc;
if (esc == null) { // revert to standard escapes
_outputEscapes = sOutputEscapes;
if (isEnabled(Feature.WRITE_SINGLE_QUOTES)) {
_outputEscapes = sOutputEscapesSingleQuoted;
} else {
_outputEscapes = sOutputEscapes;
}
} else {
_outputEscapes = esc.getEscapeCodesForAscii();
}
Expand Down
Loading