Thread overview | |||||
---|---|---|---|---|---|
|
January 07, 2013 Unexpected behavior of std.json.toJSON | ||||
---|---|---|---|---|
| ||||
Sample code: ------------------- import std.stdio; import std.json; void main(string[] args){ string text = "{ \"url\":\"http://www.boost.org\" }"; JSONValue json = parseJSON(text); writeln(toJSON(&json)); // prints: { "url":"http\/\/www.boost.org\" } // The same happens when writing to a file. } -------------------- The double slash "//" in the url is replaced by "\/\/". Is that a feature or a bug? If the former, what is the reason for this behavior and how to avoid it? Peter |
January 07, 2013 Re: Unexpected behavior of std.json.toJSON | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Sommerfeld | On Monday, 7 January 2013 at 23:16:51 UTC, Peter Sommerfeld wrote: > The double slash "//" in the url is replaced by "\/\/". > Is that a feature or a bug? It is intentional. I searched the web for other json implementations that do the same thing and came up with this link for why: http://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped "Allowing \/ helps when embedding JSON in a <script> tag, which doesn't allow </ inside strings, like Seb points out." There's no way to disable it in D's std.json, but you don't have to either - it is perfectly correct according to the standard. |
January 08, 2013 Re: Unexpected behavior of std.json.toJSON | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | Thanks Adam! D. Ruppe:
> On Monday, 7 January 2013 at 23:16:51 UTC, Peter Sommerfeld wrote:
>> The double slash "//" in the url is replaced by "\/\/".
>> Is that a feature or a bug?
>
> It is intentional. I searched the web for other json implementations that do the same thing and came up with this link for why:
>
> http://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped
>
> "Allowing \/ helps when embedding JSON in a <script> tag, which doesn't allow </ inside strings, like Seb points out."
>
> There's no way to disable it in D's std.json, but you don't have to either - it is perfectly correct according to the standard.
Thanks Adam! On the other hand: json is fine to be edited by humans and
in this case simple double slashes would be better. Thus a second bool
value to discriminate both variants where preferable. No serious problem
anyway ...
Peter
|
Copyright © 1999-2021 by the D Language Foundation