Thread overview
std.json questions
Apr 25, 2015
tired_eyes
Apr 25, 2015
Dan Olson
Apr 25, 2015
rcorre
Apr 25, 2015
Baz
Apr 26, 2015
tired_eyes
Apr 26, 2015
extrawurst
Apr 26, 2015
weaselcat
Apr 27, 2015
Pierre Krafft
April 25, 2015
Hello, D community!

I'm pretty new to D and to compiled languages in general, and have primarily web background (PHP, JS), when JSON workflow is very organic. I was always sure that JSON is a simple thing, but std.json proves me wrong. So may I have a little advice from more experienced D folk?

Say, I have a simple JSON file:

{
    "entities" : [
        {
            "x" : 0,
            "y" : 0,
            "texture" : "box1"
        },
        {
            "x" : 100,
            "y" : 200,
            "texture" : "box2",
            "isControllable" : true
        }
    ]
}

First issue: what is the proper ("idiomatic") way to conver JSONValue to the proper types?

Second: what is the proper way of handling boolean values in JSON (how to convert JSON_TYPE.TRUE and JSON_TYPE.FALSE to bool)?

Righ now I'm doing is something like this:

string data = readText("file.json");
JSONValue[string] parsedData = parseJSON(data).object;
JSONValue[] etities = stateData["entities"].array;

foreach(e; entities) {
    int x = to!int(e["x"].integer);
    int y = to!int(e["y"].integer);
    string texture = stripExtension(e["texture"].str);

    auto isControllable = "isControllable" in e;

    if (isControllable !is null) {
        if (e["isControllable"].type == JSON_TYPE.TRUE) {
            isControllable = true;
        } else {
            isControllable = false;
        }
    }
}

I think this is ugly and clunky approach, what is the beautiful one?

A brief look at code.dlang.org gives us 7 (!) additional JSON libraries. Keeping in mind that D community isn't so huge, I think I'm not the only person struggling with std.json. Are there any plans on upgrading it?

Thank you in advance!
April 25, 2015
"tired_eyes" <pastuhov85@gmail.com> writes:
>
> First issue: what is the proper ("idiomatic") way to conver JSONValue
> to the proper types?
>
> Second: what is the proper way of handling boolean values in JSON (how to convert JSON_TYPE.TRUE and JSON_TYPE.FALSE to bool)?
>
> Righ now I'm doing is something like this:
>
> string data = readText("file.json");
> JSONValue[string] parsedData = parseJSON(data).object;
> JSONValue[] etities = stateData["entities"].array;
>
> foreach(e; entities) {
>     int x = to!int(e["x"].integer);
>     int y = to!int(e["y"].integer);
>     string texture = stripExtension(e["texture"].str);
>
>     auto isControllable = "isControllable" in e;
>
>     if (isControllable !is null) {
>         if (e["isControllable"].type == JSON_TYPE.TRUE) {
>             isControllable = true;
>         } else {
>             isControllable = false;
>         }
>     }
> }
>
> I think this is ugly and clunky approach, what is the beautiful one?
>
> A brief look at code.dlang.org gives us 7 (!) additional JSON libraries. Keeping in mind that D community isn't so huge, I think I'm not the only person struggling with std.json. Are there any plans on upgrading it?

Hi and welcome to D land.  I see discussions on how std.json needs to be upgraded.  And it is not well documented.

I tried to progressively simplify the code that was posted to show what can be done, but keeping the same spirit.  Not necessarily beautiful, but less verbose code. I did not see a simpler way to deal with bools in the std.json code.  Others here are experts on idiomatic D, they may show something much better.

// First , make it work and show all types
void f1()
{
    string data = readText("file.json");
    JSONValue parsedData = parseJSON(data);
    JSONValue entities = parsedData["entities"];

    foreach(size_t index, e; entities) {
        long x = e["x"].integer;
        long y = e["y"].integer;
        string texture = stripExtension(e["texture"].str);

        bool isControllable = false;

        if ("isControllable" in e) {
            if (e["isControllable"].type == JSON_TYPE.TRUE) {
                isControllable = true;
            } else {
                isControllable = false;
            }
        }
        writefln("x %d y %d texture %s isControllable %s",
                 x, y, texture, isControllable);
    }
}

