python - Rename deseralized fields in Colander -
i'm using colander convert json strings python objects , vice versa in pyramid/cornice based project.
is there way can serialize/deserialize different names/keys ?
here colander schema:
class commentschema(mappingschema): resource_id = schemanode(int(), name="resourceid", location="body") text = schemanode(string(), name="text", location="body")
and here input json
{"text":"hello!", "resourceid":12}
it being converted :
{u'text': u'hello!', u'resourceid': 12}
here question, can have same input json converted following?
{u'full_text': u'hello!', u'resource_id': 12}
thanks help.
i had manually. whatever received json used construct data object. object have custom function map data desired output format, , pass output serializer:
data_schema = dataschema().deserialize(self.request.json) data_obj = dataobject(data_schema**) // or dataobject(full_text = data_schema['text'], resource_id = data_schema['resourceid']) # # ... # rbody = dataschema().serialize(data_obj.map_dump()) return response(body=rbody, status_code=201)
and dataobject this:
class profileobj(object): def __init__(self, text, resourceid): // or __init__(self, full_text, resource_id) self.text = text self.resourceid = resourceid def map_dump(self): output['full_text'] = self.text output['resource_id'] = self.resource return output
Comments
Post a Comment