Skip to content

Commit

Permalink
Fix issue 20511: make toJSON infer safeness based on output range saf…
Browse files Browse the repository at this point in the history
…eness.
  • Loading branch information
FeepingCreature committed Jan 17, 2020
1 parent dd27702 commit 3e26069
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions std/json.d
Expand Up @@ -1399,10 +1399,10 @@ void toJSON(Out)(
auto ref Out json,
const ref JSONValue root,
in bool pretty = false,
in JSONOptions options = JSONOptions.none) @safe
in JSONOptions options = JSONOptions.none)
if (isOutputRange!(Out,char))
{
void toStringImpl(Char)(string str) @safe
void toStringImpl(Char)(string str)
{
json.put('"');

Expand Down Expand Up @@ -1463,7 +1463,7 @@ if (isOutputRange!(Out,char))
json.put('"');
}

void toString(string str) @safe
void toString(string str)
{
// Avoid UTF decoding when possible, as it is unnecessary when
// processing JSON.
Expand All @@ -1473,7 +1473,19 @@ if (isOutputRange!(Out,char))
toStringImpl!char(str);
}

void toValue(ref const JSONValue value, ulong indentLevel) @safe
// recursive @safe inference is broken here
// workaround: if json.put is @safe, we should be too,
// so annotate the recursion as @safe manually
static if (isSafe!({ json.put(""); }))
{
void delegate(ref const JSONValue, ulong) @safe toValue;
}
else
{
void delegate(ref const JSONValue, ulong) @system toValue;
}

void toValueImpl(ref const JSONValue value, ulong indentLevel)
{
void putTabs(ulong additionalIndent = 0)
{
Expand Down Expand Up @@ -1629,6 +1641,8 @@ if (isOutputRange!(Out,char))
}
}

toValue = &toValueImpl;

toValue(root, 0);
}

Expand All @@ -1646,6 +1660,15 @@ if (isOutputRange!(Out,char))
assert(toJSON(jv1, false, JSONOptions.none) == `"été"`);
}

@system unittest // bugzilla 20511
{
import std.format : formattedWrite;
import std.range : nullSink, outputRangeObject;

outputRangeObject!(const(char)[])(nullSink)
.formattedWrite!"%s"(JSONValue.init);
}

/**
Exception thrown on JSON errors
*/
Expand Down

0 comments on commit 3e26069

Please sign in to comment.