Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ujson] Support for serializing arbitrary types #130

Closed
wants to merge 1 commit into from

Conversation

mvismonte
Copy link
Contributor

  • 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
Copy link

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
Copy link
Contributor Author

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!

- 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
@mvismonte
Copy link
Contributor Author

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

@thedrow
Copy link

thedrow commented May 25, 2014

Should this be merged?

@jskorpan
Copy link

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//pull/130#issuecomment-44121453.

@thedrow
Copy link

thedrow commented May 26, 2014

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

@brettatoms
Copy link

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

@mitar
Copy link
Contributor

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.

@Jahaja
Copy link
Contributor

Jahaja commented Feb 5, 2017

This should be covered somewhat by the __json__ merge and even more if we were to add default fallback functionality, thus I'm closing this one.

@Jahaja Jahaja closed this Feb 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants