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

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

问题描述

我喜欢 JsonProperty 在将属性放入数据存储区时自动将Python结构编码为JSON,并在检索时自动对其解码。但是,将该JSON数据发送到Web浏览器而无需再次对其进行编码将会很好。有没有办法得到原始的JSON数据(也就是说,防止解码)?

pre codelass DataForBrowser(ndb.Models)
json = ndb.JsonProperty()

def get_json(self):
return ???


解决方案

所以,你想要的是一个字典,获取在保存到数据存储区时编码,但在检索时没有被解码......底层的情况是,JsonProperty是BlobProperty的一个子类,它每次被写入数据存储区时都被编码(json.dumps()),解码(json.loads())每次读取。
这只能通过属性子类来完成,它可以消除其中一个功能(但我不认为根据实体的状态对财产进行不同的行为是明智的)。只是为了教育目的,让我们看看会发生什么,然后从google.appengine.ext导入ndb
导入json

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

def _from_base_type( self,value):
返回值
#最初返回json.loads(值)
$ b $ class DataForBrowser(ndb.Model):
json = ExtendedJsonProperty()


data = {'a':'A'}
data_for_browser = DataForBrowser()
data_for_browser.json = data
打印类型(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'>

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

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

如果您只需使用该词典创建JSON数据,然后在将数据分配给属性之前仅使用 BlobProperty 并传递 json.dumps()。 $ b

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天全站免登陆