#!/usr/bin/env python

# -*- coding:utf-8 -*-

# author: Changhua Gong


'''

Json仅支持简单的数据类型,不支持复杂类型的序列化,如函数

'''


import json



info = {"name":"daidai", "salary":15000}

# 序列化

with open("test.txt", "w") as f:

    # f.write(json.dumps(info))

    json.dump(info,f)  # 与上句完全等价

# 反序列化

with open("test.txt", "r") as f:

    # print(json.loads(f.read()))

    print(json.load(f)) # 与上句完全等价

    

    

#!/usr/bin/env python

# -*- coding:utf-8 -*-

# author: Changhua Gong

'''

Pickle可序列化python中所有的数据类型,仅在Python中

例如pickle可序列化函数

'''


import pickle

def fun_print():

    print("1......")


info = {"name":"daidai", "salary":15000, "func":fun_print}

# 序列化

with open("test.txt", "wb") as f:  # 以二进制写入

    # f.write(pickle.dumps(info))

    pickle.dump(info,f)  # 与上句完全等价




#!/usr/bin/env python

# -*- coding:utf-8 -*-

# author: Changhua Gong

import pickle


def fun_print(s):

    print("2......%s" % s)

# 反序列化

with open("test.txt", "rb") as f: # 以二进制读取

    rs = pickle.load(f)

    print(rs)

    rs["func"]("daidai")  # 实际上这里有func对应函数对应即可(仅需保证函数名一致即可),甚至可修改调用方式和函数体