Skip to content

Make sure that the type discriminator is the first property #2730

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

Merged
merged 1 commit into from
Aug 26, 2024

Conversation

flobernd
Copy link
Member

@flobernd flobernd commented Jul 17, 2024

This changes allows to speed up deserialization a lot (for statically typed languages) as the polymorhic variants can be determined by peeking a single JSON key-value pair instead of deserializing a complete object into an intermediate representation.

This as well allows to use built-in polymorphic JSON deserialization with several JSON frameworks (like e.g. System.Text.Json in .NET).

Note:
For other polymorhic objects like Body, Value, Variant, the discriminator is already emitted as the first key already.

Edit:
Needs further work to ensure rest of the properties stay sorted. In the best case, safe-stable-stringify would provide an option to customize the sort order.

@flobernd flobernd requested review from a team, Anaethelion and l-trotta July 17, 2024 12:46
@flobernd flobernd force-pushed the discriminator-first branch 2 times, most recently from f250493 to 8f362fe Compare July 17, 2024 12:51
@flobernd flobernd marked this pull request as draft July 17, 2024 13:03
Copy link
Contributor

@l-trotta l-trotta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still have to check if we can improve the java generator somehow, but in the meantime this doesn't break anything, so LGTM

@flobernd flobernd force-pushed the discriminator-first branch 2 times, most recently from aaf7b33 to 51afabe Compare August 2, 2024 11:17
@flobernd flobernd marked this pull request as ready for review August 2, 2024 11:17
@flobernd
Copy link
Member Author

flobernd commented Aug 2, 2024

I have to wait for the author of safe-stable-stringify to release version 2.5.0 on NPM, before we can merge this. Using the GH repo works locally, but not in the CI.

@flobernd flobernd force-pushed the discriminator-first branch from 51afabe to 4d52271 Compare August 26, 2024 10:35
@flobernd flobernd force-pushed the discriminator-first branch from 4d52271 to fb20b89 Compare August 26, 2024 10:36
@flobernd flobernd merged commit 765d7ce into main Aug 26, 2024
8 checks passed
@flobernd flobernd deleted the discriminator-first branch August 26, 2024 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants