Python实现支持JSON存储和解析的对象

我们知道利用JSON模块可方便的将Python基本类型(dict、list等)数据永久的存储成文件,同时也可以通过自定义转换函数和继承JSON encode&decode的方法实现自定义类的存储。本文就在前文“ Python JSON模块”的基础上,实现python支持JSON存储的对象。

对象能够采取JSON存储和解析是有很大意义的。例如机器学习中所有分类算法的训练过程中都存在大量的数据计算,如果每次启动分类都需要重新训练分类算法浪费资源且没有效率,如果能够将训练产生的分类算法对象保存起来,那么除非需要算法调优,以后只需载入即可。另一方面,对象能够进行JSON解析和存储也使得其可以在网络上传送,这在当下云计算、分布式数据处理中都有非凡的意义。

为了实现自存储和解析,定义对象的关键操作有:

0,将object_json.py copy至包中,定义对象的模块导入object_json:import object_json。

1,__init__()函数要支持可变数量的函数调用,即要写成__init__(self, …, , **args)。如此定义对象才可以有除构造阶段需要初始化的属性之外的属性。

2,对于对象构造阶段必须初始化的属性,__init__()函数中的形参必须与这些属性名称完全相同,如此才能通过字典‘key’: value对构造对象。

3,定义一个属性‘__name__’–该对象实例的名称,利用inspect模块实现。‘__name__‘属性主要用于产生对象存储时默认的文件名称。

4,定义jsonDumps()和jsonLoadTransfer()方法,通过objectLoadFromFile()完成对象JSON文件load和新对象创建。

(i)jsonDumps()用于将对象转换成dict并通过json.dumps()将对象存储成json文件,若用户不指定文件名则以instancename.json为默认存储文件。由于JSON只支持python基本类型,因此若对象中有一些其他类型(如numpy matrix),则需将其转化成Python基本类型(如matrix.tolist()将matrix转换成list)。

(ii)jsonLoadTransfer()用于完成数据格式的转换,将一些对象属性从基本类型转化成需要的类型(如mat(list)将类型从list转换成matrix),若对象只有Python基本类型则可以省略该方法。创建完整、可用对象过程是:

obj = objectLoadFromFile() 
obj.jsonLoadTransfer() 

下面的代码就是支持自定义对象进行JSON存储和解析的object_json模块源码。

import json 
import inspect   
import pdb 
def object2dict(obj):     
    #convert object to a dict     
    d = {‘__class__’:obj.__class__.__name__, ‘__module__’:obj.__module__}     
    d.update(obj.__dict__)     
    return d 
def objectDumps2File(obj, jsonfile): 
    objDict = object2dict(obj) 
    with open(jsonfile, ‘w’) as f: 
        f.write(json.dumps(objDict)) 
     
def dict2object(d):     
    ”’convert dict to object, the dict will be changed”’     
    if’__class__’ in d:     
        class_name = d.pop(‘__class__’)     
        module_name = d.pop(‘__module__’)     
        module = __import__(module_name)     
        #print ‘the module is:’, module     
        class_ = getattr(module,class_name)     
        args = dict((key.encode(‘ascii’), value) for key, value in d.items()) #get args   
        #print ‘the atrribute:’, repr(args) 
        #pdb.set_trace() 
        inst = class_(**args) #create new instance     
    else:     
        inst = d     
    return inst 
def objectLoadFromFile(jsonFile): 
    ”’load json file and generate a new object instance whose __name__ filed 
    will be ‘inst’ ”’ 
    with open(jsonFile) as f: 
        objectDict =json.load(f) 
    obj = dict2object(objectDict) 
    return obj 
#test function     
if __name__  == ‘__main__’: 
    class Person(object):     
        def __init__(self,name,age, **args): 
            obj_list = inspect.stack()[1][-2] 
            self.__name__ = obj_list[0].split(‘=’)[0].strip()#object instance name 
            self.name = name     
            self.age = age 
             
        def __repr__(self):     
            return ‘Person Object name : %s , age : %d’ % (self.name,self.age) 
        def say(self): 
            #d = inspect.stack()[1][-2] 
            #print d[0].split(‘.’)[0].strip() 
            return self.__name__ 
        def jsonDumps(self, filename=None): 
            ”’essential transformation to Python basic type in order to 
            store as json. dumps as objectname.json if filename missed ”’ 
            if not filename: 
                jsonfile = self.__name__+’.json’ 
            else: jsonfile = filename 
            objectDumps2File(self, jsonfile) 
         
        def jsonLoadTransfer(self):#TBD 
            ”’essential transformation to object required type,such as 
            numpy matrix.call this function after newobject = objectLoadFromFile(jsonfile)”’ 
            pass 
 
    p = Person(‘Aidan’,22)       
    #json.dumps(p)#error will be throwed 
     
    #objectDumps2File(p,’Person.json’) 
    p.jsonDumps() 
    p_l = objectLoadFromFile(‘p.json’) 
       
    print ‘the decoded obj type: %s, obj:%s’ % (type(p_l),repr(p_l)) 

Python类有新旧两种,py 2.2 后类定义继承 object 的目的是使这个类成为 new style class, 没有继承 object 的为传统classic class(最终也会继承object)。

类定义中如下两种方法:

class Person(): 
class Person(object) 

其区别在于:

若创建新的Person instanc test,则type(test)的输出分别为:

 
 

inspect 模块提供了一系列自省函数,它可以获取模块,类,方法,函数,traceback,帧对象,代码对象的信息。常用的方法getmembers,ismodule,getcallargs,isclass等,更多详细信息参见http://docs.python.org/library/inspect.html。

————————————–分割线 ————————————–

Struts中异步传送XML和JSON类型的数据 http://www.linuxidc.com/Linux/2013-08/88247.htm

Linux下JSON库的编译及代码测试 http://www.linuxidc.com/Linux/2013-03/81607.htm

jQuery 获取JSON数据[$.getJSON方法] http://www.linuxidc.com/Linux/2013-03/81673.htm

用jQuery以及JSON包将表单数据转为JSON字符串 http://www.linuxidc.com/Linux/2013-01/77560.htm

在C语言中解析JSON配置文件 http://www.linuxidc.com/Linux/2014-05/101822.htm

————————————–分割线 ——————————-