Skip to content

Commit

Permalink
special characters shouldn't force double quoting for multi-line strings
Browse files Browse the repository at this point in the history
the logic added in c427465 for FasterXML#116 to force double-quoting of strings should only apply to single-line strings.
the quoting of multi-line strings look naff, and because they take literal mode, the characters aren't special and the quotes are not necessary.
  • Loading branch information
ahgittin committed Mar 5, 2021
1 parent 7a6f899 commit 493e674
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -591,13 +591,13 @@ public void writeString(String text) throws IOException,JsonGenerationException
DumperOptions.ScalarStyle style;
if (Feature.MINIMIZE_QUOTES.enabledIn(_formatFeatures)) {
// If one of reserved values ("true", "null"), or, number, preserve quoting:
if (_quotingChecker.needToQuoteValue(text)
if (text.indexOf('\n') >= 0) {
style = STYLE_LITERAL;
} else if (_quotingChecker.needToQuoteValue(text)
|| (Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS.enabledIn(_formatFeatures)
&& PLAIN_NUMBER_P.matcher(text).matches())
) {
style = STYLE_QUOTED;
} else if (text.indexOf('\n') >= 0) {
style = STYLE_LITERAL;
} else {
style = STYLE_PLAIN;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ public void testLiteralStringsMultiLine() throws Exception
"key: |-\n first\n second\n third", yaml);
}

public void testMinimizeQuotesWithStringsContainingSpecialCharsMultiLine() throws Exception
{
Map<String, Object> content = new HashMap<String, Object>();
content.put("key", "first\nsecond: third");
String yaml = MINIM_MAPPER.writeValueAsString(content).trim();

assertEquals("---\n" +
"key: |-\n first\n second: third", yaml);
}

public void testQuoteNumberStoredAsString() throws Exception
{
YAMLFactory f = new YAMLFactory();
Expand Down

0 comments on commit 493e674

Please sign in to comment.