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 ...
随机推荐
- HDU1212 大数膜
大数MOD #include<cstdio> #include<cstdlib> #include<iostream> #include<algorithm& ...
- int? 竟然真的可以是 null!.NET/C# 确定可空值类型 Nullable 实例的真实类型
使用 Nullable<T> 我们可以为原本不可能为 null 的值类型像引用类型那样提供一个 null 值.不过注意:Nullable<T> 本身也是个 struct,是个值 ...
- 思维题(两点逼近)LeetCode11 Container with Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- css文字和背景色渐变色
定义一个div: <div class="shop-title" >上海迪士尼度假区官方旗舰店</div> 使用css: .shop-title{ widt ...
- 极快瑞的函数式编程,Jquery涉及的一些函数
$(function(){ 一些实现功能的代码:})————————————文档载入完成后执行的函数.$(function(){}) 是 $(document).ready(function(){}) ...
- 如何取出word文档里的图片
在生活当中,Word办公是必不可少的.但是在工作中也会遇到一些麻烦,比如说如何取出word文档里的图片呢?有的人会通过复制粘贴,通过画图保存,可是这种方法未免太繁琐了吧.下面我就来分享一下我的经验. ...
- Source Insight 4 中文乱码的解决办法(source insight 3.5 及以下版本就到其他地方看看吧)
干货:Source Insight 4 中文乱码的解决办法(source insight 3.5 及以下版本就到其他地方看看吧) [解决办法]: 菜单栏中[File]->[Reload As E ...
- maven工程下的“run as application”
为了让maven能够在Terminal窗口执行(比在Console执行方便多了,命令行总是比右键选择run要快),需要配置如下的profile:然后敲入 mvn -test -Prun 即可. ...
- bzoj 4772 显而易见的数论——拆分数(五边形数定理)+线性筛
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4772 题解:https://blog.csdn.net/Dream_Lolita/artic ...
- 【linux】Linux内存的free的真实含义