amnaredo / test

0 stars 0 forks source link

Customizable discrimination annotations #109

Open amnaredo opened 3 years ago

amnaredo commented 3 years ago

It would be nice if annotation format could be customized, for example, in order to have

{ "$variant": "MyClass", "key": "value" }

instead of

["your.package.MyClass", {"key": "value"}]

While such approach can lead to collisions, with some fancy discriminator key it will be very unlikely (and it is not really a problem if it can be configured). It would also be nice to be able to store only the last part of the discriminator value (or, for example, two parts if MyClass is defined in an object).

As far as I can see (I may be wrong, though) it should not be very difficult - currently the autogenerated picklers depend on Internal.annotate behavior; I think it should be possible to extract the annotation behavior to another implicit and provide the default one with the current behavior.

Having the discriminator as a part of the object is important (at least for our project) for migration reasons (a lot of other serializators are using this format) and for working with the serialized objects - for example, mongodb queries tend to be simpler without arrays.

ID: 64 Original Author: netvl

amnaredo commented 3 years ago

Yeah, I want to do this too. It'd make interoperating with other json producers/consumers much easier since afaict nobody else does my tagged-union-as-array thing

On Fri, Jan 23, 2015 at 1:02 PM, Vladimir Matveev notifications@github.com wrote:

It would be nice if annotation format could be customized, for example, in order to have

{ "$variant": "MyClass", "key": "value" }

instead of

["your.package.MyClass", {"key": "value"}]

While such approach can lead to collisions, with some fancy discriminator key it will be very unlikely (and it is not really a problem if it can be configured). It would also be nice to be able to store only the last part of the discriminator value (or, for example, two parts if MyClass is defined in an object).

As far as I can see (I may be wrong, though) it should not be very difficult - currently the autogenerated picklers depend on Internal.annotate behavior; I think it should be possible to extract the annotation behavior to another implicit and provide the default one with the current behavior.

Having the discriminator as a part of the object is important (at least for our project) for migration reasons (a lot of other serializators are using this format) and for working with the serialized objects - for example, mongodb queries tend to be simpler without arrays.

— Reply to this email directly or view it on GitHub https://github.com/lihaoyi/upickle/issues/64.

Original Author: lihaoyi

amnaredo commented 3 years ago

Closing in preference for https://github.com/lihaoyi/upickle/issues/55

Original Author: lihaoyi