Open FCO opened 3 years ago
Hi,
looking at the JSON I think you don't need that to be a hash rather a Positional
there:
use JSON::Class;
class TestObject does JSON::Class {
has Str $.string;
}
class Bla does JSON::Class {
has TestObject @.aaa;
}
my $json = q<{"aaa": [{ "string" : "one" }, { "string" : "two" }]}>;
say Bla.from-json: $json
If you actually want to deserialise a bare Hash containing an array of objects then you have to go one step further:
use JSON::Class;
class TestObject does JSON::Class {
has Str $.string;
}
constant TestObjects := (Array[TestObject] but JSON::Class);
constant Bla := (Hash[TestObjects] but JSON::Class);
my $json = q<{"aaa": [{ "string" : "one" }, { "string" : "two" }]}>;
say Bla.from-json: $json
Which gives you:
{aaa => [TestObject.new(string => "one") TestObject.new(string => "two")]}
Is there someway of doing something like this?