Skip to content

Commit

Permalink
Add quotes for all values listed in https://yaml.org/type/bool.htmlA
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeSmaha committed Jun 28, 2019
1 parent 7092c65 commit e6817b9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,10 @@ private Feature(boolean defaultState) {
* better retain quoting for some values
*/
private final static Set<String> MUST_QUOTE_VALUES = new HashSet<>(Arrays.asList(
"y", "Y", "n", "N",
"yes", "Yes", "YES", "no", "No", "NO",
"true", "True", "TRUE", "false", "False", "FALSE",
"null", "Null", "NULL"
"on", "On", "ON", "off", "Off", "OFF"
));

/*
Expand Down Expand Up @@ -955,11 +957,15 @@ private boolean _valueNeedsQuoting(String name) {
switch (name.charAt(0)) { // caller ensures no empty String
// First, reserved name starting chars:
case 'f': // false
case 'n': // null
case 'o': // on/off
case 'n': // no
case 't': // true
case 'F': // False/FALSE
case 'N': // Null/NULL
case 'T': // True/TRUE
case 'y': // yes
case 'F': // False
case 'O': // On/Off
case 'N': // No
case 'T': // True
case 'Y': // Yes
return MUST_QUOTE_VALUES.contains(name);
}
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.fasterxml.jackson.dataformat.yaml.misc;

import java.util.*;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase;

public class ReservedValuesTest extends ModuleTestBase
{
private final ObjectMapper MAPPER = newObjectMapper();

public void testQuotingOfBooleanValues() throws Exception
{
for (String value : new String[] {
"true", "True",
"false", "False",
"yes", "no",
"y", "Y", "n", "N",
"on", "off",
}) {
_testQuotingOfBooleanValues(value);
}
}

private void _testQuotingOfBooleanValues(String key) throws Exception
{
final Map<String, Integer> input = Collections.singletonMap("key", key);
final String doc = trimDocMarker(MAPPER.writeValueAsString(input).trim());

assertEquals("key: \""+key+"\"", doc);
}
}

0 comments on commit e6817b9

Please sign in to comment.