"function ... is not JSON serializable" error when rendering db row as JSON

195 views
Skip to first unread message

Amnon Khen

unread,
Sep 2, 2011, 11:57:48 AM9/2/11
to web2py-users
Dear web2py folks,

I have a controller that fetches a record from the DB (I'm using
GAE).

def event():
id=request.args[0]
myrow = db.event[id]
return dict(myrow = myrow)

What I try to render the result as JSON, I get the following error
(trace starts from generic.json view)

File "C:\Users\Hampi\git\skeduk\web2py\gluon\serializers.py", line
30, in custom_json
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <function <lambda> at 0x05EA7A30> is not JSON
serializable

When I try to render the same controller's result using the
generic.xml view, the rending is done OK, nad I see that "function
<lambda> at 0x05EA7A30" is the row's "update_record" method.

Why are my object's methods being serialized?
Is this a bug in the custom_json function in serializers.py?

Thanks for you help,
Amnon Khen

Amnon Khen

unread,
Sep 2, 2011, 12:12:28 PM9/2/11
to web2py-users
Hi,
As a quick workaround, I thought of modifying serializers.custom_json
in the following manner:
1. check whether the item is a lambda expression
2. if so, return its string representation (using repr())

I used a nice "islambda" function I found in
http://bytes.com/topic/python/answers/39908-what-lambda-used-real-code

I guess there's no point in returning the string represntation of the
function to the user, so this probably won't be the final solution to
this issue.

What do yo think about this fix?

Sincerely,
Amnon

Amnon Khen

unread,
Sep 2, 2011, 12:14:09 PM9/2/11
to web2py-users
Sorry for the noise.
I am such a noob :)

I posted a wrong link to thepost about "islambda" function.
The correct one is: http://kbyanc.blogspot.com/2007/05/python-islambda.html

Amnon

On Sep 2, 7:12 pm, Amnon Khen <amnon...@gmail.com> wrote:
> Hi,
> As a quick workaround, I thought of modifying serializers.custom_json
> in the following manner:
> 1. check whether the item is a lambda expression
> 2. if so, return its string representation (using repr())
>
> I used a nice "islambda" function I found inhttp://bytes.com/topic/python/answers/39908-what-lambda-used-real-code
Reply all
Reply to author
Forward
0 new messages