Jump to page: 1 2
Thread overview
Is there a way to not escape slashes when parsing JSON?
Feb 21, 2022
bachmeier
Feb 21, 2022
bachmeier
Feb 21, 2022
bachmeier
Feb 21, 2022
bachmeier
Feb 21, 2022
bauss
Feb 21, 2022
Kagamin
Feb 21, 2022
bauss
Feb 21, 2022
bachmeier
Feb 21, 2022
Ali Çehreli
Feb 21, 2022
Ali Çehreli
Feb 22, 2022
bachmeier
Feb 22, 2022
jmh530
Feb 22, 2022
bachmeier
February 21, 2022

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?

February 21, 2022

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?

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:

// 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

February 21, 2022

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.

February 21, 2022

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.

February 21, 2022

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

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:

// 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.

February 21, 2022

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.

February 21, 2022

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

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:

// 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.

February 21, 2022

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.

February 21, 2022
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

February 21, 2022

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"}
« First   ‹ Prev
1 2