// Next,  let compiler figure types for us
void f2()
{
    auto data = readText("file.json");
    auto parsedData = parseJSON(data);
    auto entities = parsedData["entities"];

    foreach(size_t _, e; entities) {
        auto x = e["x"].integer;
        auto y = e["y"].integer;
        auto texture = stripExtension(e["texture"].str);

        bool isControllable = false;

        if ("isControllable" in e) {
            isControllable = e["isControllable"].type == JSON_TYPE.TRUE;
        }
        writefln("x %d y %d texture %s isControllable %s",
                 x, y, texture, isControllable);
    }
}

// A little simpler isControllable.
void f3()
{
    auto parsedData = readText("file.json").parseJSON;

    foreach(size_t _, e; parsedData["entities"]) {
        auto x = e["x"].integer;
        auto y = e["y"].integer;
        auto texture = stripExtension(e["texture"].str);
        auto isControllable = "isControllable" in e &&
                               e["isControllable"].type == JSON_TYPE.TRUE;
        writefln("x %d y %d texture %s isControllable %s",
                 x, y, texture, isControllable);
    }
}

April 25, 2015
On Saturday, 25 April 2015 at 09:56:25 UTC, tired_eyes wrote:
> A brief look at code.dlang.org gives us 7 (!) additional JSON libraries. Keeping in mind that D community isn't so huge, I think I'm not the only person struggling with std.json. Are there any plans on upgrading it?

See http://wiki.dlang.org/Review_Queue. std.data.json is the proposed replacement for the current phobos json implementation.
There is also supposedly std.serialization in the works.
April 25, 2015
On Saturday, 25 April 2015 at 09:56:25 UTC, tired_eyes wrote:
> I think this is ugly and clunky approach, what is the beautiful one?

What you clearly need is a serializer:

look at these:

http://wiki.dlang.org/Libraries_and_Frameworks#Serialization

and also:

https://github.com/search?utf8=✓&q=serializer+language%3AD&type=Repositories&ref=searchresults

some of them might have an API to save load an object or a struct in a single call.
April 26, 2015
Thank everybody for you help. For now, yajl-d seems to be an optimal for my task, however will keep an eye for stdx.data.json too.
April 26, 2015
On Saturday, 25 April 2015 at 18:30:33 UTC, Baz wrote:
> On Saturday, 25 April 2015 at 09:56:25 UTC, tired_eyes wrote:
>> I think this is ugly and clunky approach, what is the beautiful one?
>
> What you clearly need is a serializer:
>
> look at these:
>
> http://wiki.dlang.org/Libraries_and_Frameworks#Serialization
>
> and also:
>
> https://github.com/search?utf8=✓&q=serializer+language%3AD&type=Repositories&ref=searchresults
>
> some of them might have an API to save load an object or a struct in a single call.

too bad D:YAML links are broken, do you know where to find that project ?
April 26, 2015
On Sunday, 26 April 2015 at 17:14:22 UTC, extrawurst wrote:
> On Saturday, 25 April 2015 at 18:30:33 UTC, Baz wrote:
>> On Saturday, 25 April 2015 at 09:56:25 UTC, tired_eyes wrote:
>>> I think this is ugly and clunky approach, what is the beautiful one?
>>
>> What you clearly need is a serializer:
>>
>> look at these:
>>
>> http://wiki.dlang.org/Libraries_and_Frameworks#Serialization
>>
>> and also:
>>
>> https://github.com/search?utf8=✓&q=serializer+language%3AD&type=Repositories&ref=searchresults
>>
>> some of them might have an API to save load an object or a struct in a single call.
>
> too bad D:YAML links are broken, do you know where to find that project ?

https://github.com/kiith-sa/D-YAML
April 27, 2015
On Saturday, 25 April 2015 at 18:30:33 UTC, Baz wrote:
> On Saturday, 25 April 2015 at 09:56:25 UTC, tired_eyes wrote:
>> I think this is ugly and clunky approach, what is the beautiful one?
>
> What you clearly need is a serializer:
>
> look at these:
>
> http://wiki.dlang.org/Libraries_and_Frameworks#Serialization
>
> and also:
>
> https://github.com/search?utf8=✓&q=serializer+language%3AD&type=Repositories&ref=searchresults
>
> some of them might have an API to save load an object or a struct in a single call.

Also http://code.dlang.org/search?q=json