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

Is there a way to deserialise without knowing the class? #10

Open FCO opened 2 years ago

FCO commented 2 years ago

Sorry, it’s a question rather than an issue, but is there a way of deserializing without knowing the class? Maybe a way to add the class name on the serialised json to make it possible to find out what class to use…

jonathanstowe commented 2 years ago

I've thought about this before, my difficulty has been that there is no firm specification for this kind of metadata in JSON (unlike e.g. XML namespaces,) and JSON has typically depended on contextual or out-of-band metadata (e.g. media type or special HTTP headers,) for this kind of thing. If one was using Cro::HTTP then it would be possible to create a specialised BodyParser that could map the out-of-band metadata to a particular type to deserialize the JSON to.

If there were to be some theoretical specification for in-band JSON metadata like this (something like JSON-LD for instance, ) then I would see the implementation as sitting on top of JSON::Class as a separate module rather than as part of it as the specification is likely to move more quickly than the underlying requirement to deserialise JSON to a known type.