-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add to_json method for exporting Entry metadata to JSON #306
Conversation
class DictToSerializableJsonEncoder(json.JSONEncoder): | ||
def default(self, obj): | ||
if isinstance(obj, Decimal): | ||
return str(obj) | ||
if isinstance(obj, dt): | ||
return obj.isoformat() | ||
return super().default(obj) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's define this in some kind of utility file, so that other models can use it as well.
class DictToSerializableJsonEncoder(json.JSONEncoder): | ||
def default(self, obj): | ||
if isinstance(obj, Decimal): | ||
return str(obj) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would make more sense to convert the Decimal to a float, right?
with open(path, 'w') as f: | ||
json.dump(entry_dict, f, indent=4, cls=DictToSerializableJsonEncoder) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok.
Other option is to keep the writing to file out of meta catalog and implement the class directly into the tool, where it is used. What do you think?
We decided not to take this update |
I decided to go for a new method
Entry.to_json
for serializable entry-to-json-export. I think that's easier than trying to fix theexport
extension.The created json should be serializable as I convert non-json datatypes to json datatypes with a custom JsonEncoder.
If we want to go for another way, we can revert this, but for now this should work.