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

Deserialization of records doesn't work in Java15 #151

Closed
reuschling opened this issue Feb 25, 2021 · 1 comment
Closed

Deserialization of records doesn't work in Java15 #151

reuschling opened this issue Feb 25, 2021 · 1 comment

Comments

@reuschling
Copy link
Contributor

If you want to deserialize a record, you get an IllegalAccessException: Can not set final ... field .

I assume the problem will exist also in the new Java16 which comes out next week, where records won't be a preview feature anymore.

Example code:

    public static record TestRecord(String str, boolean bool)
    {

    }

...

        TestRecord rec = new TestRecord("check", false);

        String strJson = JsonWriter.objectToJson(rec);
        System.out.println(strJson);

        TestRecord clone = (TestRecord) JsonReader.jsonToJava(strJson);
        System.out.println(clone);

Gson also has this issue: google/gson#1794. According this, the solution would be to use the constructor in favour of setting the final fields via reflection.

@jdereg
Copy link
Owner

jdereg commented Oct 14, 2022

json-io has the assign instantiator customization that lets you write the code that creates the object, which allows you to set up the object in regular code - your code is called when the troubled class is encountered. This is dicussed in the user-guide, linked here: https://github.com/jdereg/json-io/blob/master/user-guide.md under the Customization section. Also, recent changes have been added to support JDK's through 17.

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