python - 如何在 App Engine 中从 JSON 中排除 db.Blob

标签 python json google-app-engine gql

我正在使用 question 2114659 中引用的 jsonGQL 版本和 available in the svn对于 eve-pos-tracker。

我和这个可怜的应用引擎开发者有同样的问题: https://groups.google.com/group/google-appengine/browse_thread/thread/f7a44b707119013c/81ba15c883ba95fe

我的 json 序列化在我的模型中遇到 png 图像 (0x89) 的二进制数据时崩溃。我不需要序列化此数据,所以我想跳过该字段或简单地输出类似 <binary> 的内容.

这是处理程序:

class FillList(webapp.RequestHandler):
    def get(self):
        fills = Fill.all()
        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write(jsonGQL.encode(fills.fetch(100)))

我试过这个:

  elif isinstance(obj, db.Blob):
      return "<binary>"

但错误依然存在。回溯是:

    Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/_webapp25.py", line 701, in __call__
    handler.get(*groups)
  File "~/app/api.py", line 61, in get
    self.response.out.write(jsonGQL.encode(fills.fetch(100)))
  File "~/app/jsonGQL.py", line 103, in encode
    return GqlEncoder().encode(input, exclude=[])
  File "~/app/jsonGQL.py", line 87, in encode
    return simplejson.JSONEncoder.encode(self, o)
  File "~/app/simplejson/encoder.py", line 216, in encode
    chunks = list(chunks)
  File "~/app/simplejson/encoder.py", line 482, in _iterencode
    for chunk in _iterencode_list(o, _current_indent_level):
  File "~/app/simplejson/encoder.py", line 380, in _iterencode_list
    for chunk in chunks:
  File "~/app/simplejson/encoder.py", line 496, in _iterencode
    for chunk in _iterencode(o, _current_indent_level):
  File "~/app/simplejson/encoder.py", line 485, in _iterencode
    for chunk in _iterencode_dict(o, _current_indent_level):
  File "~/app/simplejson/encoder.py", line 439, in _iterencode_dict
    yield _encoder(value)
  File "~/app/simplejson/encoder.py", line 50, in py_encode_basestring_ascii
    s = s.decode('utf-8')
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x89 in position 0: invalid start byte

最佳答案

这是我的解决方案

    elif isinstance(obj, db.Model):
        properties = obj.properties().items()
        output = {}
        for field, value in properties:
            data =  getattr(obj, field)
            if isinstance(data, str):
                # db.Blob inherits from str
                data = data.encode('string-escape')
            output[field] = data
        output['id'] = obj.key().id()
        return output

关于python - 如何在 App Engine 中从 JSON 中排除 db.Blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8208697/

相关文章:

python - Pycharm 和 Google App Engine 渲染请求发布数据的问题

python - 将多行代码片段粘贴到 IPython 中

python - 使用 numpy 从均匀分布中绘制具有 [0, 1] 的 float

已经注册的类的Python boost to-Python转换器被忽略第二个转换方法

读取 Hive 表中的 JSON 数据

java - 仅当实体不存在时,如何将其保存在 GAE 数据存储中,同时防止竞争条件

Python threading.thread.start() 不会将控制权返回给主线程

jquery - 从 JSON 解析的对象数组中填充 Select HTML 元素

php - 合并 Json 格式数组中的重复值

google-app-engine - App Engine 停机时间