Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

python - JsonPickle Objects with same Identity

I am currently wondering if the following is a gug or intended behaviour. I try to serialize a Python object tree, where some objects are the same. I want to avoid the py/id pointers (and all other bloat) from jsonpickle (1.4.2) so I use unpickleable=False.

import jsonpickle


class Person:
    def __init__(self):
        self.name = "Jane Doe"


class Car:
    def __init__(self):
        self.owner = None
        self.driver = None
        self.passengers = []


def test_jsonpickle():
    han = Person()
    han.name = "Han"
    car = Car()
    car.driver = han
    car.owner = han

    pa = Person()
    pa.name = "Pete"
    car.passengers.append(pa)
    car.passengers.append(han)

    json_string = jsonpickle.encode(car, unpicklable=False)
    print("Json String: " + json_string)
    # out: Json String: 
    # {"owner": {"name": "Han"}, "driver": null, "passengers": [{"name": "Pete"}, null]}


if __name__ == '__main__':
    test_jsonpickle()

Using make_refs=False does also not work and persists __repr__ return (plus still has the rest bloating py/object stuff etc). I would like to get deep copies (or $ref JSON pointers) for same objects. Does someone know how to tweak that or can propose another lib? I want to use the result Json in a web context. And yes I tried jsonpickleJS, but it's nasty when you miss to register a class (pointers are resolved to wrong objects).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...