Proper serialization of java.time.Instant

Hi folks,

I’m trying to configure jackson to serialize java.time.Instant as a string (“2022-02-16T10:56:57.097944Z”) instead of a timestamp.

As the documentation mentioned I implemented a configuration class:

public class JacksonDataFormatConfigurator implements DataFormatConfigurator<JacksonJsonDataFormat> {
    public void configure(JacksonJsonDataFormat dataFormat) {
        ObjectMapper objectMapper = dataFormat.getObjectMapper();
       objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    }

    public Class<JacksonJsonDataFormat> getDataFormatClass() {
        return JacksonJsonDataFormat.class;
    }
}

and specified it in the file: src/main/resources/META-INF/services/org.camunda.spin.spi.DataFormatConfigurator

com.easydoc.bpm.config.JacksonDataFormatConfigurator

Now I see proper serialized variable value in Cockpit:

image

But when I’m trying to get the variable value via REST API, it still serialized as a timestamp :smiling_imp:

GET http://localhost:8080/engine-rest/task/29428d9f-8f17-11ec-8b23-ee2497a1ef63/form-variables

{
  "dto": {
    "type": "Object",
    "value": {
      "createDate": {
        "nano": 97944000,
        "epochSecond": 1645009017
      },
      "description": "TEST"
    },
    "valueInfo": {
      "objectTypeName": "com.sandbox.bpm.instant.TestDto",
      "serializationDataFormat": "application/json"
    }
  }
}

Environment configuration:

Camunda BPM v. 7.16, Embedded Engine (Spring Boot v. 2.5.4).

camunda.bpm.default-serialization-format: application/json

pom.xml (jackson/Spin related dependencies, others omitted)

    <dependency>
      <groupId>org.camunda.bpm</groupId>
      <artifactId>camunda-engine-plugin-spin</artifactId>
    </dependency>
    <dependency>
      <groupId>org.camunda.spin</groupId>
      <artifactId>camunda-spin-dataformat-json-jackson</artifactId>
    </dependency>

Any help would be appreciated :slight_smile:

WBR, Slava

I found an answer in the REST API documentation. It’s a magic parameter deserializeValues (deserializeValues). :slight_smile:

Determines whether serializable variable values (typically variables that store custom Java objects) should be deserialized on server side (default true ).

If set to true, a serializable variable will be deserialized on server side and transformed to JSON using Jackson’s POJO/bean property introspection feature. Note that this requires the Java classes of the variable value to be on the REST API’s classpath.

If set to false, a serializable variable will be returned in its serialized format. For example, a variable that is serialized as XML will be returned as a JSON string containing XML.

Note: While true is the default value for reasons of backward compatibility, we recommend setting this parameter to false when developing web applications that are independent of the Java process applications deployed to the engine.

Now it looks like expected:

{
  "dto": {
    "type": "Object",
    "value": "{\"createDate\":\"2022-02-16T10:56:57.097944Z\",\"description\":\"TEST\"}",
    "valueInfo": {
      "objectTypeName": "com.easydoc.bpm.instant.TestDto",
      "serializationDataFormat": "application/json"
    }
  }
}

Is it possible to configure such parameter = false as the default behavior?

Hello mms,

I’m facing a similar issue, could you please check the below questions?

  1. Are you setting the Instant variable to the process engine through the Java DelegateExecution?
    e.g.:
delegateExecution.setVariable("dto", myDto);
  1. If yes, could you please provide the Dto object definition?

Unfortunately, if I set an object variable to the process engine (which contains an Instant or LocalDateTime as field), then when I inspect it from Cockpit, I get an exception.