Skip to content

Commit d04202d

Browse files
committed
REF FasterXML#180 - updates scalars with indicators quoting style
1 parent 55e7d08 commit d04202d

File tree

2 files changed

+55
-15
lines changed

2 files changed

+55
-15
lines changed

Diff for: yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java

+15-6
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ private Feature(boolean defaultState) {
175175
* aliases for booleans, and we better quote such values as keys; although Jackson
176176
* itself has no problems dealing with them, some other tools do have.
177177
*/
178-
// 02-Apr-2019, tatu: Some names will look funny if escaped: let's leave out
178+
// 02-Apr-2019, tatu: Some names will look funny if escaped: let's leave out
179179
// single letter case (esp so 'y' won't get escaped)
180180
private final static Set<String> MUST_QUOTE_NAMES = new HashSet<>(Arrays.asList(
181181
// "y", "Y", "n", "N",
@@ -217,7 +217,7 @@ private Feature(boolean defaultState) {
217217
protected DumperOptions _outputOptions;
218218

219219
protected final org.yaml.snakeyaml.DumperOptions.Version _docVersion;
220-
220+
221221
// for field names, leave out quotes
222222
private final static DumperOptions.ScalarStyle STYLE_UNQUOTED_NAME = DumperOptions.ScalarStyle.PLAIN;
223223

@@ -964,7 +964,7 @@ private boolean _nameNeedsQuoting(String name) {
964964
return true;
965965
}
966966
return false;
967-
}
967+
}
968968

969969
private boolean _valueNeedsQuoting(String name) {
970970
switch (name.charAt(0)) { // caller ensures no empty String
@@ -990,19 +990,28 @@ private boolean _valueNeedsQuoting(String name) {
990990
/**
991991
* As per YAML <a href="https://yaml.org/spec/1.2/spec.html#id2788859">Plain Style</a>unquoted
992992
* strings are restricted to a reduced charset and must be quoted in case they contain
993-
* one of the following characters.
993+
* one of the following characters or character combinations.
994994
*/
995995
private static boolean _valueHasQuotableChar(String inputStr) {
996996
for (int i = 0, end = inputStr.length(); i < end; ++i) {
997997
switch (inputStr.charAt(i)) {
998-
case ':':
999-
case '#':
1000998
case '[':
1001999
case ']':
10021000
case '{':
10031001
case '}':
10041002
case ',':
10051003
return true;
1004+
case '\t':
1005+
case ' ':
1006+
if (i < end - 1 && '#' == inputStr.charAt(i + 1)) {
1007+
return true;
1008+
}
1009+
break;
1010+
case ':':
1011+
if (i < end - 1 && (' ' == inputStr.charAt(i + 1) || '\t' == inputStr.charAt(i + 1))) {
1012+
return true;
1013+
}
1014+
break;
10061015
default:
10071016
}
10081017
}

Diff for: yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorWithMinimizeTest.java

+40-9
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,50 @@ public void testMinimizeQuotesWithNulls() throws Exception
9494
public void testMinimizeQuotesWithStringsContainingSpecialChars() throws Exception {
9595
Map<String, String> content;
9696

97+
String yaml = null;
98+
99+
/* scenarios with plain scalars */
100+
97101
content = Collections.singletonMap("key", "a:b");
98-
String yaml = MINIM_MAPPER.writeValueAsString(content).trim();
102+
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
99103
assertEquals("---\n" +
100-
"key: \"a:b\"", yaml);
104+
"key: a:b", yaml);
101105

102106
content = Collections.singletonMap("key", "a#b");
103107
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
104108
assertEquals("---\n" +
105-
"key: \"a#b\"", yaml);
109+
"key: a#b", yaml);
110+
111+
content = Collections.singletonMap("key", "a# b");
112+
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
113+
assertEquals("---\n" +
114+
"key: a# b", yaml);
115+
116+
// plus also some edge cases (wrt "false" etc checking
117+
yaml = MINIM_MAPPER.writeValueAsString(Collections.singletonMap("key", "f:off")).trim();
118+
assertEquals("---\n" +
119+
"key: f:off", yaml);
120+
121+
122+
/* scenarios with single quoted scalars */
123+
124+
content = Collections.singletonMap("key", "::");
125+
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
126+
assertEquals("---\n" +
127+
"key: '::'", yaml);
128+
129+
content = Collections.singletonMap("key", "#");
130+
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
131+
assertEquals("---\n" +
132+
"key: '#'", yaml);
133+
134+
content = Collections.singletonMap("key", "#a");
135+
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
136+
assertEquals("---\n" +
137+
"key: '#a'", yaml);
138+
139+
140+
/* scenarios with double quoted scalars */
106141

107142
content = Collections.singletonMap("key", "a[b");
108143
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
@@ -128,10 +163,6 @@ public void testMinimizeQuotesWithStringsContainingSpecialChars() throws Excepti
128163
assertEquals("---\n" +
129164
"key: \"a,b\"", yaml);
130165

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

137168
public void testLiteralStringsMultiLine() throws Exception
@@ -182,7 +213,7 @@ public void testNonQuoteNumberStoredAsString() throws Exception
182213
String yaml = MINIM_MAPPER.writeValueAsString(Collections.singletonMap("key", "20")).trim();
183214
assertEquals("---\n" +
184215
"key: 20", yaml);
185-
216+
186217
yaml = MINIM_MAPPER.writeValueAsString(Collections.singletonMap("key", "2.0")).trim();
187218
assertEquals("---\n" +
188219
"key: 2.0", yaml);
@@ -213,7 +244,7 @@ public void testNumberKey() throws Exception
213244
MINIM_MAPPER.writeValueAsString(stringKeyMap).trim());
214245

215246
// And then true Integer keys
216-
247+
217248
final Map<Integer, String> intKeyMap = Collections.singletonMap(
218249
Integer.valueOf(42), "answer");
219250

0 commit comments

Comments
 (0)