Skip to content

Commit 68ceb0e

Browse files
Geod24Herringway
authored andcommitted
Fix #302: Support escaped forward slashes
This provides better compatibility with JSON, and is what Vibe.d does by default: https://github.com/vibe-d/vibe.d/blob/7604eea772b1a733f7bfd16591ddc5bd37850bd9/data/vibe/data/json.d#L2555-L2563
1 parent 2b9c75c commit 68ceb0e

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

source/dyaml/escapes.d

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ package:
1111

1212
import std.meta : AliasSeq;
1313
alias escapes = AliasSeq!('0', 'a', 'b', 't', '\t', 'n', 'v', 'f', 'r', 'e', ' ',
14-
'\"', '\\', 'N', '_', 'L', 'P');
14+
'/', '\"', '\\', 'N', '_', 'L', 'P');
1515

1616
/// YAML hex codes specifying the length of the hex number.
1717
alias escapeHexCodeList = AliasSeq!('x', 'u', 'U');
@@ -31,6 +31,7 @@ dchar fromEscape(dchar escape) @safe pure nothrow @nogc
3131
case 'f': return '\x0C';
3232
case 'r': return '\x0D';
3333
case 'e': return '\x1B';
34+
case '/': return '/';
3435
case ' ': return '\x20';
3536
case '\"': return '\"';
3637
case '\\': return '\\';
@@ -90,3 +91,16 @@ uint escapeHexLength(dchar hexCode) @safe pure nothrow @nogc
9091
}
9192
}
9293

94+
// Issue #302: Support optional escaping of forward slashes in string
95+
// for JSON compatibility
96+
@safe unittest
97+
{
98+
import dyaml.loader : Loader;
99+
100+
const str = `{
101+
"forward/slashes": "can\/be\/optionally\/escaped"
102+
}`;
103+
104+
auto node = Loader.fromString(str).load();
105+
assert(node["forward/slashes"] == "can/be/optionally/escaped");
106+
}

0 commit comments

Comments
 (0)