Conversation
Not my finest work, fixes: SingleQuotedStringPropertyNamesTest DoubleQuotedStringPropertyNamesTest
There was a problem hiding this comment.
DoubleQuotedStringPropertyNamesTest ensures that double quotes are wrapped around property names that contain single quotes.
In JavaScript JSON5.stringify({'a\'b':1}) should result in {"a'b":1} rather than {'a\'b':1}. The goal is to output the minimum number of escapes possible.
Compare the following statements that stringify strings.
JSON5.stringify(`Jane said, "Don't lie to me!"`)
// 'Jane said, "Don\'t lie to me!"'
JSON5.stringify(`"Bob's wife is Alice's sister's mother," said Joe.`)
// "\"Bob's wife is Alice's sister's mother,\" said Joe."So, if there are more single quotes than double quotes in a string or property name, then double quotes are used, otherwise, single quotes are used.
| public void SingleQuotedStringPropertyNamesTest() | ||
| { | ||
| var s = Json5.Stringify(new Json5Object { { "a-b", 1 } }); | ||
| var s = Json5.Stringify(new Json5Object { { "'a-b'", 1 } }); |
There was a problem hiding this comment.
The point of SingleQuotedStringPropertyNamesTest is to ensure that Stringify puts single quotes around property names that are not ES5 Identifiers.
In the original test, the property name was a-b, but your change makes it 'a-b'—a property name that contains single quotes.
If you ran the original test against your hack, it would fail because {'a-b':1} is not equal to {a-b:1}. Your hack actually outputs invalid JSON5.
I assume the modified test case is now better match to https://github.com/json5/json5/blob/master/test/stringify.js since C# doesn't understand single quoted strings
Not my finest work, fixes:
SingleQuotedStringPropertyNamesTest
DoubleQuotedStringPropertyNamesTest