michalmuskala / jason

A blazing fast JSON parser and generator in pure Elixir.
Other
1.61k stars 170 forks source link

Add ability to preserve the __struct__ field when encoding / decoding #43

Closed ghost closed 6 years ago

ghost commented 6 years ago

When encoding or decoding a struct it would be useful to add the ability to preserve the struct field so that it is not lost. Right now I have to decode then go back through and put the struct field back in and fix the data because it is lost.

evuez commented 6 years ago

Correct me if I'm wrong but it seems to me you should use @derive or Protocol.derive/3 on you struct for this (https://github.com/michalmuskala/jason/blob/master/README.md#differences-to-poison).

michalmuskala commented 6 years ago

Yes, it's possible to encode the __struct__ field by manually implementing the encoder. It's even possible to use @derive Jason.Encoder, only: [:__struct__, ...] to explicitly request the field to be encoded. The default when deriving the protocol without any options will remain to omit the field.