Skip to content

Commit

Permalink
REF FasterXML#180 - updates scalars with indicators quoting style
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Jun 3, 2020
1 parent 55e7d08 commit 3f19afe
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private Feature(boolean defaultState) {
* aliases for booleans, and we better quote such values as keys; although Jackson
* itself has no problems dealing with them, some other tools do have.
*/
// 02-Apr-2019, tatu: Some names will look funny if escaped: let's leave out
// 02-Apr-2019, tatu: Some names will look funny if escaped: let's leave out
// single letter case (esp so 'y' won't get escaped)
private final static Set<String> MUST_QUOTE_NAMES = new HashSet<>(Arrays.asList(
// "y", "Y", "n", "N",
Expand Down Expand Up @@ -217,7 +217,7 @@ private Feature(boolean defaultState) {
protected DumperOptions _outputOptions;

protected final org.yaml.snakeyaml.DumperOptions.Version _docVersion;

// for field names, leave out quotes
private final static DumperOptions.ScalarStyle STYLE_UNQUOTED_NAME = DumperOptions.ScalarStyle.PLAIN;

Expand Down Expand Up @@ -964,7 +964,7 @@ private boolean _nameNeedsQuoting(String name) {
return true;
}
return false;
}
}

private boolean _valueNeedsQuoting(String name) {
switch (name.charAt(0)) { // caller ensures no empty String
Expand All @@ -990,19 +990,21 @@ private boolean _valueNeedsQuoting(String name) {
/**
* As per YAML <a href="https://yaml.org/spec/1.2/spec.html#id2788859">Plain Style</a>unquoted
* strings are restricted to a reduced charset and must be quoted in case they contain
* one of the following characters.
* one of the following characters or character combinations.
*/
private static boolean _valueHasQuotableChar(String inputStr) {
for (int i = 0, end = inputStr.length(); i < end; ++i) {
switch (inputStr.charAt(i)) {
case ':':
case '#':
case '[':
case ']':
case '{':
case '}':
case ',':
return true;
case ' ':
return i < end - 1 ? '#' == inputStr.charAt(i + 1) : false;
case ':':
return i < end - 1 ? ' ' == inputStr.charAt(i + 1) : false;
default:
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,53 @@ public void testMinimizeQuotesWithNulls() throws Exception
public void testMinimizeQuotesWithStringsContainingSpecialChars() throws Exception {
Map<String, String> content;

String yaml = null;

/* scenarios with plain scalars */

content = Collections.singletonMap("key", "a:b");
String yaml = MINIM_MAPPER.writeValueAsString(content).trim();
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
assertEquals("---\n" +
"key: a:b", yaml);

content = Collections.singletonMap("key", "a#b");
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
assertEquals("---\n" +
"key: a#b", yaml);

content = Collections.singletonMap("key", "a# b");
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
assertEquals("---\n" +
"key: a# b", yaml);

// plus also some edge cases (wrt "false" etc checking
yaml = MINIM_MAPPER.writeValueAsString(Collections.singletonMap("key", "f:off")).trim();
assertEquals("---\n" +
"key: f:off", yaml);


/* scenarios with single quoted scalars */

content = Collections.singletonMap("key", "::");
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
assertEquals("---\n" +
"key: '::'", yaml);

content = Collections.singletonMap("key", "#");
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
assertEquals("---\n" +
"key: '#'", yaml);

content = Collections.singletonMap("key", "#a");
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
assertEquals("---\n" +
"key: '#a'", yaml);


/* scenarios with double quoted scalars */

content = Collections.singletonMap("key", "a:b");
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
assertEquals("---\n" +
"key: \"a:b\"", yaml);

Expand Down Expand Up @@ -128,10 +173,6 @@ public void testMinimizeQuotesWithStringsContainingSpecialChars() throws Excepti
assertEquals("---\n" +
"key: \"a,b\"", yaml);

// plus also some edge cases (wrt "false" etc checking
yaml = MINIM_MAPPER.writeValueAsString(Collections.singletonMap("key", "f:off")).trim();
assertEquals("---\n" +
"key: \"f:off\"", yaml);
}

public void testLiteralStringsMultiLine() throws Exception
Expand Down Expand Up @@ -182,7 +223,7 @@ public void testNonQuoteNumberStoredAsString() throws Exception
String yaml = MINIM_MAPPER.writeValueAsString(Collections.singletonMap("key", "20")).trim();
assertEquals("---\n" +
"key: 20", yaml);

yaml = MINIM_MAPPER.writeValueAsString(Collections.singletonMap("key", "2.0")).trim();
assertEquals("---\n" +
"key: 2.0", yaml);
Expand Down Expand Up @@ -213,7 +254,7 @@ public void testNumberKey() throws Exception
MINIM_MAPPER.writeValueAsString(stringKeyMap).trim());

// And then true Integer keys

final Map<Integer, String> intKeyMap = Collections.singletonMap(
Integer.valueOf(42), "answer");

Expand Down

0 comments on commit 3f19afe

Please sign in to comment.