www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there a way to not escape slashes when parsing JSON?

reply bachmeier <no spam.net> writes:
I tried this

```
import std.json, std.stdio;

void main() {
     writeln(parseJSON(`{"a": "path/file"}`, 
JSONOptions.doNotEscapeSlashes));
}
```

but the output is

```
{"a":"path\/file"}
```

Is there a way to avoid the escaping of the forward slash? Is 
there some reason I should want to escape the forward slash?
Feb 20 2022
next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On Monday, 21 February 2022 at 03:42:55 UTC, bachmeier wrote:
 I tried this

 ```d
 import std.json, std.stdio;

 void main() {
     writeln(parseJSON(`{"a": "path/file"}`, 
 JSONOptions.doNotEscapeSlashes));
 }
 ```

 but the output is

 ```
 {"a":"path\/file"}
 ```

 Is there a way to avoid the escaping of the forward slash? Is 
 there some reason I should want to escape the forward slash?
The options are applied on parsing or output but do not stay with the item! So just because you parsed without allowing escapes on slashes doesn't mean the output will use that option. 2 ways I found: ```d // 1. allocate a string to display writeln(parseJson(...).toString(JSONOptions.doNotEscapeSlashes)); // 2. wrap so you can hook the output range version of toString struct NoEscapeJson { JSONValue json; void toString(Out)(Out outputrange) const { json.toString(outputrange, JSONOptions.doNotEscapeSlashes); } } writeln(NoEscapeJson(parseJson(...))); ``` -Steve
Feb 20 2022
parent reply bachmeier <no spam.net> writes:
On Monday, 21 February 2022 at 04:02:23 UTC, Steven Schveighoffer 
wrote:
 On Monday, 21 February 2022 at 03:42:55 UTC, bachmeier wrote:
 I tried this

 ```d
 import std.json, std.stdio;

 void main() {
     writeln(parseJSON(`{"a": "path/file"}`, 
 JSONOptions.doNotEscapeSlashes));
 }
 ```

 but the output is

 ```
 {"a":"path\/file"}
 ```

 Is there a way to avoid the escaping of the forward slash? Is 
 there some reason I should want to escape the forward slash?
The options are applied on parsing or output but do not stay with the item! So just because you parsed without allowing escapes on slashes doesn't mean the output will use that option. 2 ways I found: ```d // 1. allocate a string to display writeln(parseJson(...).toString(JSONOptions.doNotEscapeSlashes)); // 2. wrap so you can hook the output range version of toString struct NoEscapeJson { JSONValue json; void toString(Out)(Out outputrange) const { json.toString(outputrange, JSONOptions.doNotEscapeSlashes); } } writeln(NoEscapeJson(parseJson(...))); ``` -Steve
I've had a chance to look into this. The documentation for `parseJSON` says: ``` JSONOptions options enable decoding string representations of NaN/Inf as float values ``` which looks to me as if there's no way to apply `doNotEscapeSlashes` while parsing. Your approach works if the goal is to print out the JSON as it was passed in. I don't see any way to work with the parsed JSON data. If I want to later work with element "a" and do something with "path/file", the value will always be "path\/file". AFAICT, I'd have to manually unescape every element.
Feb 21 2022
parent reply bachmeier <no spam.net> writes:
On Monday, 21 February 2022 at 17:32:23 UTC, bachmeier wrote:
 On Monday, 21 February 2022 at 04:02:23 UTC, Steven 
 Schveighoffer wrote:
 On Monday, 21 February 2022 at 03:42:55 UTC, bachmeier wrote:
 I tried this

 ```d
 import std.json, std.stdio;

 void main() {
     writeln(parseJSON(`{"a": "path/file"}`, 
 JSONOptions.doNotEscapeSlashes));
 }
 ```

 but the output is

 ```
 {"a":"path\/file"}
 ```

 Is there a way to avoid the escaping of the forward slash? Is 
 there some reason I should want to escape the forward slash?
The options are applied on parsing or output but do not stay with the item! So just because you parsed without allowing escapes on slashes doesn't mean the output will use that option. 2 ways I found: ```d // 1. allocate a string to display writeln(parseJson(...).toString(JSONOptions.doNotEscapeSlashes)); // 2. wrap so you can hook the output range version of toString struct NoEscapeJson { JSONValue json; void toString(Out)(Out outputrange) const { json.toString(outputrange, JSONOptions.doNotEscapeSlashes); } } writeln(NoEscapeJson(parseJson(...))); ``` -Steve
I've had a chance to look into this. The documentation for `parseJSON` says: ``` JSONOptions options enable decoding string representations of NaN/Inf as float values ``` which looks to me as if there's no way to apply `doNotEscapeSlashes` while parsing. Your approach works if the goal is to print out the JSON as it was passed in. I don't see any way to work with the parsed JSON data. If I want to later work with element "a" and do something with "path/file", the value will always be "path\/file". AFAICT, I'd have to manually unescape every element.
I looked at the source for `parseJSON` and I see references only to `JSONOptions.strictParsing` and `JSONOptions.specialFloatLiterals`. I may be missing something, but I don't see any option to iterating over every element and unescaping manually.
Feb 21 2022
parent bachmeier <no spam.net> writes:
On Monday, 21 February 2022 at 17:50:56 UTC, bachmeier wrote:

 I looked at the source for `parseJSON` and I see references 
 only to `JSONOptions.strictParsing` and 
 `JSONOptions.specialFloatLiterals`. I may be missing something, 
 but I don't see any option to iterating over every element and 
 unescaping manually.
