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

Allow key-fn like hook for value #168

Open
sunng87 opened this issue Sep 16, 2020 · 2 comments
Open

Allow key-fn like hook for value #168

sunng87 opened this issue Sep 16, 2020 · 2 comments

Comments

@sunng87
Copy link

sunng87 commented Sep 16, 2020

In our web application, we have a use case to transform all value from long to string when its key is found in a given key set. Currently, this was implemented as a ring middleware prior to serialization. It walks through the ring body then do the transformation. However, this operation eats quite lot of CPU time and quickly becomes the bottleneck of our application.

To optimize this behaviour, I hacked cheshire's generate-map function to add a hook on the value. At the moment, there is no way to extend the default behaviour, so I have to reimplement generate-map/generate-array and generate in cheshire.generate to make it work.

There is key-fn option to transform keyword key in object. A further step can be adding key-value-fn that transforms the value with both key and value provided as context.

If this idea makes sense to you, I would like to create a pull request for this.

@dakrone
Copy link
Owner

dakrone commented Sep 17, 2020

I think it's useful, but I'm hesitant to include it because of the scope creep that it adds to Cheshire. I'm concerned that we essentially do the work of a general purpose map transformation function library.

I'm not sure how to reconcile that, I'd be interested if there were any other folks who wanted to weigh in with their opinion on it.

@sunng87
Copy link
Author

sunng87 commented Sep 18, 2020

@dakrone Thank you for your response. I can understand your concern. The reason I want to do it within cheshire is mostly about performance. Since cheshire has to walk through the data structure in order to serialize it, it's the ideal phase to do it.

From another perspective, this key-value-fn is a little bit like @JsonSerialize annotation of jackson which could customize serializer on a field level.

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

No branches or pull requests

2 participants