www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Json in D: clean, simple API

reply aberba <karabutaworld gmail.com> writes:
Json libs in D seem not straight foward. I've seen several 
wrappers and (De)serializers trying to abstract std.json.

std_data_json proposed to replace std.json doesnt *seem* to 
tackle straigh-forward json process like I find vibe.data.json.

Handy stuff I'm not finding but much simple in vibe.data.json 
include piecewise construction (constructing json objects like 
associative arrays) and vibe.data.json's value.get!T. API feel 
also *feels* not straight-forward.

Are all the capabilities of std_data_json what I see in its docs?

Why does json seem hard in D and why is std.json still there?
May 11 2017
next sibling parent reply cym13 <cpicard openmailbox.org> writes:
On Thursday, 11 May 2017 at 19:33:09 UTC, aberba wrote:
 Json libs in D seem not straight foward. I've seen several 
 wrappers and (De)serializers trying to abstract std.json.

 std_data_json proposed to replace std.json doesnt *seem* to 
 tackle straigh-forward json process like I find vibe.data.json.

 Handy stuff I'm not finding but much simple in vibe.data.json 
 include piecewise construction (constructing json objects like 
 associative arrays) and vibe.data.json's value.get!T. API feel 
 also *feels* not straight-forward.

 Are all the capabilities of std_data_json what I see in its 
 docs?

 Why does json seem hard in D and why is std.json still there?
Try https://github.com/tamediadigital/asdf ?
May 11 2017
parent aberba <karabutaworld gmail.com> writes:
On Thursday, 11 May 2017 at 19:49:54 UTC, cym13 wrote:
 On Thursday, 11 May 2017 at 19:33:09 UTC, aberba wrote:
 Json libs in D seem not straight foward. I've seen several 
 wrappers and (De)serializers trying to abstract std.json.

 std_data_json proposed to replace std.json doesnt *seem* to 
 tackle straigh-forward json process like I find vibe.data.json.

 Handy stuff I'm not finding but much simple in vibe.data.json 
 include piecewise construction (constructing json objects like 
 associative arrays) and vibe.data.json's value.get!T. API feel 
 also *feels* not straight-forward.

 Are all the capabilities of std_data_json what I see in its 
 docs?

 Why does json seem hard in D and why is std.json still there?
Try https://github.com/tamediadigital/asdf ?
Not much helpful for my use case though it can be really useful for json-heavy rest api. Interesting.
May 11 2017
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 11 May 2017 at 19:33:09 UTC, aberba wrote:
 Why does json seem hard in D
What are you actually looking for?
May 11 2017
next sibling parent aberba <karabutaworld gmail.com> writes:
On Thursday, 11 May 2017 at 20:04:35 UTC, Adam D. Ruppe wrote:
 On Thursday, 11 May 2017 at 19:33:09 UTC, aberba wrote:
 Why does json seem hard in D
What are you actually looking for?
I managed to do the task but api makes code not clean for such simple tasks (AA to json, json to AA, json["new key"] = newJson, ...). vibe.data.json does a good job with api design specially construction of new json objects from AA and D types to json types.
May 11 2017
prev sibling parent reply aberba <karabutaworld gmail.com> writes:
On Thursday, 11 May 2017 at 20:04:35 UTC, Adam D. Ruppe wrote:
 On Thursday, 11 May 2017 at 19:33:09 UTC, aberba wrote:
 Why does json seem hard in D
What are you actually looking for?
With that i meant designing of a simple-clean api
May 11 2017
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 11 May 2017 at 20:22:22 UTC, aberba wrote:
 With that i meant designing of a simple-clean api
my jsvar.d works kinda similarly to javascript... though i wouldn't call it "clean" because it will not inform you of missing stuff. jsvar.d is here: https://raw.githubusercontent.com/adamdruppe/arsd/master/jsvar.d --- // dmd test.d jsvar.d import arsd.jsvar; import std.stdio; void main() { // reading json with `var.fromJson` var obj = var.fromJson(`{"a":{"b":10},"c":"hi"}`); // inspecting contents writeln(obj.a); writeln(obj.a.b); writeln(obj.c); // convert to basic static type with `.get!T` string c = obj.c.get!string; // change the contents with dot notation obj.a = 15; obj.d = "add new field too"; writeln(obj); struct Test { int a; string c; } // can even get plain structs out Test test = obj.get!Test; writeln(test); // and set structs test.c = "from D"; obj = test; writeln(obj); // writeln on an object prints it in json // or you can explicitly do writeln(obj.toJson()); // big thing is referencing non-existent // things is not an error, it just propagates null: writeln(obj.no.such.property); // null // but that can be convenient } ---
May 11 2017
parent reply aberba <karabutaworld gmail.com> writes:
On Thursday, 11 May 2017 at 20:36:13 UTC, Adam D. Ruppe wrote:
 On Thursday, 11 May 2017 at 20:22:22 UTC, aberba wrote:
 With that i meant designing of a simple-clean api
my jsvar.d works kinda similarly to javascript... though i wouldn't call it "clean" because it will not inform you of missing stuff. jsvar.d is here: https://raw.githubusercontent.com/adamdruppe/arsd/master/jsvar.d --- // dmd test.d jsvar.d import arsd.jsvar; import std.stdio; void main() { // reading json with `var.fromJson` var obj = var.fromJson(`{"a":{"b":10},"c":"hi"}`); // inspecting contents writeln(obj.a); writeln(obj.a.b); writeln(obj.c); // convert to basic static type with `.get!T` string c = obj.c.get!string; // change the contents with dot notation obj.a = 15; obj.d = "add new field too"; writeln(obj); struct Test { int a; string c; } // can even get plain structs out Test test = obj.get!Test; writeln(test); // and set structs test.c = "from D"; obj = test; writeln(obj); // writeln on an object prints it in json // or you can explicitly do writeln(obj.toJson()); // big thing is referencing non-existent // things is not an error, it just propagates null: writeln(obj.no.such.property); // null // but that can be convenient } ---
Something like this is exactly what I'm talking about. Vibe.data.json also has: // using piecewise construction Json j2 = Json.emptyObject; j2["field1"] = "foo"; j2["field2"] = 42.0; j2["field3"] = true; D doesn't seem to be the blocker for these convenient abstractions.
May 11 2017
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 11 May 2017 at 20:56:09 UTC, aberba wrote:
 Something like this is exactly what I'm talking about. 
 Vibe.data.json also has:

 // using piecewise construction
 Json j2 = Json.emptyObject;
 j2["field1"] = "foo";
 j2["field2"] = 42.0;
 j2["field3"] = true;
Yeah, mine can do that too, just change `Json` to `var`. I even coincidentally called it `emptyObject` too, cool. Mine also allows you do do math and concatenations and even assign functions, it is crazy what D can do.
May 11 2017