40 lines
1 KiB
Python
40 lines
1 KiB
Python
![]() |
#!/usr/bin/python
|
||
|
# -*- coding: utf-8 -*-
|
||
|
import json
|
||
|
from decimal import Decimal
|
||
|
# from task_row_x import TaskRow, SimpleTaskRow
|
||
|
|
||
|
|
||
|
def load_from_json_file(infile):
|
||
|
with open(infile, 'r') as fin:
|
||
|
obj = json.load(fin)
|
||
|
return obj
|
||
|
|
||
|
|
||
|
class SpecialEncoder(json.JSONEncoder):
|
||
|
def default(self, obj):
|
||
|
if isinstance(obj, Decimal):
|
||
|
return str(obj) # Or you could return str(obj) depending on your needs
|
||
|
# if isinstance(obj, TaskRow) or isinstance(obj, SimpleTaskRow):
|
||
|
if hasattr(obj, 'get_dict') and callable(getattr(obj, 'get_dict')):
|
||
|
return obj.get_dict()
|
||
|
if isinstance(obj, set):
|
||
|
return [str(v) for v in obj]
|
||
|
return super().default(obj)
|
||
|
|
||
|
|
||
|
def save_to_json_file(out_name, obj):
|
||
|
with open(out_name, 'w', encoding='utf-8', newline='\n') as fout:
|
||
|
json.dump(obj, fout, indent=4, cls=SpecialEncoder)
|
||
|
|
||
|
|
||
|
def save_to_json_str(obj):
|
||
|
return json.dumps(obj, indent=4, cls=SpecialEncoder)
|
||
|
|
||
|
def main():
|
||
|
pass
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
main()
|