tadzik / JSON-Unmarshal

MIT License
5 stars 9 forks source link

In a class, 'null' is unmarshalled as Mu instead of Any #29

Open lefth opened 6 years ago

lefth commented 6 years ago

I know round trip conversion can't be perfect because Perl 6 has more null types than can be represented in JSON, but I suggest that null should mean Any whenever there's no other constraint, since this is the default Perl 6 type. (A variable with no constraint or value will be Any.)

> class MyClass { has $.foo; } # MyClass.new will serialize as '{"foo": null}'
(MyClass)
> unmarshal('{"foo": null}', MyClass);
MyClass.new(foo => Mu) # should be MyClass.new(foo => Any), equivalent to MyClass.new()

Interestingly, this already works if the variable has an Any type constraint on the variable (which I would have expected to have no significance):

> class HasAny { has Any $.foo; }
(HasAny)
> unmarshal('{"foo": null}', HasAny);
HasAny.new(foo => Any)