如何在 App Engine 中从 JSON 中免除 db.Blob

发布于 2024-12-17 07:53:56 字数 2654 浏览 0 评论 0原文

我正在使用 问题 2114659可在 eve-pos-tracker 的 svn 中使用。

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

我的 json 序列化在遇到模型中 png 图像 (0x89) 的二进制数据时崩溃。我不需要序列化此数据,因此我要么想跳过该字段,要么只是输出类似 的内容。

这是处理程序:

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

I'm using a version of the jsonGQL referenced in question 2114659 and available in the svn for eve-pos-tracker.

I have the same question as this poor app engine developer:
https://groups.google.com/group/google-appengine/browse_thread/thread/f7a44b707119013c/81ba15c883ba95fe

My json serialization crashes when it hits the binary data for the png image (0x89) in my model. I don't need to serialize this data so I'd either like to skip the field or simply output something like <binary>.

this is the handler:

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)))

I tried this:

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

but the error persists. The traceback is:

    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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋风の叶未落 2024-12-24 07:53:56

这是我的修复

    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

here is my fix

    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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文