Customize serialization / deserialization of java pojo

Hi,

I was wondering if there is an example of how to create and register a custom serializer for the DataConverter for a specific data type. Ex. I have a data type that has defined interfaces within it, so the default deserializer/serializer won’t be able to determine what type the interfaces really are so I need to use a couple custom adapter registered to gson for example. So what im looking for is to specify that for this type, use this serailizer/deserializer for the DataCoverter.

I saw a post referencing sdk-java/PayloadConverter.java at master · temporalio/sdk-java · GitHub and this sdk-java/DefaultDataConverter.java at 45de2dc97486f38b57d5320eb9fdd04e098bd6f0 · temporalio/sdk-java · GitHub, but its not clear how the specific type is registered to a specific converter payload.

Thanks,
Derek

The default DataConverter uses JacksonJsonPayloadConverter to serialize Java types. Please consult the Jackson documentation on how to annotate your classes to support polymorphic types.

Here is one of the possible answers from the stackoverflow.

The problem is that the data types are from a third party library that I don’t control so Im not able to modify or annotate the classes…

Then you can configure Jackson object mapper configuration itself. Something like:

    ObjectMapper objectMapper =
        new ObjectMapper()
            .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true)
            .registerModule(new JavaTimeModule());
    DataConverter converter =
        DefaultDataConverter.getDefaultInstance()
            .withPayloadConverterOverrides(new JacksonJsonPayloadConverter(objectMapper));