def serialize_json(obj):
"""
A helper method for serializing Cytoscape.js elements in desired json form.
:param obj: Object to serialize
:return: JSON string representation of obj
"""
# handle concrete class serialization
if hasattr(obj, '__metaclass__') and obj.__metaclass__.__name__ == 'Element':
json = {} # { '__classname__' : type(obj).__name__ }
json.update(vars(obj))
json.pop('__metaclass__', None) # remove __metaclass__ from json
# handle abstract class serialization
elif obj.__class__.__name__ == 'type' and obj.__name__ == 'Element':
json = obj.__name__
elif obj.__class__.__name__ == 'ViewStyle':
json = {}
json.update(vars(obj))
else:
json = obj.__str__()
return json
评论列表
文章目录