我正在jsonpickle
用作我的小项目的数据库,并且在编码复杂类时遇到了一些奇怪的行为,基本上Book
来自User
实例的编码正确,但 db 列表中的书籍被编码为奇怪的{"py/id":4}
-s。有人可以解释我哪里出错了,我该如何解决这个问题
import jsonpickle
from random import randint
admin_ids = [302115492]
def gen_id():
return randint(1, 2**64)
class Book():
def __init__(self, name, author, img, oid):
self.name = name
self.author = author
self.img = img
self.id = gen_id()
self.owner_id = oid
class User():
id = 0
name = ""
books = []
requests = []
def __init__(self, name, uid):
self.id = uid
self.name = name
self.books = []
class Database():
def __init__(self):
self.u_dir = "users"
self.b_dir = "books"
self.users = []
self.books = []
def add_book(self, book):
self.books.append(book)
for user in self.users:
if(user.id == book.owner_id):
user.books.append(book)
def add_user(self, user):
self.users.append(user)
db=Database()
db.add_user(User('John',123))
db.add_book(Book('name','author','img.png',123))
print(jsonpickle.encode(db))
输出:
{
"py/object":"__main__.Database",
"u_dir":"users",
"b_dir":"books",
"users":[
{
"py/object":"__main__.User",
"id":123,
"name":"John",
"books":[
{
"py/object":"__main__.Book",
"name":"name",
"author":"author",
"img":"img.png",
"id":8045585124766781176,
"owner_id":123
}
]
}
],
"books":[
{
"py/id":4
}
]
}