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
Conversation
Hi, |
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 ( 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 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
@jskorpan: I just rebased and all unit tests are passing now. Thanks for reverting that last commit! |
Should this be merged? |
Thanks for your interest in the project, I’ll look into this shortly From: Omer Katz [mailto:notifications@github.com] Should this be merged? — |
This would be a huge help. I would really like to use ujson but I need to dates to be returned in .isoformat() |
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. |
This should be covered somewhat by the |
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 (
{}
).__unicode__
function. If an object defines,
__unicode__
the library should probablyserialize the object as a utf-8 string.