Skip to content
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

Closed
wants to merge 3 commits into from

Conversation

AlexDo1
Copy link
Collaborator

@AlexDo1 AlexDo1 commented Jan 22, 2024

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 the export 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.

@AlexDo1 AlexDo1 added enhancement New feature or request tests Anything related to e2e-tests, unit-tests or migration-tests labels Jan 22, 2024
@AlexDo1 AlexDo1 requested a review from mmaelicke January 22, 2024 16:44
Comment on lines +423 to +429
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)
Copy link
Member

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)
Copy link
Member

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?

Comment on lines +435 to +436
with open(path, 'w') as f:
json.dump(entry_dict, f, indent=4, cls=DictToSerializableJsonEncoder)
Copy link
Member

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?

@mmaelicke
Copy link
Member

We decided not to take this update

@mmaelicke mmaelicke closed this Jan 25, 2024
@mmaelicke mmaelicke deleted the entry_to_json branch January 25, 2024 07:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request tests Anything related to e2e-tests, unit-tests or migration-tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants