Skip to content

Commit

Permalink
fix FasterXML#116 "null" Strings when Feature.MINIMIZE_QUOTES is active
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan stefanleh@users.noreply.github.com
  • Loading branch information
Stefan Lehmann committed Jan 25, 2019
1 parent 00c3d1d commit 6106dc6
Showing 1 changed file with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@

public class YAMLGenerator extends GeneratorBase
{
/**
* Static string values which should not be instantiated every time.
*/
private final static String TRUE_VALUE = "true";
private final static String FALSE_VALUE = "false";
private final static String NULL_UP_VALUE = "NULL";
private final static String NULL_LW_VALUE = "null";

/**
* Enumeration that defines all togglable features for YAML generators
*/
Expand Down Expand Up @@ -523,8 +531,12 @@ public void writeString(String text) throws IOException,JsonGenerationException
}
_verifyValueWrite("write String value");
DumperOptions.ScalarStyle style = STYLE_QUOTED;
if (Feature.MINIMIZE_QUOTES.enabledIn(_formatWriteFeatures) && !isBooleanContent(text)) {
// If this string could be interpreted as a number, it must be quoted.

if (Feature.MINIMIZE_QUOTES.enabledIn(_formatWriteFeatures)
&& !isBooleanContent(text)
&& !isStringNullContent(text)) {

// If this string could be interpreted as a number, it must be quoted.
if (Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS.enabledIn(_formatWriteFeatures)
&& PLAIN_NUMBER_P.matcher(text).matches()) {
style = STYLE_QUOTED;
Expand All @@ -533,14 +545,19 @@ public void writeString(String text) throws IOException,JsonGenerationException
} else {
style = STYLE_PLAIN;
}

} else if (Feature.LITERAL_BLOCK_STYLE.enabledIn(_formatWriteFeatures) && text.indexOf('\n') >= 0) {
style = STYLE_LITERAL;
}
_writeScalar(text, "string", style);
}

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

private boolean isStringNullContent(String text) {
return NULL_LW_VALUE.equals(text) || NULL_UP_VALUE.equals(text);
}

@Override
Expand Down

0 comments on commit 6106dc6

Please sign in to comment.