Skip to content

Commit

Permalink
Refine string quote check to more closely match emitter in snakeyaml
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <michael@xlate.io>
  • Loading branch information
MikeEdgar committed Feb 29, 2024
1 parent af35200 commit 38c71dc
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 24 deletions.
74 changes: 55 additions & 19 deletions src/main/java/io/xlate/yamljson/StringQuotingChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ boolean needToQuoteValue(String value) {
}

boolean needToQuote(String value, boolean quoteNumeric) {
return isReservedKeyword(value) ||
return value.isEmpty() ||
isReservedKeyword(value) ||
hasQuoteableCharacter(value) ||
(quoteNumeric && YamlNumbers.isNumeric(value));
}
Expand All @@ -39,7 +40,6 @@ boolean needToQuote(String value, boolean quoteNumeric) {
* <ul>
* <li>YAML 1.2 keyword representing boolean</li>
* <li>YAML 1.2 keyword representing null value</li>
* <li>empty String (length 0)</li></li> and returns {@code true} if so.
*
* @param value
* String to check
Expand All @@ -48,10 +48,6 @@ boolean needToQuote(String value, boolean quoteNumeric) {
* (as per YAML 1.2 specification) or empty String
*/
boolean isReservedKeyword(String value) {
if (value.length() == 0) {
return true;
}

switch (value.charAt(0)) {
// First, reserved name starting chars:
case 'f': // false
Expand All @@ -70,23 +66,23 @@ boolean isReservedKeyword(String value) {
}

/**
* As per YAML <a href="https://yaml.org/spec/1.2/spec.html#id2788859">Plain
* As per YAML <a href="https://yaml.org/spec/1.2.2/#733-plain-style">Plain
* Style</a>unquoted strings are restricted to a reduced charset and must be
* quoted in case they contain one of the following characters or character
* combinations.
*/
boolean hasQuoteableCharacter(String inputStr) {
if (quotableLeadingCharacter(inputStr)) {
return true;
}

final int end = inputStr.length();
for (int i = 0; i < end; ++i) {
switch (inputStr.charAt(i)) {
case '[':
case ']':
case '{':
case '}':
case ',':
return true;

for (int i = 1; i < end; ++i) {
int current = inputStr.charAt(i);
switch (current) {
case '#':
if (precededByBlank(inputStr, i)) {
if (isBlank(inputStr.charAt(i - 1))) {
return true;
}
break;
Expand All @@ -96,17 +92,57 @@ boolean hasQuoteableCharacter(String inputStr) {
}
break;
default:
if (current < 0x20) {
// Control character
return true;
}
break;
}
}
return false;
}

boolean precededByBlank(String inputStr, int offset) {
if (offset == 0) {
boolean quotableLeadingCharacter(String inputStr) {
final int first = inputStr.charAt(0);

switch (first) {
case ' ':
// Leading space
return true;
case '#':
case ',':
case '[':
case ']':
case '{':
case '}':
case '&':
case '*':
case '!':
case '|':
case '>':
case '"':
case '%':
case '@':
case '`':
// Leading indicators
return true;
case '?':
case ':':
case '-':
// Leading indicators not followed by non-space "safe" character
if (followedByBlank(inputStr, 0)) {
return true;
}
break;
default:
if (first < 0x20) {
// Control character
return true;
}
break;
}
return isBlank(inputStr.charAt(offset - 1));

return false;
}

boolean followedByBlank(String inputStr, int offset) {
Expand Down
14 changes: 9 additions & 5 deletions src/test/java/io/xlate/yamljson/YamlGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ void testMappingOfValuesWithQuotesMinimized(String version) {
+ " DoubleQuote: Contains only \"\n"
+ " \"100\": Numeric key\n"
+ " empty: \"\"\n"
+ " blank: ' '\n"
+ " blank: \" \"\n"
+ " positiveInfinity: .inf\n"
+ " negativeInfinity: -.inf\n"
+ " NaN: .nan\n",
Expand Down Expand Up @@ -390,7 +390,7 @@ void testSpecialStringsQuoted(String version) {

try (JsonGenerator generator = createGenerator(config, writer)) {
generator.writeStartObject()
.write("#keywithhash", "value with: colon")
.write("#keywithhash", "value with:\tcolon")
.write("#anotherwithhash", "value with:colon but the :is not followed by a space")
.write("key with spaces", "ends with colon:")
.write("key\twith\ttabs", "ends with hash (preceded by space) #")
Expand All @@ -399,14 +399,16 @@ void testSpecialStringsQuoted(String version) {
.write(".inf", "Key is infinite")
.write(".NAN", "Key is not a number!")
.write("false", "Key is reserved word")
.write("array[]", "Key has indicators")
.write("array[]", "Key has indicators, but not inside of a flow collection")
.write("? question", "Key has leading indicator followed by space")
.write("\ttab first", "Key with leading tab (special character) is quoted")
.writeEnd();

writer.flush();
}

assertEquals(""
+ "\"#keywithhash\": \"value with: colon\"\n"
+ "\"#keywithhash\": \"value with:\\tcolon\"\n"
+ "\"#anotherwithhash\": value with:colon but the :is not followed by a space\n"
+ "key with spaces: \"ends with colon:\"\n"
// snakeyaml* adds quotes due to `\t`
Expand All @@ -416,7 +418,9 @@ void testSpecialStringsQuoted(String version) {
+ "\".inf\": Key is infinite\n"
+ "\".NAN\": Key is not a number!\n"
+ "\"false\": Key is reserved word\n"
+ "\"array[]\": Key has indicators\n",
+ "array[]: Key has indicators, but not inside of a flow collection\n"
+ "\"? question\": Key has leading indicator followed by space\n"
+ "\"\\ttab first\": Key with leading tab (special character) is quoted\n",
writer.toString());
}

Expand Down

0 comments on commit 38c71dc

Please sign in to comment.