Skip to content

Commit

Permalink
Apply change from s-ludwig
Browse files Browse the repository at this point in the history
  • Loading branch information
WebFreak001 committed Oct 31, 2017
1 parent 4f6ba1d commit a0b7297
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions data/vibe/data/json.d
Original file line number Diff line number Diff line change
Expand Up @@ -1707,7 +1707,7 @@ unittest { // issue #1660 - deserialize AA whose key type is string-based enum
struct JsonSerializer {
template isJsonBasicType(T) { enum isJsonBasicType = std.traits.isNumeric!T || isBoolean!T || is(T == string) || is(T == typeof(null)) || isJsonSerializable!T; }

template isSupportedValueType(T) { enum isSupportedValueType = isJsonBasicType!T || is(T == Json); }
template isSupportedValueType(T) { enum isSupportedValueType = isJsonBasicType!T || is(T == Json) || is (T == JSONValue); }

private {
Json m_current;
Expand Down Expand Up @@ -1735,7 +1735,9 @@ struct JsonSerializer {
void writeValue(Traits, T)(in T value)
if (!is(T == Json))
{
static if (isJsonSerializable!T) {
static if (is(T == JSONValue)) {
m_current = Json.fromJSONValue(value);
} else static if (isJsonSerializable!T) {
static if (!__traits(compiles, () @safe { return value.toJson(); } ()))
pragma(msg, "Non-@safe toJson/fromJson methods are deprecated - annotate "~T.stringof~".toJson() with @safe.");
m_current = () @trusted { return value.toJson(); } ();
Expand Down Expand Up @@ -1780,6 +1782,7 @@ struct JsonSerializer {
T readValue(Traits, T)()
@safe {
static if (is(T == Json)) return m_current;
else static if (is(T == JSONValue)) return m_current.toJSONValue;
else static if (isJsonSerializable!T) {
static if (!__traits(compiles, () @safe { return T.fromJson(m_current); } ()))
pragma(msg, "Non-@safe toJson/fromJson methods are deprecated - annotate "~T.stringof~".fromJson() with @safe.");
Expand Down Expand Up @@ -1817,7 +1820,7 @@ struct JsonStringSerializer(R, bool pretty = false)

template isJsonBasicType(T) { enum isJsonBasicType = std.traits.isNumeric!T || isBoolean!T || is(T == string) || is(T == typeof(null)) || isJsonSerializable!T; }

template isSupportedValueType(T) { enum isSupportedValueType = isJsonBasicType!T || is(T == Json); }
template isSupportedValueType(T) { enum isSupportedValueType = isJsonBasicType!T || is(T == Json) || is(T == JSONValue); }

this(R range)
{
Expand Down Expand Up @@ -1866,7 +1869,7 @@ struct JsonStringSerializer(R, bool pretty = false)
m_range.put('"');
}
else static if (is(T == Json)) m_range.writeJsonString(value);
else static if (is(T == JSONValue)) m_range.writeJsonString(Json.fromJSONValue(value));
else static if (is(T == JSONValue)) m_range.writeJsonString(() @trusted { return Json.fromJSONValue(value); } ());
else static if (isJsonSerializable!T) {
static if (!__traits(compiles, () @safe { return value.toJson(); } ()))
pragma(msg, "Non-@safe toJson/fromJson methods are deprecated - annotate "~T.stringof~".toJson() with @safe.");
Expand Down Expand Up @@ -2536,8 +2539,8 @@ private auto trustedRange(R)(R range)
assert(b.foos[0].foos.length == 0);
}

@system unittest { // Json <-> std.json.JSONValue
auto a = parseJsonString(`{
@safe unittest { // Json <-> std.json.JSONValue
auto astr = `{
"null": null,
"string": "Hello",
"integer": 123456,
Expand All @@ -2547,8 +2550,17 @@ private auto trustedRange(R)(R range)
"array": [1, 2, "string"],
"true": true,
"false": false
}`);
}`;
auto a = parseJsonString(astr);

// test JSONValue -> Json conversion
assert(Json.fromJSONValue(a.toJSONValue) == a);

// test Json -> JSONValue conversion
auto v = a.toJSONValue;
assert(deserializeJson!JSONValue(serializeToJson(v)) == v);

// test JSON strint <-> JSONValue serialization
assert(deserializeJson!JSONValue(astr) == v);
assert(parseJsonString(serializeToJsonString(v)) == a);
}

0 comments on commit a0b7297

Please sign in to comment.