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

Support java record deserialization on Android #4166

Closed
eranl opened this issue Oct 21, 2023 · 5 comments
Closed

Support java record deserialization on Android #4166

eranl opened this issue Oct 21, 2023 · 5 comments

Comments

@eranl
Copy link

eranl commented Oct 21, 2023

Is your feature request related to a problem? Please describe.

On a range of Android SDK versions <34, java records are supported through desugaring. Jackson's built-in support for records doesn't work on these platforms, since the classes have a non-standard super class, and record component-related reflection methods are missing.

Serialization of records works fine with setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY), but deserialization fails there.

Describe the solution you'd like

I created a Jackson module that adds automatic support for Android-desugared record deserialization, by matching constructor signatures with field types. It has one minor limitation: it doesn't allow a deserialized record to have a custom constructor with a signature that's any permutation of the canonical one's.

Can I contribute this module? How?

Usage example

No response

Additional context

No response

@eranl eranl added the to-evaluate Issue that has been received but not yet evaluated label Oct 21, 2023
@cowtowncoder
Copy link
Member

This sounds like a cool module idea; use of Fields as a work-around isn't good approach.
I think module, instead of trying to make jackson-databind include it, makes sense.

As to how to contribute... my initial thinking is that if build could work on JavaSE platform, module could be included as part of jackson-modules-base. But if specific build is needed, maybe it should be stand-alone repo.
And if/when there would be more Android-specific modules we could have multi-Maven-project repo ("jackson-modules-android"?).

But much depends on details.

@cowtowncoder cowtowncoder removed the to-evaluate Issue that has been received but not yet evaluated label Oct 24, 2023
@eranl
Copy link
Author

eranl commented Oct 27, 2023

Just posted a PR.

... use of Fields as a work-around isn't good approach. ...

I'm curious: what did you mean by this?

@cowtowncoder
Copy link
Member

@eranl I mean that forcing direct access to non-public fields can become problematic, compared to using accessors.
I probably should have said "relying on being able to access underlying Fields is problematic".

@eranl
Copy link
Author

eranl commented Nov 3, 2023

I see. The code uses field metadata, but doesn't access fields for reading/writing data.

@cowtowncoder
Copy link
Member

Implemented via FasterXML/jackson-modules-base#230 -- closing

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