Python可序列化对象json

30 python json serializable

class gpagelet:
    """
    Holds   1) the pagelet xpath, which is a string
            2) the list of pagelet shingles, list
    """
    def __init__(self, parent):
        if not isinstance( parent, gwebpage):
            raise Exception("Parent must be an instance of gwebpage")
        self.parent = parent    # This must be a gwebpage instance
        self.xpath = None       # String
        self.visibleShingles = [] # list of tuples
        self.invisibleShingles = [] # list of tuples
        self.urls = [] # list of string

class gwebpage:
    """
    Holds all the datastructure after the results have been parsed
    holds:  1) lists of gpagelets
            2) loc, string, location of the file that represents it
    """
    def __init__(self, url):
        self.url = url              # Str
        self.netloc = False         # Str
        self.gpagelets = []         # gpagelets instance
        self.page_key = ""          # str
Run Code Online (Sandbox Code Playgroud)

有没有办法让我的类json可序列化?我担心的是递归参考.

Anu*_*yal 49

编写自己的编码器和解码器,这可能非常简单 return __dict__

例如,这里是一个转储完全递归树结构的编码器,你可以根据自己的需要对其进行增强或使用

import json

class Tree(object):
    def __init__(self, name, childTrees=None):
        self.name = name
        if childTrees is None:
            childTrees = []
        self.childTrees = childTrees

class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if not isinstance(obj, Tree):
            return super(MyEncoder, self).default(obj)

        return obj.__dict__

c1 = Tree("c1")
c2 = Tree("c2") 
t = Tree("t",[c1,c2])

print json.dumps(t, cls=MyEncoder)
Run Code Online (Sandbox Code Playgroud)

它打印

{"childTrees": [{"childTrees": [], "name": "c1"}, {"childTrees": [], "name": "c2"}], "name": "t"}
Run Code Online (Sandbox Code Playgroud)

你可以类似地写一个解码器,但你会在某种程度上需要识别它是否是你的对象,所以你可以根据需要放一个类型.

  • 这对我有帮助.谢谢.:) (3认同)

lon*_*gda 16

jsonpickle赢了!

(刚才有同样的问题...... json pickle处理递归/嵌套对象图以及循环对象图的短路).

  • [不要破坏你不信任的数据!](http://nedbatchelder.com/blog/201302/war_is_peace.html) (14认同)

Eri*_*got 5

间接答案:您可以使用YAML代替使用JSON,它可以毫无问题地执行您想要的操作.(JSON本质上是YAML的一个子集.)

例:

import yaml
o1 = gwebpage("url")
o2 = gpagelet(o1)
o1.gpagelets = [o2]
print yaml.dump(o1)
Run Code Online (Sandbox Code Playgroud)

事实上,YAML很好地为您处理循环引用.