[sage-support] json and sage

2010-02-19 Thread jpc
Python json library is of simple use but there's this problem:

sage: import json
sage: json.dumps( {a: 1.8} )
...produce an error because:
sage: x = 1.8
sage: type(x)
type 'sage.rings.real_mpfr.RealLiteral'

Should one do the type cast below for every int or float when using
sage ?

sage: type(float(x))
type 'float'
sage: json.dumps( {a: float(x)} )
'{a: 1.8}'

Any easier way ?

Thank you,
Pedro

-- 
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org


Re: [sage-support] json and sage

2010-02-19 Thread Mike Hansen
On Fri, Feb 19, 2010 at 3:52 PM, jpc pedrocruzave...@gmail.com wrote:

 Should one do the type cast below for every int or float when using
 sage ?

 sage: type(float(x))
 type 'float'
 sage: json.dumps( {a: float(x)} )
 '{a: 1.8}'

 Any easier way ?

You write a custom encoder to do this:

http://docs.python.org/library/json.html#encoders-and-decoders

--Mike

-- 
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org