Re: jsonify Decimals and datetime.date
On Thu, Apr 30, 2009 at 03:27:10PM -0700, Philip Jenvey wrote: Unfortunately our @jsonify just sucks in that you can't specify args to dumps. Otherwise this would be even slightly easier than Domhnall's example: # Don't even need a subclass def myenc(d): if if isinstance(d, decimal.Decimal): return float(str(d) # etc raise TypeError() simplejson.dumps(data, default=myenc) We need to fix jsonify -- what's a little problematic is it's built to take no arguments, which is significantly different from a decorator that's built to take arguments. We may be able to make the decorator work both ways for 0.10 but it'd tricky/annoying. It's tricky, but not too tricky, if you only accept keyword arguments in to @jsonify(): def jsonify(fn=None, arg1=value1, arg2=value2): if fn is None: # this is @jsonify(arg1=...), so return a decorator return functools.partial(jsonify, arg1=arg1, arg2=arg2) @functools.wraps def wrapped(*a, **kw): # do stuff retval = fn(*a, **kw) return simplejson.dumps(retval, arg1=arg1, arg2=arg2) return wrapped Use it as @jsonify def fn(...): @jsonify() def fn(...): @jsonify(arg1=42) def fn(...): but don't try @jsonify(42) def fn(...): Pylons 1.0 could definitely break it, however: @jsonify - @jsonify() Marius Gedminas -- There is nothing more practical than a good theory. -- James Clerk Maxwell signature.asc Description: Digital signature
Re: jsonify Decimals and datetime.date
I ended up moving the issue out of the jsonified method as Domhnall suggested which is working nicely and may have some extra benefits. Many thanks to all. Domhnall Walsh wrote: Well, maybe you should consider processing the tuple you plan to return somehow to change the datetimes and Decimals to something more palatable to @jsonify before returning it (via the decorator). If your controller action is only returning JSON, then this shouldn't be a problem? Domhnall. 2009/4/30 Uwe Feldtmann u...@microshare.com.au mailto:u...@microshare.com.au Thanks Domhnall. Not sure if it will help as I'm using the jsonify decorator on a method in the controller and it is what is failing when attempting to wrap the result set. I managed to get the dates to work out by simply checking for data type and then assigning in the controller. Decimals on the other hand are not working for me. There doesn't appear to be a way to extract the value of a Decimal into another variable without copying the entire object. Any suggestions or pointers greatly appreciated. Domhnall Walsh wrote: HI: This might give you a start: import simplejson class FancyEncoder(simplejson.JSONEncoder): Handle datetime objects. def default(self, obj): if isinstance(obj, datetime.datetime): return obj.isoformat() # Add more type-specific handlers here, for example for Decimals return simplejson.JSONEncoder.default(self, obj) json = simplejson.dumps(data_to_encode, cls=FancyEncoder) As you can see it replaces the datetimes with their ISO format representations as strings. Hope this helps, Domhnall. 2009/4/30 Uwe Feldtmann u...@microshare.com.au mailto:u...@microshare.com.au Hi all. I've been struggling with a controller that gets a result set from a database and should simply return that result set as json data and I keep getting this traceback ||TypeError: Decimal(0.00) is not JSON serializable the data returned from the db is: (1492, 'BAKER', 'MR. AND MRS BAKER', Decimal(843.50), Decimal(5000.00), Decimal(0.00), datetime.date(1998, 8, 24), datetime.date(1998, 8, 31)) Is this an issue with @jsonify or am I missing something? I am using simplejson-2.0.8 and py2.5 Thanks in advance. -- Quidquid latine dictum sit, altum sonatur. (Whatever is said in Latin sounds profound) -- Quidquid latine dictum sit, altum sonatur. (Whatever is said in Latin sounds profound) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-discuss@googlegroups.com To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en -~--~~~~--~~--~--~---
Re: jsonify Decimals and datetime.date
On May 1, 2009, at 12:45 AM, Marius Gedminas wrote: It's tricky, but not too tricky, if you only accept keyword arguments in to @jsonify(): Yep, but I definitely don't want to keep it working both ways going forward (1.0). It'd be a good stopgap. def jsonify(fn=None, arg1=value1, arg2=value2): if fn is None: # this is @jsonify(arg1=...), so return a decorator return functools.partial(jsonify, arg1=arg1, arg2=arg2) @functools.wraps def wrapped(*a, **kw): # do stuff retval = fn(*a, **kw) return simplejson.dumps(retval, arg1=arg1, arg2=arg2) return wrapped functools.wraps here isn't enough because it doesn't maintain the method argspec -- we need the decorator module magic. Pylons needs the argspec maintained to know what routing args to pass along. -- Philip Jenvey --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-discuss@googlegroups.com To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en -~--~~~~--~~--~--~---
Re: jsonify Decimals and datetime.date
I ended up moving the issue out of the jsonified method, as Domhnall suggested, which is working nicely and may have some extra benefits. Needless to say I'll be looking closely at jsonify as we move closer to 1.0. Many thanks to all. On May 2, 9:10 am, Philip Jenvey pjen...@underboss.org wrote: On May 1, 2009, at 12:45 AM, Marius Gedminas wrote: It's tricky, but not too tricky, if you only accept keyword arguments in to @jsonify(): Yep, but I definitely don't want to keep it working both ways going forward (1.0). It'd be a good stopgap. def jsonify(fn=None, arg1=value1, arg2=value2): if fn is None: # this is @jsonify(arg1=...), so return a decorator return functools.partial(jsonify, arg1=arg1, arg2=arg2) �...@functools.wraps def wrapped(*a, **kw): # do stuff retval = fn(*a, **kw) return simplejson.dumps(retval, arg1=arg1, arg2=arg2) return wrapped functools.wraps here isn't enough because it doesn't maintain the method argspec -- we need the decorator module magic. Pylons needs the argspec maintained to know what routing args to pass along. -- Philip Jenvey --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-discuss@googlegroups.com To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en -~--~~~~--~~--~--~---
Re: jsonify Decimals and datetime.date
HI: This might give you a start: import simplejson class FancyEncoder(simplejson.JSONEncoder): Handle datetime objects. def default(self, obj): if isinstance(obj, datetime.datetime): return obj.isoformat() # Add more type-specific handlers here, for example for Decimals return simplejson.JSONEncoder.default(self, obj) json = simplejson.dumps(data_to_encode, cls=FancyEncoder) As you can see it replaces the datetimes with their ISO format representations as strings. Hope this helps, Domhnall. 2009/4/30 Uwe Feldtmann u...@microshare.com.au Hi all. I've been struggling with a controller that gets a result set from a database and should simply return that result set as json data and I keep getting this traceback TypeError: Decimal(0.00) is not JSON serializable the data returned from the db is: (1492, 'BAKER', 'MR. AND MRS BAKER', Decimal(843.50), Decimal(5000.00), Decimal(0.00), datetime.date(1998, 8, 24), datetime.date(1998, 8, 31)) Is this an issue with @jsonify or am I missing something? I am using simplejson-2.0.8 and py2.5 Thanks in advance. -- Quidquid latine dictum sit, altum sonatur. (Whatever is said in Latin sounds profound) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-discuss@googlegroups.com To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en -~--~~~~--~~--~--~---
jsonify Decimals and datetime.date
Hi all. I've been struggling with a controller that gets a result set from a database and should simply return that result set as json data and I keep getting this traceback ||TypeError: Decimal(0.00) is not JSON serializable the data returned from the db is: (1492, 'BAKER', 'MR. AND MRS BAKER', Decimal(843.50), Decimal(5000.00), Decimal(0.00), datetime.date(1998, 8, 24), datetime.date(1998, 8, 31)) Is this an issue with @jsonify or am I missing something? I am using simplejson-2.0.8 and py2.5 Thanks in advance. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-discuss@googlegroups.com To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en -~--~~~~--~~--~--~---
Re: jsonify Decimals and datetime.date
Thanks Domhnall. Not sure if it will help as I'm using the jsonify decorator on a method in the controller and it is what is failing when attempting to wrap the result set. I managed to get the dates to work out by simply checking for data type and then assigning in the controller. Decimals on the other hand are not working for me. There doesn't appear to be a way to extract the value of a Decimal into another variable without copying the entire object. Any suggestions or pointers greatly appreciated. Domhnall Walsh wrote: HI: This might give you a start: import simplejson class FancyEncoder(simplejson.JSONEncoder): Handle datetime objects. def default(self, obj): if isinstance(obj, datetime.datetime): return obj.isoformat() # Add more type-specific handlers here, for example for Decimals return simplejson.JSONEncoder.default(self, obj) json = simplejson.dumps(data_to_encode, cls=FancyEncoder) As you can see it replaces the datetimes with their ISO format representations as strings. Hope this helps, Domhnall. 2009/4/30 Uwe Feldtmann u...@microshare.com.au mailto:u...@microshare.com.au Hi all. I've been struggling with a controller that gets a result set from a database and should simply return that result set as json data and I keep getting this traceback ||TypeError: Decimal(0.00) is not JSON serializable the data returned from the db is: (1492, 'BAKER', 'MR. AND MRS BAKER', Decimal(843.50), Decimal(5000.00), Decimal(0.00), datetime.date(1998, 8, 24), datetime.date(1998, 8, 31)) Is this an issue with @jsonify or am I missing something? I am using simplejson-2.0.8 and py2.5 Thanks in advance. -- Quidquid latine dictum sit, altum sonatur. (Whatever is said in Latin sounds profound) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-discuss@googlegroups.com To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en -~--~~~~--~~--~--~---
Re: jsonify Decimals and datetime.date
Well, maybe you should consider processing the tuple you plan to return somehow to change the datetimes and Decimals to something more palatable to @jsonify before returning it (via the decorator). If your controller action is only returning JSON, then this shouldn't be a problem? Domhnall. 2009/4/30 Uwe Feldtmann u...@microshare.com.au Thanks Domhnall. Not sure if it will help as I'm using the jsonify decorator on a method in the controller and it is what is failing when attempting to wrap the result set. I managed to get the dates to work out by simply checking for data type and then assigning in the controller. Decimals on the other hand are not working for me. There doesn't appear to be a way to extract the value of a Decimal into another variable without copying the entire object. Any suggestions or pointers greatly appreciated. Domhnall Walsh wrote: HI: This might give you a start: import simplejson class FancyEncoder(simplejson.JSONEncoder): Handle datetime objects. def default(self, obj): if isinstance(obj, datetime.datetime): return obj.isoformat() # Add more type-specific handlers here, for example for Decimals return simplejson.JSONEncoder.default(self, obj) json = simplejson.dumps(data_to_encode, cls=FancyEncoder) As you can see it replaces the datetimes with their ISO format representations as strings. Hope this helps, Domhnall. 2009/4/30 Uwe Feldtmann u...@microshare.com.au Hi all. I've been struggling with a controller that gets a result set from a database and should simply return that result set as json data and I keep getting this traceback TypeError: Decimal(0.00) is not JSON serializable the data returned from the db is: (1492, 'BAKER', 'MR. AND MRS BAKER', Decimal(843.50), Decimal(5000.00), Decimal(0.00), datetime.date(1998, 8, 24), datetime.date(1998, 8, 31)) Is this an issue with @jsonify or am I missing something? I am using simplejson-2.0.8 and py2.5 Thanks in advance. -- Quidquid latine dictum sit, altum sonatur. (Whatever is said in Latin sounds profound) -- Quidquid latine dictum sit, altum sonatur. (Whatever is said in Latin sounds profound) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-discuss@googlegroups.com To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en -~--~~~~--~~--~--~---
Re: jsonify Decimals and datetime.date
On Apr 30, 2009, at 5:13 AM, Uwe Feldtmann wrote: Thanks Domhnall. Not sure if it will help as I'm using the jsonify decorator on a method in the controller and it is what is failing when attempting to wrap the result set. Unfortunately our @jsonify just sucks in that you can't specify args to dumps. Otherwise this would be even slightly easier than Domhnall's example: # Don't even need a subclass def myenc(d): if if isinstance(d, decimal.Decimal): return float(str(d) # etc raise TypeError() simplejson.dumps(data, default=myenc) We need to fix jsonify -- what's a little problematic is it's built to take no arguments, which is significantly different from a decorator that's built to take arguments. We may be able to make the decorator work both ways for 0.10 but it'd tricky/annoying. Pylons 1.0 could definitely break it, however: @jsonify - @jsonify() -- Philip Jenvey --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups pylons-discuss group. To post to this group, send email to pylons-discuss@googlegroups.com To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en -~--~~~~--~~--~--~---