jonathanstowe / JSON-Class

A Role to allow Raku objects to be constructed and serialised from/to JSON.
Artistic License 2.0
6 stars 6 forks source link

No way to deal with top-level arrays #2

Closed treyharris closed 5 years ago

treyharris commented 8 years ago

Please see the gist at https://gist.github.com/treyharris/7b57dd3855d3afb9461d86c13938362f

I'm using JSON::Infer, but the issue originates with JSON::Class. There's no way for me to parse this file. Perhaps something as simple as

my Something @something = Something.from-json($contents);

?

jonathanstowe commented 8 years ago

It's actually JSON::Unmarshal that needs to altered, everything is in there to do it, it just needs to know that the data is an array of the objects it wants to unmarshal not a single one.

jonathanstowe commented 8 years ago

Hi, I've just done some better testing and actually discovered that JSON::Unmarshal doesn't need to be changed, it's just the other bits need to be able to deal with the Array case. That is if you tell it you want a an Array[Object] it does the right thing, however JSON::Class won't like that because the array returned won't be a JSON::Class, so I'm going to fix that (easy) and then fix JSON::Infer to generate some code like :

package Homebrew {
    our constant Formulas = (Array[Homebrew::Formula] but JSON::Class);
}

And it'll all be good. I'll make the change to JSON::Class first so you can actually use the above rather than my original workaround, then do the actual generating of the above in JSON::Infer if the input data is an array.

jonathanstowe commented 8 years ago

I've just update JSON::Class to be able to deal with this nicely, so next time you update it should work as above. If you want to serialise the same objects then you will also need to update JSON::Marshal