Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hash of Array of JSON::Array #8

Open
FCO opened this issue Dec 19, 2020 · 1 comment
Open

Hash of Array of JSON::Array #8

FCO opened this issue Dec 19, 2020 · 1 comment

Comments

@FCO
Copy link

FCO commented Dec 19, 2020

Is there someway of doing something like this?

raku -MJSON::Class -e '

class TestObject does JSON::Class {
    has Str $.string;
}

constant TestObjects = (Array[TestObject] but JSON::Class);


class Bla does JSON::Class {
    has TestObjects %.aaa;
}



my $json = q<{"aaa": [{ "string" : "one" }, { "string" : "two" }]}>;


say Bla.from-json: $json



'
Type check failed in assignment to %!aaa; expected Array[TestObject]+{JSON::Class} but got Array ([TestObject.new(stri...)
  in sub _unmarshal at /Users/fernando/.rakubrew/versions/moar-2020.11/share/perl6/site/sources/BD58585C8BB103CC821AB89EFD8D30DA4FB8FDF9 (JSON::Unmarshal) line 112
  in sub unmarshal at /Users/fernando/.rakubrew/versions/moar-2020.11/share/perl6/site/sources/BD58585C8BB103CC821AB89EFD8D30DA4FB8FDF9 (JSON::Unmarshal) line 158
  in method from-json at /Users/fernando/.rakubrew/versions/moar-2020.11/share/perl6/site/sources/C0029D661A8CA443DB83A67FE58F3E10D590C1A0 (JSON::Class) line 91
  in block <unit> at -e line 19
@jonathanstowe
Copy link
Owner

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")]}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants