Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #78 from rhuss/strip-boolean-fix
Browse files Browse the repository at this point in the history
Never unquote boolean-like string content
  • Loading branch information
cowtowncoder authored Oct 13, 2016
2 parents 29702cc + ce84597 commit b44ed81
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public enum Feature // implements FormatFeature // for 2.7
/**
* Whether we are to write an explicit document start marker ("---")
* or not.
*
*
* @since 2.3
*/
WRITE_DOC_START_MARKER(true),
Expand All @@ -38,17 +38,17 @@ public enum Feature // implements FormatFeature // for 2.7
* or "generic" Object Id mechanism (false). Former works better for systems that
* are YAML-centric; latter may be better choice for interoperability, when
* converting between formats or accepting other formats.
*
*
* @since 2.5
*/
USE_NATIVE_OBJECT_ID(true),

/**
* Whether to use YAML native Type Id construct for indicating type (true);
* or "generic" type property (false). Former works better for systems that
* are YAML-centric; latter may be better choice for interoperability, when
* converting between formats or accepting other formats.
*
*
* @since 2.5
*/
USE_NATIVE_TYPE_ID(true),
Expand Down Expand Up @@ -80,7 +80,7 @@ public enum Feature // implements FormatFeature // for 2.7
* @since 2.7
*/
MINIMIZE_QUOTES(false),

/**
* Whether numbers stored as strings will be rendered with quotes (true) or
* without quotes (false, default) when MINIMIZE_QUOTES is enabled.
Expand All @@ -96,7 +96,7 @@ public enum Feature // implements FormatFeature // for 2.7

protected final boolean _defaultState;
protected final int _mask;

/**
* Method that calculates bit set (flags) of all features that
* are enabled by default.
Expand All @@ -111,14 +111,14 @@ public static int collectDefaults()
}
return flags;
}

private Feature(boolean defaultState) {
_defaultState = defaultState;
_mask = (1 << ordinal());
}

public boolean enabledByDefault() { return _defaultState; }
public boolean enabledIn(int flags) { return (flags & _mask) != 0; }
public boolean enabledIn(int flags) { return (flags & _mask) != 0; }
public int getMask() { return _mask; }
}

Expand All @@ -127,7 +127,7 @@ private Feature(boolean defaultState) {
/* Internal constants
/**********************************************************
*/

