Discussion:
[bottlepy] returning objects as JSON in bottle
Daniel Watrous
2015-03-17 00:25:20 UTC
Permalink
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?
--
--
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.
Daniel Watrous
2015-03-17 00:32:51 UTC
Permalink
I found this interesting article about flask:
http://flask.pocoo.org/docs/0.10/security/#json-security

It seems to apply to bottle too. If I change my last line to this

return {'result': matching_people}

It begins working. That's not quite what I was hoping to accomplish, but it
gives me something to work with.

Daniel
Post by Daniel Watrous
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.
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')
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.
Avelino
2015-03-17 00:56:57 UTC
Permalink
Sample[1]

import json
from bottle import response

response.set_header('charset', 'utf-8')
response.content_type = 'application/json'
data = {'a': 1, 'b': 2}
return json.dumps(data)



[1] https://github.com/avelino/mining/blob/master/mining/controllers/api/base.py#L29
Post by Daniel Watrous
http://flask.pocoo.org/docs/0.10/security/#json-security
It seems to apply to bottle too. If I change my last line to this
return {'result': matching_people}
It begins working. That's not quite what I was hoping to accomplish, but it
gives me something to work with.
Daniel
Post by Daniel Watrous
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.
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')
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.
For more options, visit https://groups.google.com/d/optout.
--
Thiago Avelino - GPG Keys: 2E98180A
<***@avelino.xxx> (Mail/XMPP)
http://avelino.xxx
I love long mails! - http://email.is-not-s.ms/
--
--
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.
Loading...