Fatal1ty / mashumaro

Fast and well tested serialization library
Apache License 2.0
774 stars 45 forks source link

Add sort keys option for dataclasses inheriting from mixin #157

Closed mishamsk closed 1 year ago

mishamsk commented 1 year ago

Hi,

Thought this is small enough to directly open a PR and see if you would support this feature.

What: add a sort_keys option for BaseConfig which would efficiently generate serialization code such that keys are sorted on creation (which in many cases will make much heavier encoder approach unnecessary).

What's missing:

Rationale: I am using key sort (via __post_serialize__ hook) a lot in order to maintain stable serialized view of my objects (those pesky trees). This would make it more efficient (depending on how classes are defined of course).

mishamsk commented 1 year ago

Would you like to add such a case to the tests since you mentioned it? forgot to pull latest changes, so naturally it didn't work for dataclasses in my branch.

now pulled and rebased. added a test + added a README

Fatal1ty commented 1 year ago

@mishamsk Thank you for contribution, I'm merging it now.

mishamsk commented 1 year ago

glad to help!

Fatal1ty commented 1 year ago

@mishamsk

I just realised that I was wrong. Config options are not propagated to inner dataclasses by design, and the tests didn't show it because two dictionaries with different key order are equal. I created the pull request to fix the tests.

mishamsk commented 1 year ago

@Fatal1ty ouch. sorry. my bad!