Appengine中的JSON序列化

2021-04-24 14:45:04 字數 3178 閱讀 1408

def getter(func):

if not func.__name__.startswith("get_"):

raise invalidmethodname("method name must start with 'get_'")

func.getter = true

return func

class resource(userdict.dictmixin):

def exposed_attrs(self):

"""attribute names to be exposed as keys"""

return

def hidden_keys(self):

"""keys to hide from iteration. a key will still be accessible if it is

a getter or if it is listed in exposed_attrs()"""

return

def child_object(self, name):

"""called to get a child object if it isn't found as a getter or an attribute"""

raise attributeerror

# dictionary methods

def __getitem__(self, key):

"""called to implement evaluation of self[key]"""

getter = getattr(self, 'get_'+key, none)

if hasattr(getter, 'getter'):

return getter()

if key in self.exposed_attrs():

return getattr(self, key)

return self.child_object(key)

def __setitem__(self, key, value):

"""called to implement assignment to self[key]"""

setter = getattr(self, 'set_'+key, none)

if hasattr(setter, 'setter'):

return setter(value)

def __delitem__(self, key):

"""called to implement deletion of self[key]"""

deleter = getattr(self, 'del_'+key, none)

if hasattr(deleter, 'deleter'):

return deleter(value)

def keys(self):

"""list of keys for iteration"""

getter_keys = [name.replace('get_', '', 1) for name in dir(self)

if hasattr(getattr(self, name, none), 'getter')]

exposed_keys = getter_keys + self.exposed_attrs()

return [key for key in exposed_keys if key not in self.hidden_keys()]

當繼承db.model時,同時繼承resource:

class personmodel(resource, db.model):

user_id = db.stringproperty()

user_content = db.textproperty()

@getter

def get_uid(self):

return self.user_id

@getter

def get_content(self):

return self.user_content

在處理請求的時候,再做如下處理:

def post(self):

self.dispatch_request()

def dispatch_request(self):

self.response.headers["content-type"] = "text/plain"

params = {}

for key in self.request.params:

params[str(key)] = self.request.get(key)

obj = //fetch data from personmodel.

obj = self.preserialize(obj)

out = ******json.dumps(obj)

self.response.out.write(out)

def translate(self, obj, depth):

return obj

def preserialize(self, obj, depth=0):

obj = self.translate(obj, depth)

if isinstance(obj, datetime.datetime):

return obj.isoformat()

elif isinstance(obj, db.users.user):

return obj.nickname()

elif isinstance(obj, (db.geopt, db.im, db.key)):

return str(obj)

elif isinstance(obj, types.listtype):

return [self.preserialize(item, depth+1) for item in obj]

elif isinstance(obj, (types.dicttype, resource)):

copy = {}

for key in obj:

copy[key] = self.preserialize(obj[key], depth+1)

return copy

return obj

這個方法不是太好,還有很大的優化空間...

SJ中的json 及多維json

規則如下 1 對映用冒號 表示。名稱 值 2 並列的資料之間用逗號 分隔。名稱1 值1,名稱2 值2 3 對映的集合 物件 用大括號 表示。4 並列資料的集合 陣列 用方括號 表示。5 元素值可具有的型別 string,number,object,array,true,false,null 2.js...

Flash中JSON的使用

在as中使用json其實並不是乙個必須或是很好的選擇,因為as對xml的解析已經很不錯了,但是為什麼可以考慮使用 json呢,有以下幾點 json是介於單純的文字方式 如 下面就介紹一下adobe的官方的json類的用法 下面是教程,比較簡單 1 伺服器端來的json 怎麼樣獲得伺服器端的json我...

js中的json使用

一 json字串轉成物件 首先建立字串 var str 呼叫eval函式轉成物件,1 var obj eval str alert obj 0 name 輸出 cxh 或者 與上面的等價 eval var ooo str alert ooo 0 name 輸出 cxh 2 直接使用json物件轉換,...