App Engine 从 JsonProperty 返回 JSON [英] App Engine return JSON from JsonProperty

查看:19
本文介绍了App Engine 从 JsonProperty 返回 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢 JsonProperty 如何在属性被放入数据存储时自动将 Python 结构编码为 JSON,并在检索时自动解码.但是,最好将该 JSON 数据发送到 Web 浏览器而无需再次对其进行编码.有没有办法获取原始JSON数据(即防止解码)?

class DataForBrowser(ndb.Models)json = ndb.JsonProperty()def get_json(self):返回 ???

解决方案

所以你想要的是有一个 dict 在保存到数据存储时被编码但在检索它时不解码......幕后"会发生什么是 JsonProperty 是 BlobProperty 的子类,每次写入数据存储时都会对其进行编码 (json.dumps()) 并在每次读取时进行解码 (json.loads()).这只能通过消除这些功能之一的属性子类来完成(但我认为根据实体所处的状态对财产有不同的行为是不明智的).只是为了教育目的"让我们看看会发生什么

 from google.appengine.ext import ndb导入json类 ExtendedJsonProperty(ndb.BlobProperty):def _to_base_type(self, value):返回 json.dumps(value)def _from_base_type(self, value):返回值# 原本返回 json.loads(value)类 DataForBrowser(ndb.Model):json = ExtendedJsonProperty()数据 = {'a':'A'}data_for_browser = DataForBrowser()data_for_browser.json = 数据打印类型(data_for_browser.json) # 返回<type 'dict'>data_for_browser.put()打印类型(data_for_browser.json) # 返回<type 'str'>data_for_browser_retrieverd = DataForBrowser.query().get()打印类型(data_for_browser_retrieverd.json) # 返回<type 'str'>

如果您需要在代码中使用 dict,那么我建议使用 JsonProperty 并添加一个新的属性方法,该方法将 dict 作为 JSON 返回并使用它.

@propertydef json_as_json(self):返回 json.dumps(self.json)

如果您仅使用 dict 来创建 JSON 数据,则只需使用 BlobProperty 并在将数据分配给属性之前通过 json.dumps()>

I like how the JsonProperty automatically encodes a Python structure into JSON when the property is put into the data store, and automatically decodes it when retrieved. However, it would be nice to send that JSON data to a web browser without having to encode it again. Is there a way to get the raw JSON data (that is, prevent the decoding)?

class DataForBrowser(ndb.Models)
    json = ndb.JsonProperty()

    def get_json(self):
        return ???

解决方案

So what you want is to have a dict that gets encoded when saved to the datastore but not decoded upon retrieving it... What happens "under the hood" is that a JsonProperty is a subclass of BlobProperty that gets encoded (json.dumps()) every time it gets written to the datastore and decoded (json.loads()) every time it's read. This can be done only with a property subclass that eliminates one of these functions (but I don't think it's wise to have different behaviors for the property according to the state the entity is). Just for "educational purposes" let's see what will happen then

from google.appengine.ext import ndb
import json

class ExtendedJsonProperty(ndb.BlobProperty):
  def _to_base_type(self, value):
    return json.dumps(value) 

  def _from_base_type(self, value):
    return value
    # originally return json.loads(value)

class DataForBrowser(ndb.Model):
    json = ExtendedJsonProperty()


data = {'a': 'A'}
data_for_browser = DataForBrowser()
data_for_browser.json = data
print type(data_for_browser.json)  # returns <type 'dict'>
data_for_browser.put()
print type(data_for_browser.json) # returns <type 'str'>
data_for_browser_retrieverd = DataForBrowser.query().get()
print type(data_for_browser_retrieverd.json) # returns <type 'str'>

If you need to make use of the dict in your code then I suggest using a JsonProperty and adding a new property method that will return the dict as JSON and use that.

@property
def json_as_json(self):
  return json.dumps(self.json)

If you use the dict only to create the JSON data then just use a BlobProperty and pass through json.dumps() before assigning the data to the property

这篇关于App Engine 从 JsonProperty 返回 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