atextcrawler/src/atextcrawler/utils/json.py

33 lines
608 B
Python

"""
Custom JSON encoder.
"""
import json
class JSONEncoderExt(json.JSONEncoder):
"""
Extended JSON encoder with encoding of sets as lists.
"""
def default(self, obj):
"""
Encode sets as lists and everything else as by default.
"""
if isinstance(obj, set):
return list(obj)
return json.JSONEncoder.default(self, obj)
def json_dumps(obj):
"""
Encode an object to a JSON string using JSONEncoderExt.
"""
return json.dumps(obj, cls=JSONEncoderExt)
json_loads = json.loads
"""
Decoding of JSON strings as by default.
"""