asolfre / appengine-rest-server

Automatically exported from code.google.com/p/appengine-rest-server
Other
0 stars 0 forks source link

All numbers are represented as strings in JSON output #32

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create a data model with some numbers, 
    SomeInt = db.IntegerProperty(default=0)
    SomeFloat = db.FloatProperty(default=-1.0)
2. Implement the rest services

3. Perform a Get and request JSON output. 

What is the expected output? What do you see instead?
EXPECTED:
"EntityName":
{
    "SomeInt": 1
    "SomeFloat": 1.0
}

RECEIVED:
"EntityName":
{
    "SomeInt": "1"
    "SomeFloat": "1.0"
} 

What version of the product are you using? On what operating system?
1.0.5

Please provide any additional information below.

Original issue reported on code.google.com by redrocke...@gmail.com on 17 Jun 2011 at 9:37

GoogleCodeExporter commented 9 years ago
unfortunately, the json output is handled by post-processing the xml.  this was 
a convenient and easy way to add json support, but, as a consequence, any 
number information is unavailable at the time the json output is generated.  as 
such, it's unlikely that this will be changed.

Original comment by jahlborn@gmail.com on 18 Jun 2011 at 12:28

GoogleCodeExporter commented 9 years ago
I had a similar problem. I usually don't use xml for any of my web stuff, and I 
also wanted to represent the datatype as much as possible. Writing your own 
Model->Rest functions is an easy and quick way to do this. 

Here is a sample of what I am doing 
-------------------------------

import simplejson as json
class JSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if hasattr(obj, 'isoformat'): #handles both date and datetime objects
            return obj.isoformat()
        else:
            return json.JSONEncoder.default(self, obj)

def toJSON(_object, default):
    result = None
    if isinstance(_object, list):
        result = []
        for item in _object:
            _result = dict([(p, getattr(item, p)) for p in item.properties()])
            _result['key'] = item.key().id_or_name()
            result.append(_result)
    else:
        try:
            result = dict([(p, getattr(_object, p)) for p in _object.properties()])
            result['key'] = _object.key().id_or_name()
        except: 
            LOG.exception("Error trying to convert %r to json", _object)
            result = default
    return json.dumps(result, cls=JSONEncoder)

-------------------- 
The above function toJSON will work with Model Objects, and the json string 
will have proper (almost) datatypes. 

Original comment by dshe...@etsy.com on 12 Dec 2011 at 9:49

GoogleCodeExporter commented 9 years ago
think i should be able to make this happen after all.

Original comment by jahlborn@gmail.com on 29 Jun 2012 at 4:28

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r79.

Original comment by jahlborn@gmail.com on 30 Jun 2012 at 12:20