Python序列化和反序列化vsJSON
# -*- coding: utf-8 -*
"""没有嵌套类的类 author: Jill usage: """
import json class Leaf:
def __init__(self, leaf_arg_a, leaf_arg_b):
self.leaf_arg_a = leaf_arg_a
self.leaf_arg_b = leaf_arg_b def __str__(self):
return (
"Leaf:\n"
"arg_a: {0.leaf_arg_a}\n"
"arg_b: {0.leaf_arg_b}\n"
).format(self) def leaf2dict(self):
return {
'leafArgA': self.leaf_arg_a,
'leafArgB': self.leaf_arg_b
} def dict2leaf(self):
return Leaf(self['leafArgA'], self['leafArgB']) @staticmethod
def parse_json_obj(json_str):
a = json.loads(json_str, object_hook=Leaf.dict2leaf)
return a def to_json_str(self):
return json.dumps(self, default=Leaf.leaf2dict) if __name__ == '__main__':
leaf = Leaf("a", "b")
print(leaf.to_json_str()) print() json_str = '{"leafArgA": "a", "leafArgB": "b"}'
json_obj = Leaf.parse_json_obj(json_str)
print(json_obj.leaf_arg_a)
"""有嵌套类Leaf的类 author: Jill usage:
"""
import json from entity.child import Leaf class Root:
def __init__(self, root_arg_a, leafs):
self.root_arg_a = root_arg_a
self.leafs = leafs def root2dict(self):
return {
'rootArgA': self.root_arg_a,
'leafs': json.loads(json.dumps(self.leafs, default=Leaf.leaf2dict)),
} def to_json_str(self):
return json.dumps(self, default=Root.root2dict) @staticmethod
def from_json_obj_get(key, json_str):
if key == 'leafs':
json_array = json.dumps(json.loads(json_str).get(key))
array = json.loads(json_array, object_hook=Leaf.dict2leaf)
return array
return json.loads(json_str).get(key) @staticmethod
def parse_json_obj(json_str):
json_dict = json.loads(json_str)
root_arg_a = json_dict.get('rootArgA')
leafs = Leaf.load_from_java(json.dumps(json_dict.get('leafs')))
return Root(root_arg_a, leafs) if __name__ == '__main__':
leaf1 = Leaf('a1', 'b1')
leaf2 = Leaf('a2', 'b2')
leafs = [leaf1, leaf2]
root = Root("root_a", leafs) json_obj = root.to_json_str()
print(json_obj) json_str = '{"rootArgA": "root_a", "leafs": ' \
'[{"leafArgA": "a1", "leafArgB": "b1"}, {"leafArgA": "a2", "leafArgB": "b2"}]}'
root_obj = Root.parse_json_obj(json_str)
for leaf in root_obj.leafs:
print(leaf) print(root_obj.root_arg_a)
leaf_list = Root.from_json_obj_get('leafs', json_str)
print(leaf_list[0].leaf_arg_a)
Python序列化和反序列化vsJSON的更多相关文章
- Python序列化和反序列化
Python序列化和反序列化 通过将对象序列化可以将其存储在变量或者文件中,可以保存当时对象的状态,实现其生命周期的延长.并且需要时可以再次将这个对象读取出来.Python中有几个常用模块可实现这一功 ...
- python序列化与反序列化(json与pickle)
在python中,序列化可以理解为将python中对象的编码格式转换为json(pickle)格式的字符串,而反序列化可以 理解为将json(pickle)格式的字符串转换为python中对象的编码格 ...
- Python—序列化和反序列化模块(json、pickle和shelve)
什么是序列化 我们把对象(或者变量)从内存中变为可存储或者可传输的过程称为序列化.在python中为pickling,在其他语言中也被称之为serialization,marshalling,flat ...
- Python 序列化与反序列化
序列化是为了将内存中的字典.列表.集合以及各种对象,保存到一个文件中(字节流).而反序列化是将字节流转化回原始的对象的一个过程. json库 序列化:json.dumps() 反序列化:json.lo ...
- python序列化与反序列化(json、pickle)-(五)
1.什么是序列化&反序列化? 序列化:将字典.列表.类的实例对象等内容转换成一个字符串的过程. 反序列化:将一个字符串转换成字典.列表.类的实例对象等内容的过程 PS:Python中常见的数据 ...
- python 序列化和反序列化
概念 序列化: 将对象的状态信息转换为可以存储或传输的形式的过程.就是把对象转换成字符串的过程 反序列化: 把字符串转换成python可以识别的数据类型对象的过程 应用 #数据存储 #网络传输 模块 ...
- python 序列化,反序列化
附: pickle 有大量的配置选项和一些棘手的问题.对于最常见的使用场景,你不需要去担心这个,是如果你要在一个重要的程序中使用pickle 去做序列化的话,最好去查阅一下官方文档. https:// ...
- Python序列化与反序列化-json与pickle
Python序列化与反序列化-json与pickle 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.json的序列化方式与反序列化方式 1>.json序列化 #!/usr ...
- Python库:序列化和反序列化模块pickle介绍
1 前言 在“通过简单示例来理解什么是机器学习”这篇文章里提到了pickle库的使用,本文来做进一步的阐述. 通过简单示例来理解什么是机器学习 pickle是python语言的一个标准模块,安装pyt ...
随机推荐
- Photoshop通道抠出散乱的儿童头发
抠图之前仔细分析是必不可少的.要了解清楚需要抠取部分的构成,然后选择最快捷的方法.教程素材图片人物头发色调比较单一,背景色也比较单一,用通道抠图是非常快捷的. 最终效果1 最终效果2 原图 一.复制图 ...
- CF1096. G. Lucky Tickets(快速幂NTT)
All bus tickets in Berland have their numbers. A number consists of n digits (n is even). Only k dec ...
- each遍历 的原理
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- tarjan强联通分量(模板)
#include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #inc ...
- 解决安装vmware-tools出现的“The path "" is not a valid path to the 3.2.0-4-amd64 kernel headers”问题
在用虚拟机安装使用64位Crunchbang(一种Debian GNU/Linux 的linux)的过程中出现很多小问题.其中vmware-tools安装就是第一个问题. 在使用终端安装vmware- ...
- Struts2重学习之作用域的获取
第一种:获取requestMap,sessionMap,applicationMap, HttpServletRequest,HttpServletResponse对象的获取,在Struts2中 pu ...
- MySQL--区分表名大小写
============================================================================ 在MySQL中,可以通过lower_case_ ...
- stenciljs 学习十二 官方doc 路由使用的例子
路由在单页面应用开发中是一个相对比较重要的位置 以下为官方网站的路由配置 <stencil-router scrollTopOffset={0}> <stencil-route-sw ...
- web.xml中context-param详解
<context-param> <param-name>contextConfigLocation</param-name> <param-value> ...
- CountDownLatch、CyclicBarrier、Semaphore 区别
CountDownLatch.CyclicBarrier.Semaphore 区别: CountDownLatch和CyclicBarrier都能够实现线程之间的等待,只不过它们侧重点不同: Coun ...