-
Notifications
You must be signed in to change notification settings - Fork 150
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
check the __json__ magic method before serialising the object #116
Conversation
Hi @rach, I'm not sure to completely get what your proposition is. You already can put the renderer you want with cornice. |
It's more about coherency with the default json renderer from pyramid.
I found a bit weird that the same behaviour is not respected. Now because I didn't wanted to change the renderer on all my Service so I'm having:
I guess that I'm missing the point why Cornice is creating its own renderer and is not reusing the one of pyramid. |
Hi @ametaireau Just to resume, it's suggestion for an improvement and not a bug at all. Feel free to close this issue, if it's out the scope . |
I thnk we should use the default JSON serializer, you're right, if it handles datetime serialisation. |
The default serializer is very nice and have a method I have some code already done at home, I can add a pull request later on. I realize that I wasn't very clear at the start. |
Pyramid has a json renderer which allow to precis the serializer. This allow to use renderer adapter to add behavior when some types are detected plus the check of the __json__ method on returned object. See #116
@rach Looks like you need to update the tests to check for |
The test was already broken.
|
yeah that's unrelated to this patch. |
|
||
|
||
__all__ = ['json_renderer', 'to_list', 'json_error', 'match_accept_header', | ||
__all__ = ['json_datetime_adapter', 'json_decimal_adapter', 'json_renderer', 'to_list', 'json_error', 'match_accept_header', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we really want to expose these?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you want, happy to remove it.
It's reusable but maybe not useful to expose it.
I had initially exposed it because I was building the renderer in the
init
On Thu, Feb 21, 2013 at 2:46 PM, Alexis Metaireau
[email protected]:
In cornice/util.py:
-all = ['json_renderer', 'to_list', 'json_error', 'match_accept_header',
+all = ['json_datetime_adapter', 'json_decimal_adapter', 'json_renderer', 'to_list', 'json_error', 'match_accept_header',do we really want to expose these?
—
Reply to this email directly or view it on GitHubhttps://github.com//pull/116/files#r3099381.
Rach Belaid
Director at Iron Braces Ltd
Flat 9 240B Amhurst Road, London N16 7UL
Phone : +44 75 008 660 84
VAT : 114 2764 36
I removed the exposed adapter in the case that it's not use anywhere else |
Maybe worth adding few words in the doc about the json method that can be implemented on object returned for helping the serialization |
Yep that would be useful to point this out, agreed. |
Actually, we in the mean time introduced some behaviour which depends on having a custom JSON Serializer (which returns the good Content-Type, using the request "accept" headers). So in its current state, it's not possible for me to merge this, because of this particular behaviour. Do you think this could be possible? |
Hi, I can imagine that can be very useful for building services. I guess that you refer to this line? https://github.com/mozilla-services/cornice/blob/master/cornice/util.py#L21 And then change the conten-type via the I'm not familiar with all the cornice codebase but I noticed that you are Now I don't know what happen when you change the response.content-type Need to be tested. On Sun, Mar 3, 2013 at 9:29 PM, Alexis Metaireau
Rach Belaid |
Sorry to come on top of your contribution, but it seems appropriate (and related) to discuss date/datetime JSON serialization here :) Is there any working solution ? Stacktrace is here : https://travis-ci.org/spiral-project/daybed/jobs/9473751#L420 I tried this, without success :
+# renderers.py
+import json
+from datetime import date, datetime
+from decimal import Decimal
+
+from pyramid.renderers import JSONP as PyramidJSONP
+
+
+def json_datetime_adapter(obj, request):
+ return obj.isoformat()
+
+
+def json_decimal_adapter(obj, request):
+ return json.dumps(obj, use_decimal=True)
+
+
+class JSONP(PyramidJSONP):
+ def __init__(self, *args, **kwargs):
+ super(JSONP, self).__init__(*args, **kwargs)
+ self.add_adapter(date, json_datetime_adapter)
+ self.add_adapter(datetime, json_datetime_adapter)
+ self.add_adapter(Decimal, json_decimal_adapter)
Thanks ! |
@leplatrem: the error_handler, that is called when a validation added a error to I was suprised by this, too. But this is probably an issue for itself. |
Thanks for your reply ! My issue was precisely due to a couchdb storage somewhere else in the code :) But your message helped me to understand some cornice mechanics, since I was first focused on error handling :) |
To be more compliant with the current implementation of Pyramid:
http://docs.pylonsproject.org/projects/pyramid/en/master/narr/renderers.html#serializing-custom-objects
https://github.com/Pylons/pyramid/blob/master/pyramid/renderers.py#L263
It will be great if cornice could support the json method to makes life easier.
By the way, couldn't you reuse the Json Renderer from Pyramid and specify you own
serializer
https://github.com/Pylons/pyramid/blob/master/pyramid/renderers.py#L263