From d617410f904c31a25a6a559ce6ac567fb190ce81 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Mon, 1 Apr 2019 22:24:31 -0700 Subject: [PATCH] Minor tweak wrt #68, leave out single-letter names --- .../dataformat/yaml/YAMLGenerator.java | 5 ++- .../yaml/misc/ReservedNamesTest.java | 8 ++-- .../yaml/ser/DatabindWriteTest.java | 41 +++++++++++++++---- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java b/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java index ae5dc433..120b2aaf 100644 --- a/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java +++ b/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java @@ -175,8 +175,11 @@ 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 + // single letter case (esp so 'y' won't get escaped) private final static Set RESERVED_NAMES = new HashSet<>(Arrays.asList( - "y", "Y", "yes", "Yes", "YES", "n", "N", "no", "No", "NO", +// "y", "Y", "n", "N", + "yes", "Yes", "YES", "no", "No", "NO", "true", "True", "TRUE", "false", "False", "FALSE", "on", "On", "ON", "off", "Off", "OFF" )); diff --git a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/misc/ReservedNamesTest.java b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/misc/ReservedNamesTest.java index 7076507c..9e79646a 100644 --- a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/misc/ReservedNamesTest.java +++ b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/misc/ReservedNamesTest.java @@ -15,10 +15,10 @@ public void testQuotingOfBooleanKeys() throws Exception for (String value : new String[] { "true", "True", "false", "False", - "yes", "y", "Y", - "no", "n", "N", - "on", - "off", + "yes", "no", + // NOTE: single-letter cases left out on purpose +// "y", "Y", "n", "N", + "on", "off", }) { _testQuotingOfBooleanKeys(value); } diff --git a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/DatabindWriteTest.java b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/DatabindWriteTest.java index b53b1bfe..37c9bfea 100644 --- a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/DatabindWriteTest.java +++ b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/DatabindWriteTest.java @@ -9,18 +9,31 @@ import java.util.Map; import java.util.TreeSet; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase; public class DatabindWriteTest extends ModuleTestBase { + final ObjectMapper MAPPER = newObjectMapper(); + + @JsonPropertyOrder(alphabetic = true) + static class Point { + public int x, y; + + protected Point() { } + public Point(int x, int y) { + this.x = x; + this.y = y; + } + } + public void testBasicPOJO() throws Exception { - ObjectMapper mapper = newObjectMapper(); FiveMinuteUser user = new FiveMinuteUser("Bob", "Dabolito", false, FiveMinuteUser.Gender.MALE, new byte[] { 1, 3, 13, 79 }); - String yaml = mapper.writeValueAsString(user).trim(); + String yaml = MAPPER.writeValueAsString(user).trim(); String[] parts = yaml.split("\n"); // unify ordering, need to use TreeSets to get alphabetic ordering TreeSet exp = new TreeSet(); @@ -45,14 +58,27 @@ public void testBasicPOJO() throws Exception assertFalse(it.hasNext()); } + // Related to [dataformats-test#68], escaping of "reserved" names + public void testBasicDatabind2() throws Exception + { + String yaml = trimDocMarker(MAPPER.writeValueAsString(new Point(1, 2))); + + // Just verify 'y' will NOT be escaped + assertEquals("x: 1\ny: 2", yaml); + + // Actually let's try reading back, too + Point p = MAPPER.readValue(yaml, Point.class); + assertEquals(1, p.x); + assertEquals(2, p.y); + } + public void testWithFile() throws Exception { File f = File.createTempFile("test", ".yml"); f.deleteOnExit(); - ObjectMapper mapper = newObjectMapper(); Map map = new HashMap(); map.put("a", 3); - mapper.writeValue(f, map); + MAPPER.writeValue(f, map); assertTrue(f.canRead()); BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream( f), "UTF-8")); @@ -71,13 +97,12 @@ public void testWithFile2() throws Exception { File f = File.createTempFile("test", ".yml"); f.deleteOnExit(); - ObjectMapper mapper = newObjectMapper(); - ObjectNode root = mapper.createObjectNode(); + ObjectNode root = MAPPER.createObjectNode(); root.put("name", "Foobar"); - mapper.writeValue(f, root); + MAPPER.writeValue(f, root); // and get it back - Map result = mapper.readValue(f, Map.class); + Map result = MAPPER.readValue(f, Map.class); assertEquals(1, result.size()); assertEquals("Foobar", result.get("name")); }