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

Add JavaTimeModule #1065

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<artifactId>jackson-databind</artifactId>
<version>2.15.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.15.1</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
Expand Down
32 changes: 24 additions & 8 deletions sdk/src/main/java/io/dapr/client/ObjectSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.google.protobuf.MessageLite;
import io.dapr.client.domain.CloudEvent;
import io.dapr.utils.TypeRef;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Objects;

/**
* Serializes and deserializes an internal object.
Expand All @@ -35,16 +38,29 @@ public class ObjectSerializer {
*/
protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.registerModule(new JavaTimeModule())
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.setSerializationInclusion(JsonInclude.Include.NON_NULL);

protected ObjectMapper objectMapper;

/**
* Default constructor to avoid class from being instantiated outside package but still inherited.
*/
protected ObjectSerializer() {
this(OBJECT_MAPPER);
}

protected ObjectSerializer(final ObjectMapper objectMapper) {
this.objectMapper = Objects.requireNonNullElse(objectMapper, OBJECT_MAPPER);
}

public static ObjectSerializer withObjectMapper(final ObjectMapper objectMapper) {
return new ObjectSerializer(objectMapper);
}

/**
* Serializes a given state object into byte array.
* Serializes a given state object into a byte array.
*
* @param state State object to be serialized.
* @return Array of bytes[] with the serialized content.
Expand All @@ -70,7 +86,7 @@ public byte[] serialize(Object state) throws IOException {
}

// Not string, not primitive, so it is a complex type: we use JSON for that.
return OBJECT_MAPPER.writeValueAsBytes(state);
return objectMapper.writeValueAsBytes(state);
}

/**
Expand All @@ -83,7 +99,7 @@ public byte[] serialize(Object state) throws IOException {
* @throws IOException In case content cannot be deserialized.
*/
public <T> T deserialize(byte[] content, TypeRef<T> type) throws IOException {
return deserialize(content, OBJECT_MAPPER.constructType(type.getType()));
return deserialize(content, objectMapper.constructType(type.getType()));
}

/**
Expand All @@ -96,7 +112,7 @@ public <T> T deserialize(byte[] content, TypeRef<T> type) throws IOException {
* @throws IOException In case content cannot be deserialized.
*/
public <T> T deserialize(byte[] content, Class<T> clazz) throws IOException {
return deserialize(content, OBJECT_MAPPER.constructType(clazz));
return deserialize(content, objectMapper.constructType(clazz));
}

private <T> T deserialize(byte[] content, JavaType javaType) throws IOException {
Expand Down Expand Up @@ -138,7 +154,7 @@ private <T> T deserialize(byte[] content, JavaType javaType) throws IOException
}
}

return OBJECT_MAPPER.readValue(content, javaType);
return objectMapper.readValue(content, javaType);
}

/**
Expand All @@ -149,7 +165,7 @@ private <T> T deserialize(byte[] content, JavaType javaType) throws IOException
* @throws IOException In case content cannot be parsed.
*/
public JsonNode parseNode(byte[] content) throws IOException {
return OBJECT_MAPPER.readTree(content);
return OBJECT_MAPPER.readTree(content);
}

/**
Expand All @@ -161,7 +177,7 @@ public JsonNode parseNode(byte[] content) throws IOException {
* @return Result as corresponding type.
* @throws IOException if cannot deserialize primitive time.
*/
private static <T> T deserializePrimitives(byte[] content, JavaType javaType) throws IOException {
public <T> T deserializePrimitives(byte[] content, JavaType javaType) throws IOException {
if ((content == null) || (content.length == 0)) {
if (javaType.hasRawClass(boolean.class)) {
return (T) Boolean.FALSE;
Expand Down Expand Up @@ -198,6 +214,6 @@ private static <T> T deserializePrimitives(byte[] content, JavaType javaType) th
return null;
}

return OBJECT_MAPPER.readValue(content, javaType);
return objectMapper.readValue(content, javaType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.dapr.serializer;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.dapr.client.ObjectSerializer;
import io.dapr.utils.TypeRef;

import java.io.IOException;
import java.util.Objects;

public class CustomizableDaprObjectSerializer implements DaprObjectSerializer {

private final ObjectSerializer delegate;

public CustomizableDaprObjectSerializer(final ObjectMapper objectMapper) {
ObjectMapper nonNullableObjectMapper = Objects.requireNonNull(objectMapper);
this.delegate = ObjectSerializer.withObjectMapper(nonNullableObjectMapper);
}

@Override
public byte[] serialize(Object o) throws IOException {
return this.delegate.serialize(o);
}

@Override
public <T> T deserialize(byte[] data, TypeRef<T> type) throws IOException {
return deserialize(data, type);
}

@Override
public String getContentType() {
return "application/json";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.dapr.serializer;

import com.fasterxml.jackson.databind.ObjectMapper;

public interface DaprObjectSerializerFactory {

static DaprObjectSerializer createDefaultSerializer() {
return new DefaultObjectSerializer();
}

static DaprObjectSerializer createJacksonSerializer(final ObjectMapper objectMapper) {
return new CustomizableDaprObjectSerializer(objectMapper);
}
}
2 changes: 1 addition & 1 deletion sdk/src/test/java/io/dapr/client/DaprClientHttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class DaprClientHttpTest {
private static final int HTTP_SERVER_ERROR = 500;
private static final int HTTP_OK = 200;
private static final Duration READ_TIMEOUT = Duration.ofSeconds(60);

private String sidecarIp;

private String daprApiToken;
Expand Down
Loading