Thread overview
How to convert array of structs to JSON
Jun 25, 2019
mark
Jun 25, 2019
Marco de Wild
Jun 25, 2019
zoujiaqing
June 25, 2019
I have the following array of structs:

struct Person {
    string name;
    int age;
};


Person people[];
Person p;

Person p1 = {
    "Bob",
    12
};

Person p2 = {
    "Bob",
    12
};

people ~= p1;
people ~= p2;

I've read through the std.json documentation, but I'm still confused as to how to create an array of objects, especially if the objects are structs.

I've included this:

JSONValue jj;
jj["nodes"].array = [];

But I get the error: JSONValue is not an object. Ideally, I'd like to have a json output that looks like this:

[

    {
        "name": "Bob",
        "age": 12
    },
    {
        "name": "Bob",
        "age": 12
    },
]

Any help would be greatly appreciated.

June 25, 2019
On Tuesday, 25 June 2019 at 05:33:57 UTC, mark wrote:
> I have the following array of structs:
>
> struct Person {
>     string name;
>     int age;
> };
>
>
> Person people[];
> Person p;
>
> Person p1 = {
>     "Bob",
>     12
> };
>
> Person p2 = {
>     "Bob",
>     12
> };
>
> people ~= p1;
> people ~= p2;
>
> I've read through the std.json documentation, but I'm still confused as to how to create an array of objects, especially if the objects are structs.
>
> I've included this:
>
> JSONValue jj;
> jj["nodes"].array = [];
>
> But I get the error: JSONValue is not an object. Ideally, I'd like to have a json output that looks like this:
>
> [
>
>     {
>         "name": "Bob",
>         "age": 12
>     },
>     {
>         "name": "Bob",
>         "age": 12
>     },
> ]
>
> Any help would be greatly appreciated.

As far as I can tell, there's no "out of the box" support for serialization in std.json. You can work around that by manually supplying the serialization:

https://run.dlang.io/is/AZqTJE

struct Person
{
    string name;
    int age;
    JSONValue toJson()
    {
        return JSONValue(["name": JSONValue(name), "age": JSONValue(age)]);
    }
}

auto json = JSONValue(people.map!(p => p.toJson).array);
writeln(json);

I use the data module from vibe.d, which supports data serialization:

http://code.dlang.org/packages/vibe-d%3Adata
https://vibed.org/api/vibe.data.json/

struct Person
{
    string name;
    int age;
}

auto json = people.serializeToJson();
writeln(json);
June 25, 2019
On Tuesday, 25 June 2019 at 05:33:57 UTC, mark wrote:
> I have the following array of structs:
>
> struct Person {
>     string name;
>     int age;
> };
>
>
> Person people[];
> Person p;
>
> Person p1 = {
>     "Bob",
>     12
> };
>
> Person p2 = {
>     "Bob",
>     12
> };
>
> people ~= p1;
> people ~= p2;
>
> I've read through the std.json documentation, but I'm still confused as to how to create an array of objects, especially if the objects are structs.
>
> I've included this:
>
> JSONValue jj;
> jj["nodes"].array = [];
>
> But I get the error: JSONValue is not an object. Ideally, I'd like to have a json output that looks like this:
>
> [
>
>     {
>         "name": "Bob",
>         "age": 12
>     },
>     {
>         "name": "Bob",
>         "age": 12
>     },
> ]
>
> Any help would be greatly appreciated.

use hunt library:

https://github.com/huntlabs/hunt


Sample code:

import hunt.serialize;

void main()
{
    auto json = toJson(pepole);
}