std.json questions
Dan Olson via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Apr 25 09:34:35 PDT 2015
"tired_eyes" <pastuhov85 at 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);
}
}
More information about the Digitalmars-d-learn
mailing list