Skip to content

Commit d617410

Browse files
committed
Minor tweak wrt #68, leave out single-letter names
1 parent 3767598 commit d617410

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,11 @@ 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
179+
// single letter case (esp so 'y' won't get escaped)
178180
private final static Set<String> RESERVED_NAMES = new HashSet<>(Arrays.asList(
179-
"y", "Y", "yes", "Yes", "YES", "n", "N", "no", "No", "NO",
181+
// "y", "Y", "n", "N",
182+
"yes", "Yes", "YES", "no", "No", "NO",
180183
"true", "True", "TRUE", "false", "False", "FALSE",
181184
"on", "On", "ON", "off", "Off", "OFF"
182185
));

yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/misc/ReservedNamesTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public void testQuotingOfBooleanKeys() throws Exception
1515
for (String value : new String[] {
1616
"true", "True",
1717
"false", "False",
18-
"yes", "y", "Y",
19-
"no", "n", "N",
20-
"on",
21-
"off",
18+
"yes", "no",
19+
// NOTE: single-letter cases left out on purpose
20+
// "y", "Y", "n", "N",
21+
"on", "off",
2222
}) {
2323
_testQuotingOfBooleanKeys(value);
2424
}

yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/DatabindWriteTest.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,31 @@
99
import java.util.Map;
1010
import java.util.TreeSet;
1111

12+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
1213
import com.fasterxml.jackson.databind.ObjectMapper;
1314
import com.fasterxml.jackson.databind.node.ObjectNode;
1415
import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase;
1516

1617
public class DatabindWriteTest extends ModuleTestBase
1718
{
19+
final ObjectMapper MAPPER = newObjectMapper();
20+
21+
@JsonPropertyOrder(alphabetic = true)
22+
static class Point {
23+
public int x, y;
24+
25+
protected Point() { }
26+
public Point(int x, int y) {
27+
this.x = x;
28+
this.y = y;
29+
}
30+
}
31+
1832
public void testBasicPOJO() throws Exception
1933
{
20-
ObjectMapper mapper = newObjectMapper();
2134
FiveMinuteUser user = new FiveMinuteUser("Bob", "Dabolito", false,
2235
FiveMinuteUser.Gender.MALE, new byte[] { 1, 3, 13, 79 });
23-
String yaml = mapper.writeValueAsString(user).trim();
36+
String yaml = MAPPER.writeValueAsString(user).trim();
2437
String[] parts = yaml.split("\n");
2538
// unify ordering, need to use TreeSets to get alphabetic ordering
2639
TreeSet<String> exp = new TreeSet<String>();
@@ -45,14 +58,27 @@ public void testBasicPOJO() throws Exception
4558
assertFalse(it.hasNext());
4659
}
4760

61+
// Related to [dataformats-test#68], escaping of "reserved" names
62+
public void testBasicDatabind2() throws Exception
63+
{
64+
String yaml = trimDocMarker(MAPPER.writeValueAsString(new Point(1, 2)));
65+
66+
// Just verify 'y' will NOT be escaped
67+
assertEquals("x: 1\ny: 2", yaml);
68+
69+
// Actually let's try reading back, too
70+
Point p = MAPPER.readValue(yaml, Point.class);
71+
assertEquals(1, p.x);
72+
assertEquals(2, p.y);
73+
}
74+
4875
public void testWithFile() throws Exception
4976
{
5077
File f = File.createTempFile("test", ".yml");
5178
f.deleteOnExit();
52-
ObjectMapper mapper = newObjectMapper();
5379
Map<String,Integer> map = new HashMap<String,Integer>();
5480
map.put("a", 3);
55-
mapper.writeValue(f, map);
81+
MAPPER.writeValue(f, map);
5682
assertTrue(f.canRead());
5783
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(
5884
f), "UTF-8"));
@@ -71,13 +97,12 @@ public void testWithFile2() throws Exception
7197
{
7298
File f = File.createTempFile("test", ".yml");
7399
f.deleteOnExit();
74-
ObjectMapper mapper = newObjectMapper();
75-
ObjectNode root = mapper.createObjectNode();
100+
ObjectNode root = MAPPER.createObjectNode();
76101
root.put("name", "Foobar");
77-
mapper.writeValue(f, root);
102+
MAPPER.writeValue(f, root);
78103

79104
// and get it back
80-
Map<?,?> result = mapper.readValue(f, Map.class);
105+
Map<?,?> result = MAPPER.readValue(f, Map.class);
81106
assertEquals(1, result.size());
82107
assertEquals("Foobar", result.get("name"));
83108
}

0 commit comments

Comments
 (0)