flow icon indicating copy to clipboard operation
flow copied to clipboard

Make the JSON ObjectMapper accessible

Open JannikBirn opened this issue 1 year ago • 1 comments

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.

JannikBirn avatar Jul 04 '24 15:07 JannikBirn

Why not do this in a @WebListener somewhere?

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

roeltje25 avatar Aug 14 '24 14:08 roeltje25