On Friday, 26 August 2016 at 07:46:13 UTC, Daniel Kozak wrote:
Another way is to implement deepCopy by yourself (something like below)

import std.json;
import std.stdio;

JSONValue deepCopy(ref JSONValue val)
{
    JSONValue newVal;
    switch(val.type)
    {
        case JSON_TYPE.STRING:
            newVal = JSONValue(val.str.idup);
            break;
        case JSON_TYPE.OBJECT:
            foreach (string key, value; val)
            {
                newVal[key] = value.deepCopy;
            }
            break;
        case JSON_TYPE.ARRAY:
            foreach (size_t index, value; val)
            {
                newVal[index] = value.deepCopy;
            }
            break;
        default:
            newVal = val;

    }
    return newVal;
}

void main()
{
string s = "{ \"language\": \"D\", \"rating\": 3.14, \"code\": 42 }";
    JSONValue j = parseJSON(s);
    writeln("code: ", j["code"].integer);

    auto j2 = j.deepCopy;
    j2["code"] = 43;
    writeln("code of j2: ", j2["code"].integer);
    writeln("code of j: ", j["code"].integer);
}

Yeah... I thought that i would have to implement something like this... And wanted to avoid this... So, yes, I think this is the more convincing way, but in my case the --> toString --> toJson conversion is enough...

Thanks for help :)

Reply via email to