protected final static long MIN_INT_AS_LONG = (long) Integer.MIN_VALUE;
protected final static long MAX_INT_AS_LONG = (long) Integer.MAX_VALUE;
protected final static Pattern PLAIN_NUMBER_P = Pattern.compile("[0-9]*(\\.[0-9]*)?");
Expand All @@ -153,7 +153,7 @@ private Feature(boolean defaultState) {

// for field names, leave out quotes
private final static Character STYLE_NAME = null;

// numbers, booleans, should use implicit
private final static Character STYLE_SCALAR = null;
// Strings quoted for fun
Expand Down Expand Up @@ -185,7 +185,7 @@ private Feature(boolean defaultState) {
* need to output one.
*/
protected String _typeId;

/*
/**********************************************************
/* Life-cycle
Expand All @@ -203,14 +203,14 @@ public YAMLGenerator(IOContext ctxt, int jsonFeatures, int yamlFeatures,
_writer = out;

_outputOptions = buildDumperOptions(jsonFeatures, yamlFeatures, version);

_emitter = new Emitter(_writer, _outputOptions);
// should we start output now, or try to defer?
_emitter.emit(new StreamStartEvent(null, null));
Map<String,String> noTags = Collections.emptyMap();

boolean startMarker = Feature.WRITE_DOC_START_MARKER.enabledIn(yamlFeatures);

_emitter.emit(new DocumentStartEvent(null, null, startMarker,
version, // for 1.10 was: ((version == null) ? null : version.getArray()),
noTags));
Expand All @@ -232,10 +232,10 @@ protected DumperOptions buildDumperOptions(int jsonFeatures, int yamlFeatures, o
return opt;
}

/*
/**********************************************************
/* Versioned
/**********************************************************
/*
/**********************************************************
/* Versioned
/**********************************************************
*/

@Override
Expand Down Expand Up @@ -291,7 +291,7 @@ public JsonGenerator overrideFormatFeatures(int values, int mask) {
_formatFeatures = (_formatFeatures & ~mask) | (values & mask);
return this;
}

@Override
public boolean canUseSchema(FormatSchema schema) {
return false;
Expand Down Expand Up @@ -330,13 +330,13 @@ public YAMLGenerator configure(Feature f, boolean state) {
}
return this;
}

/*
/**********************************************************************
/* Overridden methods; writing field names
/**********************************************************************
*/

/* And then methods overridden to make final, streamline some
* aspects...
*/
Expand Down Expand Up @@ -389,7 +389,7 @@ public final void flush() throws IOException
{
_writer.flush();
}

@Override
public void close() throws IOException
{
Expand All @@ -406,7 +406,7 @@ public void close() throws IOException
/* Public API: structural output
/**********************************************************
*/

@Override
public final void writeStartArray() throws IOException
{
Expand All @@ -422,15 +422,15 @@ public final void writeStartArray() throws IOException
_emitter.emit(new SequenceStartEvent(anchor, yamlTag,
implicit, null, null, style));
}

@Override
public final void writeEndArray() throws IOException
{
if (!_writeContext.inArray()) {
_reportError("Current context not Array but "+_writeContext.typeDesc());
}
// just to make sure we don't "leak" type ids
_typeId = null;
_typeId = null;
_writeContext = _writeContext.getParent();
_emitter.emit(new SequenceEndEvent(null, null));
}
Expand Down Expand Up @@ -458,7 +458,7 @@ public final void writeEndObject() throws IOException
_reportError("Current context not Object but "+_writeContext.typeDesc());
}
// just to make sure we don't "leak" type ids
_typeId = null;
_typeId = null;
_writeContext = _writeContext.getParent();
_emitter.emit(new MappingEndEvent(null, null));
}
Expand All @@ -478,7 +478,7 @@ public void writeString(String text) throws IOException,JsonGenerationException
}
_verifyValueWrite("write String value");
Character style = STYLE_QUOTED;
if (Feature.MINIMIZE_QUOTES.enabledIn(_formatFeatures)) {
if (Feature.MINIMIZE_QUOTES.enabledIn(_formatFeatures) && !isBooleanContent(text)) {
// If this string could be interpreted as a number, it must be quoted.
if (Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS.enabledIn(_formatFeatures)
&& PLAIN_NUMBER_P.matcher(text).matches()) {
Expand All @@ -492,6 +492,10 @@ public void writeString(String text) throws IOException,JsonGenerationException
_writeScalar(text, "string", style);
}

private boolean isBooleanContent(String text) {
return text.equals("true") || text.equals("false");
}

@Override
public void writeString(char[] text, int offset, int len) throws IOException
{
Expand Down Expand Up @@ -565,7 +569,7 @@ public void writeRawValue(char[] text, int offset, int len) throws IOException {
/* Output method implementations, base64-encoded binary
/**********************************************************
*/

@Override
public void writeBinary(Base64Variant b64variant, byte[] data, int offset, int len) throws IOException
{
Expand Down Expand Up @@ -624,13 +628,13 @@ public void writeNumber(BigInteger v) throws IOException
_verifyValueWrite("write number");
_writeScalar(String.valueOf(v.toString()), "java.math.BigInteger", STYLE_SCALAR);
}

@Override
public void writeNumber(double d) throws IOException
{
_verifyValueWrite("write number");
_writeScalar(String.valueOf(d), "double", STYLE_SCALAR);
}
}

@Override
public void writeNumber(float f) throws IOException
Expand Down Expand Up @@ -681,14 +685,14 @@ public boolean canWriteObjectId() {
// yes, YAML does support Native Type Ids!
// 10-Sep-2014, tatu: Except as per [#23] might not want to...
return Feature.USE_NATIVE_OBJECT_ID.enabledIn(_formatFeatures);
}
}

@Override
public boolean canWriteTypeId() {
// yes, YAML does support Native Type Ids!
// 10-Sep-2014, tatu: Except as per [#22] might not want to...
return Feature.USE_NATIVE_TYPE_ID.enabledIn(_formatFeatures);
}
}

@Override
public void writeTypeId(Object id)
Expand All @@ -706,7 +710,7 @@ public void writeObjectRef(Object id)
AliasEvent evt = new AliasEvent(String.valueOf(id), null, null);
_emitter.emit(evt);
}

@Override
public void writeObjectId(Object id)
throws IOException
Expand Down Expand Up @@ -749,7 +753,7 @@ protected void _writeScalar(String value, String type, Character style) throws I
{
_emitter.emit(_scalarEvent(value, style));
}

protected ScalarEvent _scalarEvent(String value, Character style)
{
String yamlTag = _typeId;
Expand Down
Loading

0 comments on commit b44ed81

Please sign in to comment.