The restjson return values are not nested in the result attribute of an object. The former behavior can be obtained with the nest_result protocol option

This commit is contained in:
Christophe de Vienne 2011-11-28 18:51:31 +01:00
parent 4b5d37a32f
commit 5aa8cf367a
2 changed files with 19 additions and 5 deletions

View File

@ -170,6 +170,10 @@ class RestJsonProtocol(RestProtocol):
'text/javascript',
'']
def __init__(self, nest_result=False):
super(RestJsonProtocol, self).__init__()
self.nest_result = nest_result
def decode_arg(self, value, arg):
return fromjson(arg.datatype, value)
@ -182,7 +186,9 @@ class RestJsonProtocol(RestProtocol):
def encode_result(self, context, result):
r = tojson(context.funcdef.return_type, result)
return json.dumps({'result': r}, ensure_ascii=False).encode('utf8')
if self.nest_result:
r = {'result': r}
return json.dumps(r, ensure_ascii=False).encode('utf8')
def encode_error(self, context, errordetail):
return json.dumps(errordetail, encoding='utf-8')

View File

@ -1,5 +1,6 @@
import decimal
import base64
import datetime
import wsme.tests.protocol
@ -10,7 +11,7 @@ except:
import wsme.protocols.restjson
from wsme.protocols.restjson import fromjson
from wsme.utils import *
from wsme.utils import parse_isodatetime, parse_isotime, parse_isodate
from wsme.types import isusertype
@ -79,8 +80,7 @@ class TestRestJson(wsme.tests.protocol.ProtocolTestCase):
return res
r = json.loads(res.body)
if 'result' in r:
r = r['result']
if res.status_int == 200:
if _rt and r:
r = prepare_result(r, _rt)
return r
@ -97,4 +97,12 @@ class TestRestJson(wsme.tests.protocol.ProtocolTestCase):
def test_keyargs(self):
r = self.app.get('/argtypes/setint.json?value=2')
assert json.loads(r.body) == {'result': 2}
print r
assert json.loads(r.body) == 2
nestedarray = 'value[0].inner.aint=54&value[1].inner.aint=55'
r = self.app.get('/argtypes/setnestedarray.json?' + nestedarray)
print r
assert json.loads(r.body) == [
{'inner': {'aint': 54}},
{'inner': {'aint': 55}}]