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

Error using hash with different key type #3

Closed FCO closed 7 years ago

FCO commented 7 years ago

class C is JSON::Class {has Str %.bla{subset :: of Str where any }}; C.from-json: 「{"bla": {"ble": "bli"}}」

jonathanstowe commented 7 years ago

Hi, can you clarify where the error is?

The error I get from the actual code you give is a compiler error todo with the definition of the subset:

===SORRY!=== Error while compiling /home/jonathan/devel/perl6/jc
Function "any" may not be called without arguments (please use () or whitespace to denote arguments, or &any to refer to the function as a noun, or use .any if you meant to call it as a method on $_)
jonathanstowe commented 7 years ago

Adjusting the code to a properly formed any :+1:

use JSON::Class;

class C is JSON::Class {
    has Str %.bla{subset :: of Str where any("ble", "blob")}
}; 

C.from-json: 「{"bla": {"ble": "bli"}}」

I get :

No appropriate parametric role variant available for 'Associative'
  in sub _unmarshal at /home/jonathan/.rakudobrew/moar-nom/install/share/perl6/site/sources/BD58585C8BB103CC821AB89EFD8D30DA4FB8FDF9 (JSON::Unmarshal) line 127
  in block  at /home/jonathan/.rakudobrew/moar-nom/install/share/perl6/site/sources/BD58585C8BB103CC821AB89EFD8D30DA4FB8FDF9 (JSON::Unmarshal) line 108
  in sub _unmarshal at /home/jonathan/.rakudobrew/moar-nom/install/share/perl6/site/sources/BD58585C8BB103CC821AB89EFD8D30DA4FB8FDF9 (JSON::Unmarshal) line 92
  in sub unmarshal at /home/jonathan/.rakudobrew/moar-nom/install/share/perl6/site/sources/BD58585C8BB103CC821AB89EFD8D30DA4FB8FDF9 (JSON::Unmarshal) line 157
  in method from-json at /home/jonathan/.rakudobrew/moar-nom/install/share/perl6/site/sources/F41A067DB59AED3AC154606CDF116F5EA645DB3D (JSON::Class) line 87
  in block <unit> at jc line 7

Which is more understandable but points to a problem wth https://github.com/tadzik/JSON-Unmarshal - I'll make a ticket there and link it here.

jonathanstowe commented 7 years ago

I'm just making a small patch to rakudo to fix this