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

Custom serializer for BuiltMap #234

Closed
andreavaccari opened this issue Apr 17, 2021 · 1 comment
Closed

Custom serializer for BuiltMap #234

andreavaccari opened this issue Apr 17, 2021 · 1 comment
Assignees
Labels

Comments

@andreavaccari
Copy link

Hi @davidmorgan, I have a User entity defined with built_value and including a BuiltMap. I use StandardJsonSerializer to produce objects I persist to Firestore. I'd like to specify a custom serializer for the BuiltMap field so that it's serialized to an object with shape { ids: [...], entities: {...} }.

The reasons for this are: 1) we need to maintain insertion order and Firestore reorders map keys alphabetically; 2) we can't simply serialize the map to a list because some Firestore lookups only work on maps. This pattern is common enough that Redux offers a standardized solution.

Could you give me a hint on the best way to accomplish this? Thank you!

@davidmorgan davidmorgan self-assigned this Jun 7, 2021
@davidmorgan
Copy link
Contributor

This is a built_value question rather than a built_collection question :) so please re-open over there if you still have questions.

You could replace the existing BuiltMap serializer with a fully custom one, i.e. write a replacement for the built-in BuiltMapSerializer. This only works if you want all BuiltMap fields to work like that.

Otherwise, I'm afraid there isn't a way to set a different serializer per field--it's always based on type. I think you'd need to create your own type which is basically a copy of BuiltMap but comes with its own serializer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants