Skip to content

Commit

Permalink
Minor tweak wrt #68, leave out single-letter names
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Apr 2, 2019
1 parent 3767598 commit d617410
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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"
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> exp = new TreeSet<String>();
Expand All @@ -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<String,Integer> map = new HashMap<String,Integer>();
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"));
Expand All @@ -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"));
}
Expand Down

0 comments on commit d617410

Please sign in to comment.