patx / pickledb

pickleDB is an open source key-value store using Python's json module.
https://patx.github.io/pickledb
BSD 3-Clause "New" or "Revised" License
925 stars 125 forks source link

pass in a json encoder function to json.dumps #57

Closed academyofzhuang closed 4 years ago

academyofzhuang commented 5 years ago

json.dumps can't serialize user defined instance variables. If you allow us to pass in configuration to json.dumps, we can solve the problem.

def my_instance_json_encoder(x):
    lambda x: x.__dict__

s = json.dumps(
        users, default=my_instance_json_encoder
)

Better yet:

config = {
    "default": lambda x: x.__dict__,
    "indent": "    ",
}

 json.dumps(users, config)