std.json questions
tired_eyes via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Apr 25 02:56:22 PDT 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!
More information about the Digitalmars-d-learn
mailing list