vaadin / flow

Vaadin Flow is a Java framework binding Vaadin web components to Java. This is part of Vaadin 10+.
Apache License 2.0
622 stars 166 forks source link

Make the JSON ObjectMapper accessible #19673

Open JannikBirn opened 5 months ago

JannikBirn commented 5 months ago

Describe your motivation

I'm using the new ReactAdapterComponent and wan't to read the sate of a value. The type of the field is an object with some optional fields. I would like to represent these optional fields with java.util.Optional in the Java class representation. Because the ReactAdapterComponent uses com.vaadin.flow.internal.JsonUtils im unable to register the necessary Jdk8Module from com.fasterxml.jackson.datatype:jackson-datatype-jdk8 to the ObjectMapper used to deserialize the JSON.

Describe the solution you'd like

The best solution would be to make the com.vaadin.flow.internal.JsonUtils ObjectMapper somehow accessible so it would be possible to add Custom Modules. In the Hilla docs is a section about how to access the ObjectMapper in Hilla applications the same solution could be applied to Flow (https://vaadin.com/docs/latest/hilla/lit/reference/type-conversion#json-object-mapper).

An alternative solution would be to make the protected static <T> T readFromJson inside ReactAdapterComponent non-static to easily override the method and redirect the parsing to my own ObjectMapper.

Describe alternatives you've considered

The alternative for me would be a workaround where i have to implement basically all methods of the ReactAdapterComponent myself.

Additional context

I'm currently using the Vaadin Flow 24.5.0.alpha4, but i think that this problem is already existing in the 24.4.3 release.

roeltje25 commented 3 months ago

Why not do this in a @WebListener somewhere?

            Field objectMapperFld = JsonUtils.class.getDeclaredField( "objectMapper" );
            objectMapperFld.setAccessible( true );
            ObjectMapper objectMapper = (ObjectMapper) objectMapperFld.get( null );