So it looks like `.toString(JSONOptions.doNotEscapeSlashes)` is the correct way to do what I need: ``` import std.conv, std.json, std.stdio; void main() { auto json = parseJSON(`{"a": "path/file"}`); writeln(json["a"].toString); writeln(json["a"].to!string); writeln(json["a"].toString(JSONOptions.doNotEscapeSlashes)); } ``` I was using to!string to be consistent with my other D code, and thought that would be okay since toString returns the same value. What I didn't realize is that I might need to send options to toString.
Feb 21 2022
prev sibling parent reply bauss <jj_1337 live.dk> writes:
On Monday, 21 February 2022 at 03:42:55 UTC, bachmeier wrote:
 I tried this

 ```
 import std.json, std.stdio;

 void main() {
     writeln(parseJSON(`{"a": "path/file"}`, 
 JSONOptions.doNotEscapeSlashes));
 }
 ```

 but the output is

 ```
 {"a":"path\/file"}
 ```

 Is there a way to avoid the escaping of the forward slash? Is 
 there some reason I should want to escape the forward slash?
Why are we even escaping them by default, it should be the other way around, that slashes are only escaped if you ask for it; that's how it literally is in almost every JSON library. Escaping slashes as a default is a huge mistake IMHO.
Feb 21 2022
next sibling parent reply Kagamin <spam here.lot> writes:
On Monday, 21 February 2022 at 09:04:06 UTC, bauss wrote:
 Why are we even escaping them by default, it should be the 
 other way around, that slashes are only escaped if you ask for 
 it; that's how it literally is in almost every JSON library.
Really? I always see escaped slashes in JSON, e.g. wikipedia does this, but everything else too.
Feb 21 2022
parent bauss <jj_1337 live.dk> writes:
On Monday, 21 February 2022 at 15:13:52 UTC, Kagamin wrote:
 On Monday, 21 February 2022 at 09:04:06 UTC, bauss wrote:
 Why are we even escaping them by default, it should be the 
 other way around, that slashes are only escaped if you ask for 
 it; that's how it literally is in almost every JSON library.
Really? I always see escaped slashes in JSON, e.g. wikipedia does this, but everything else too.
I'm going to assume that JS is probably the most used language for JSON, since it originated from it, so a small demonstration will show you that even JS defaults to not escaping slashes: ``` let o = { a: "/path/to/something" }; console.log(JSON.stringify(o)); ``` Output: ``` {"a":"/path/to/something"} ```
Feb 21 2022
prev sibling parent reply bachmeier <no spam.net> writes:
On Monday, 21 February 2022 at 09:04:06 UTC, bauss wrote:

 Why are we even escaping them by default, it should be the 
 other way around, that slashes are only escaped if you ask for 
 it; that's how it literally is in almost every JSON library.

 Escaping slashes as a default is a huge mistake IMHO.
I seldom work with JSON data, but I may have to look for an alternative JSON library for D. std.json is not the most fun independent of this issue.
Feb 21 2022
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 2/21/22 09:34, bachmeier wrote:

 I may have to look for an alternative
 JSON library for D. std.json is not the most fun independent of this 
issue. std.json is a very good module. At work, we had to write additional code to cover its defficiencies. Looking forward to versioning in Phobos so that we can get to better implementations of many old modules... Ali
Feb 21 2022
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 2/21/22 14:58, Ali Çehreli wrote:

 std.json is a very good module.
Correction: std.json is NOT a very good module. Ali
Feb 21 2022
prev sibling parent reply bachmeier <no spam.net> writes:
On Monday, 21 February 2022 at 22:58:17 UTC, Ali Çehreli wrote:
 On 2/21/22 09:34, bachmeier wrote:

 I may have to look for an alternative
 JSON library for D. std.json is not the most fun independent
of this issue. std.json is a very good module. At work, we had to write additional code to cover its defficiencies. Looking forward to versioning in Phobos so that we can get to better implementations of many old modules... Ali
Yes. std.random is another. I gave up out on the current one. Luckily I already had external libraries for that before I started using D.
Feb 21 2022
parent reply jmh530 <john.michael.hall gmail.com> writes:
On Tuesday, 22 February 2022 at 00:36:38 UTC, bachmeier wrote:
 [snip]

 Yes. std.random is another. I gave up out on the current one. 
 Luckily I already had external libraries for that before I 
 started using D.
Have you tried mir.random?
Feb 21 2022
parent bachmeier <no spam.net> writes:
On Tuesday, 22 February 2022 at 00:44:58 UTC, jmh530 wrote:
 On Tuesday, 22 February 2022 at 00:36:38 UTC, bachmeier wrote:
 [snip]

 Yes. std.random is another. I gave up out on the current one. 
 Luckily I already had external libraries for that before I 
 started using D.
Have you tried mir.random?
I haven't done much with it. I already have what I need, so not much motivation to use anything else. The main advantage of std.random is that it eliminates an external dependency.
Feb 21 2022