Skip to content

Allow customizable "reading" of JSON maps #1045

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 5 commits into from
Nov 29, 2021
Merged

Conversation

kevmoo
Copy link
Collaborator

@kevmoo kevmoo commented Nov 29, 2021

Allows aliasing values to different names or computing a single
field value from any aspect of the source JSON map

Fixes #144
Fixes #888

Allows aliasing values to different names or computing a single
field value from any aspect of the source JSON map

Fixes #144
Fixes #888
@kevmoo kevmoo force-pushed the i144_jsonKey_readValue branch from 69ad071 to efb85de Compare November 29, 2021 20:44
S Function<S>(
String,
_CastFunction<S>, {
Object? Function(Map, String)? readValue,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should readValue also return a T here to give better static checking? In general the relationship here between the _CastFunction and readValue params is a bit murky to me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will investigate...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So this is a bit weird.

readValue is meant to be pre-converter. So it should return a JSON-ish result. that then goes through any associated converters, etc.

If one wants readValue to return the field type, they have to also create a converter.

I pondered doing the logic where readValue just takes over ALL conversion logic, too. But that gets tricky.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ya, that is what I was wondering about. This would cover some number of use cases, where one or more json fields can be merged into a new json-like field, but its a bit indirect for others. For instance you might have people actually returning a Map from readValue, built from some other fields, and then deserializing that map into some class in the regular conversion function.

Ultimately I think this choice is probably fine, but I would clarify things in the documentation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added more docs. PTAL

Copy link
Member

Choose a reason for hiding this comment

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

I pondered doing the logic where readValue just takes over ALL conversion logic, too. But that gets tricky.

Tricky for users or for this library?

It feels like a simpler concept to document if readValue does all the work instead of coordinating with some other uncoupled concept with no static types in between...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It could be both. Imagine a case where I just want to rename a field foo -> bar but the type is something complex like Map<int, List<Duration?>>? – that's a bear to rewrite for the user.

@kevmoo kevmoo merged commit d2fe514 into master Nov 29, 2021
@kevmoo kevmoo deleted the i144_jsonKey_readValue branch November 29, 2021 22:57
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

Successfully merging this pull request may close these issues.

Serialize multiple JSON key in a single variable Consider support aliases for name: for @JsonKey
3 participants