开发者

google app engine jsonpickle

开发者 https://www.devze.com 2022-12-15 06:07 出处:网络
Has anyone got jsonpickle working on the google app engine? My logs say there is no module but there is a module as sure as you\'re born. i\'m using jsonpickle 0.32.

Has anyone got jsonpickle working on the google app engine? My logs say there is no module but there is a module as sure as you're born. i'm using jsonpickle 0.32.

<type 'exceptions.ImportError'>: No module named jsonpickle开发者_运维知识库
Traceback (most recent call last):
  File "/base/data/home/apps/xxxxx/xxxxxxxxxxxxxxxxx/main.py", line 4, in <module>
    import jsonpickle


I have managed to make it work registering django.utils.simplejson as a json encoder/decoder. In this real file index.py class Pizza is encoded and decoded back:

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

import jsonpickle

class Pizza:
    pass                

class Example(webapp.RequestHandler):
    def get(self):
        jsonpickle.load_backend('django.utils.simplejson',
                                'dumps','loads',ValueError)
        encoded = jsonpickle.encode(Pizza())
        self.response.out.write( jsonpickle.decode(encoded).__class__ )

run_wsgi_app(webapp.WSGIApplication([('/', Example),],debug=True))


As this post explains, jsonpickle requires one of a few underlying JSON modules. I would fix the issue as follows -- put at the top of your module(s) that need jsonpickle the following few lines:

import sys
import django.utils.simplejson
sys.modules['simplejson'] = django.utils.simplejson

This addresses the problem: jsonpickle needs simplejson (as one of the JSON modules it can use), but GAE has it as django.utils.simplejson, so you need to "alias" it appropriately.

0

精彩评论

暂无评论...
验证码 换一张
取 消