Daniel Watrous
2015-03-17 00:25:20 UTC
Hello,
I have a list of objects that I want converted to json at the time the
response is sent. I found this
(https://github.com/bottlepy/bottle/issues/287), but I can't seem to get it
working.
class MyJsonEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, model.person):
return obj.__dict__
return json.JSONEncoder.default(self, obj)
appv1 = bottle.Bottle(autojson=False)
appv1.install(bottle.JSONPlugin(json_dumps=lambda s: json.dumps(s,
cls=MyJsonEncoder)))
@appv1.get('/v1.0/person/search')
def search_people():
bottle.response.content_type = 'application/json'
search, city, country = get_person_search_attributes()
matching_people = persondao.find_by_last_first_comma(search, city,
country)
return matching_people
When i "return matching_people" I am expecting it to be run through
MyJsonEncoder.default. If the obj is an instance of "model.person" it
should return a dict. The default function doesn't appear to ever get
called.
What am I missing?
--
I have a list of objects that I want converted to json at the time the
response is sent. I found this
(https://github.com/bottlepy/bottle/issues/287), but I can't seem to get it
working.
class MyJsonEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, model.person):
return obj.__dict__
return json.JSONEncoder.default(self, obj)
appv1 = bottle.Bottle(autojson=False)
appv1.install(bottle.JSONPlugin(json_dumps=lambda s: json.dumps(s,
cls=MyJsonEncoder)))
@appv1.get('/v1.0/person/search')
def search_people():
bottle.response.content_type = 'application/json'
search, city, country = get_person_search_attributes()
matching_people = persondao.find_by_last_first_comma(search, city,
country)
return matching_people
When i "return matching_people" I am expecting it to be run through
MyJsonEncoder.default. If the obj is an instance of "model.person" it
should return a dict. The default function doesn't appear to ever get
called.
What am I missing?
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.
---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.
---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.