Skip to content

Commit

Permalink
Use a version of Nick's to/from json funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
dsblank committed Oct 13, 2024
1 parent a9da731 commit bc5ac5b
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions gramps/gen/lib/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,47 @@


def __default(obj):
obj_dict = {"_class": obj.__class__.__name__}
obj_dict = {'_class': obj.__class__.__name__}
if isinstance(obj, lib.GrampsType):
obj_dict["string"] = getattr(obj, "string")
if isinstance(obj, lib.Date):
if obj.is_empty() and not obj.text:
return None
obj_dict['string'] = getattr(obj, 'string')
# FIXME
#if isinstance(obj, lib.Date):
#if obj.is_empty() and not obj.text:
#return None
for key, value in obj.__dict__.items():
if not key.startswith("_"):
if not key.startswith('_'):
obj_dict[key] = value
for key, value in obj.__class__.__dict__.items():
if isinstance(value, property):
if key != "year":
if key != 'year':
obj_dict[key] = getattr(obj, key)
return obj_dict


def __object_hook(obj_dict):
obj = getattr(lib, obj_dict["_class"])()
for key, value in obj_dict.items():
if key != "_class":
if key in ("dateval", "rect") and value is not None:
value = tuple(value)
if key == "ranges":
value = [tuple(item) for item in value]
setattr(obj, key, value)
if obj_dict["_class"] == "Date":
if obj.is_empty() and not obj.text:
return None
g_class = obj_dict.pop('_class')
objcl = getattr(lib, g_class)
obj = objcl.__new__(objcl) # now we have instance, but NOT initialized
if isinstance(obj, lib.GrampsType):
obj.set(obj_dict['string'])
else:
if 'dateval' in obj_dict: # fix up tuple
value = obj_dict['dateval']
if value is not None:
obj_dict['dateval'] = tuple(value)
elif 'rect' in obj_dict: # fix up tuple
value = obj_dict['rect']
if value is not None:
obj_dict['rect'] = tuple(value)
elif 'ranges' in obj_dict: # fix up tuple
value = obj_dict['ranges']
obj_dict['ranges'] = [tuple(item) for item in value]

# now we can merge the json dict with the object, loading everything
obj.__dict__.update(obj_dict)
# FIXME
#if obj_dict['_class'] == 'Date':
#if obj.is_empty() and not obj.text:
#return None
return obj


Expand Down

0 comments on commit bc5ac5b

Please sign in to comment.