On Wed, 6 Jan 2016 10:27:01 -0500 Michael Manfre <mman...@gmail.com> wrote:
> On Wed, Jan 6, 2016 at 9:58 AM, Tom Christie <christie....@gmail.com> wrote: > > > Customizing the encoder (or even using DjangoJSONEncoder by default) isn't > > so bad. > > > > I'm less convinced about the usefulness of customizing the decoder - once > > you've encoded the data into JSON any additional type information is lost, > > so casting back to python primitives is always going to need to be handled > > explicitly. > > > > Whether or not a configurable decoder seems useful for the situations we > can think of, only allowing users to configure half of the encode/decode > cycle seems odd to me. Allowing both to be configurable requires a trivial > amount of extra effort to implement and maintain. > > Regards, > Michael Manfre > Indeed, this is only helpful if the user is able to customize both encoding and decoding of its messages. For instance, on one of our projects, we use a custom serializer that encodes complex types as {"__type__": "foo", "value": "bar"} dicts. Our deserializer can then handle the decoding of such fields to a native Python datastructure. This is possible with "jsonfield" (https://pypi.python.org/pypi/jsonfield) which allows to set a custom encoder/decoder. Coming back to Aymeric's question, I'd love to be able to set a custom decoder/encoder for a JSONField. Being able to only specify the encoding is error-prone, since my code will then have to handle the distinction between "this is a value set pre-save, so it's in a native type" and "this time, it's from the database, so I have to cast it back". Regards, -- Raphaël Barrois -- You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To post to this group, send email to django-developers@googlegroups.com. Visit this group at https://groups.google.com/group/django-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/20160106173748.43400622%40ithor. For more options, visit https://groups.google.com/d/optout.