jonathanstowe / JSON-Class

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

Custom class type objects (undefined) fail round trip conversion / can't be serialized #6

Closed lefth closed 6 years ago

lefth commented 6 years ago

Builtin (undefined) types are serialized as null. This works well:

> (Int but JSON::Class).to-json
null
> (Str but JSON::Class).to-json
null
> (Int but JSON::Class).from-json: (Int but JSON::Class).to-json;
(Int+{Class})

Custom classes should be serialized the same way. Instead, they're serialized the same way as a defined class, and this either causes round-trip conversion failure, or an exception, depending on whether the class has members:

> class C does JSON::Class { }
> C.to-json;
{
}
> C.from-json: C.to-json;
C.new
> class D does JSON::Class { has $.i; }
> D.to-json;
Cannot look up attributes in a D type object
  in block  at D:\rakudo-2017.12\share\perl6\site\sources\0D5D5ABC9DC9B8DB1F75571789AD2F8F5FD3487A (JSON::Marshal) line 143
  in sub _marshal at D:\rakudo-2017.12\share\perl6\site\sources\0D5D5ABC9DC9B8DB1F75571789AD2F8F5FD3487A (JSON::Marshal) line 132
  in sub marshal at D:\rakudo-2017.12\share\perl6\site\sources\0D5D5ABC9DC9B8DB1F75571789AD2F8F5FD3487A (JSON::Marshal) line 172
  in method to-json at D:\rakudo-2017.12\share\perl6\site\sources\AEC54816C1D35CBEB0033396AD685FA76A800D6E (JSON::Class) line 94
  in block <unit> at <unknown file> line 1
lefth commented 6 years ago

I moved this issue to JSON::Marshal: https://github.com/jonathanstowe/JSON-Marshal/issues/8