www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Are JSONOptions broken?

reply frame <frame86 live.com> writes:
I need to encode a key as string through json.

This throws an UTF-exception:

auto json = JSONValue(cast(char[])[0x00, 0x7D, 0xFE, 0xFF, 0x14, 
0x32, 0x43, 0x10]);
writeln(json.toString(JSONOptions.escapeNonAsciiChars));

Makes no sense. Either the bytes should be properly escaped or 
there should not be any option if a string want to be converted 
to UTF anyway.

This is also fun:

auto json = 
JSONValue(r"\u0000\u007D\u00FE\u00FF\u0014\u0032\u0043\u0010");
assert(json.toString() == 
json.toString(JSONOptions.doNotEscapeSlashes));

and ends with:

"\\u0000\\u007D\\u00FE\\u00FF\\u0014\\u0032\\u0043\\u0010"
Nov 28 2020
parent reply ag0aep6g <anonymous example.com> writes:
On 28.11.20 15:21, frame wrote:
 This throws an UTF-exception:
 
 auto json = JSONValue(cast(char[])[0x00, 0x7D, 0xFE, 0xFF, 0x14, 0x32, 
 0x43, 0x10]);
 writeln(json.toString(JSONOptions.escapeNonAsciiChars));
 
 Makes no sense. Either the bytes should be properly escaped or there 
 should not be any option if a string want to be converted to UTF anyway.
Makes perfect sense. The option is called "escapeNonAsciiChars", not "escapeNonUnicodeChars".
 This is also fun:
 
 auto json = JSONValue(r"\u0000\u007D\u00FE\u00FF\u0014\u0032\u0043\u0010");
 assert(json.toString() == json.toString(JSONOptions.doNotEscapeSlashes));
 
 and ends with:
 
 "\\u0000\\u007D\\u00FE\\u00FF\\u0014\\u0032\\u0043\\u0010"
This is a slash: / This is a backslash: \ There are no slashes in your string.
Nov 28 2020
parent frame <frame86 live.com> writes:
On Saturday, 28 November 2020 at 16:59:11 UTC, ag0aep6g wrote:
 Makes perfect sense. The option is called 
 "escapeNonAsciiChars", not "escapeNonUnicodeChars".
 This is a slash: /
 This is a backslash: \

 There are no slashes in your string.
Thanks, I realized that the options are only for dealing on UTF input, not to ensure valid JSON output.
Nov 28 2020