[ujson] Support for serializing arbitrary types #130

Open
wants to merge 1 commit into
from

Projects

None yet

5 participants

@mvismonte
  • Previously, any arbitrary python object that wasn't a dict, int, string, etc.
    would be considered a dict/object. The library would try to call the toDict
    function to find the object's dict value. If no toDict function is found,
    the object will be serialized as an empty JSON object ({}).
  • This change will add a fallback that calls a python object's __unicode__
    function. If an object defines, __unicode__ the library should probably
    serialize the object as a utf-8 string.
  • Added unit tests for regular and error handling cases
  • Ran guppy in tests/tests.py and confirmed there are no memory leaks
@jskorpan
Member

Hi,
Thanks for this pull request. Is there a more specific use case to when this may be useful?

@jskorpan jskorpan closed this Apr 14, 2014
@mvismonte

Hi @jskorpan,

Sure, no problem. This pull request will allow developers to serialize any arbitrary object to a string instead of an empty JSON object (currently the default). There are many times where developers will use classes/objects to wrap a string so they can add additional functionality, but the representation of the object is a string.

One example is the Django Lazy Translation object: https://docs.djangoproject.com/en/dev/topics/i18n/translation/#lazy-translation. This object wraps a string and is meant to represent the translated value of a string. Currently, the serialized value of a lazy translation instance is an empty JSON object ('{}'). To me, this seems that the library is silently failing to compute a JSON value for the objects and seems broken (especially since most objects don't have a toDict function).

The long term solution is to eventually support customer encoders as #124 suggests; however, it seems that the feature isn't being worked on currently and is far from completion (please, correct me if I'm wrong).

Anyways, the pull request will add functionality that will use an object's __unicode__ function to serialize the object to JSON if that function is defined. It's supplemental to the toDict function lookup that is already implemented in ujson. This solution will allow developers to specify string values for their objects instead of just dict values, which is actually a pretty common use case (at least, it seems like it from issued I've looked through). If you have any other questions, let me know.

Thanks!

@jskorpan jskorpan reopened this Apr 15, 2014
@mvismonte mvismonte referenced this pull request Apr 16, 2014
Jonas Tarnstrom Minor performance optimization 510a8fc
@mvismonte mvismonte [ujson] Added support for seralizing objects to JSON
- This is accomplished by calling the __unicode__ function on an object.
- Added unit tests to test this change.  It also seems that this should not be
  leaking memory according to the test in tests/tests.py
6bc3b81
@mvismonte

@jskorpan: I just rebased and all unit tests are passing now. Thanks for reverting that last commit!

@thedrow
thedrow commented May 25, 2014

Should this be merged?

@jskorpan
Member

Thanks for your interest in the project, I’ll look into this shortly

From: Omer Katz [mailto:notifications@github.com]
Sent: den 25 maj 2014 09:37
To: esnme/ultrajson
Cc: Jonas Tärnström
Subject: Re: [ultrajson] [ujson] Support for serializing arbitrary types (#130)

Should this be merged?


Reply to this email directly or view it on GitHubhttps://github.com/esnme/ultrajson/pull/130#issuecomment-44121453.

@thedrow
thedrow commented May 26, 2014

@jskorpan I need this for the Encoder/Decoder API (see #124).

@brettatoms

This would be a huge help. I would really like to use ujson but I need to dates to be returned in .isoformat()

@mitar
mitar commented Dec 19, 2014

Hm, that is interesting, but I would need something like this, but just that the object can return raw JSON. I have situations where I already have a JSON from somewhere else and I would like to include it directly, instead of having to first deserialize it to be able to serialize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment