jsoniopipe - exaples?

Steven Schveighoffer schveiguy at gmail.com
Sat Dec 30 01:30:22 UTC 2023


On Friday, 29 December 2023 at 08:09:58 UTC, Zz wrote:
> Hi,
>
> Here are some samples from the std.json documentation.
> Any idea on how to do something similar using jsoniopipe?
>
> Directly copied from https://dlang.org/phobos/std_json.html
>
> import std.conv : to;
>
> // parse a file or string of json into a usable structure
> string s = `{ "language": "D", "rating": 3.5, "code": "42" }`;
> JSONValue j = parseJSON(s);
> // j and j["language"] return JSONValue,
> // j["language"].str returns a string
> writeln(j["language"].str); // "D"
> writeln(j["rating"].floating); // 3.5
>
> // check a type
> long x;
> if (const(JSONValue)* code = "code" in j)
> {
>     if (code.type() == JSONType.integer)
>         x = code.integer;
>     else
>         x = to!int(code.str);
> }
>
> // create a json struct
> JSONValue jj = [ "language": "D" ];
> // rating doesnt exist yet, so use .object to assign
> jj.object["rating"] = JSONValue(3.5);
> // create an array to assign to list
> jj.object["list"] = JSONValue( ["a", "b", "c"] );
> // list already exists, so .object optional
> jj["list"].array ~= JSONValue("D");
>
> string jjStr = 
> `{"language":"D","list":["a","b","c","D"],"rating":3.5}`;
> writeln(jj.toString); // jjStr

jsoniopipe is not focused on the `JSONValue` 
[equivalent](https://github.com/schveiguy/jsoniopipe/blob/7d63a2e19ae46a1ae56ccab4c6c872bcce094286/source/iopipe/json/dom.d#L22)

You can see it's pretty basic and just serves as a "catch any 
type" thing.

It could easily be replaced with `std.json.JSONValue`, though I 
like the fact that it's templated on the string type.

The main focus of jsoniopipe is parsing and serialization -- I 
prefer to use real concrete structs/classes rather than variant 
types. In fact, the huge benefit from the library is that there 
is no intermediate type.

But yeah, I could ingest all the functionality from std.json 
there. Or maybe even just use `JSONValue` from std.json. Could 
you make an issue?

-Steve


More information about the Digitalmars-d-learn mailing list