Thread overview
jsoniopipe - exaples?
Dec 29
Zz
Dec 31
Zz
December 29

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

Regards,
Zz

December 30

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

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

December 31

On Saturday, 30 December 2023 at 01:30:22 UTC, Steven Schveighoffer wrote:

>

On Friday, 29 December 2023 at 08:09:58 UTC, Zz wrote:

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

Hi Steve,

It would be a welcome addition.

Regards,
Zz