Thread overview
Are JSONOptions broken?
Nov 28, 2020
frame
Nov 28, 2020
ag0aep6g
Nov 28, 2020
frame
November 28, 2020
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"
November 28, 2020
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.
November 28, 2020
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.