Revert changes with ObjectSerializer class.

This commit is contained in:
Andrey Bogdanov 2021-03-11 09:52:57 +03:00
parent 4c0d043fb4
commit a3c7056be2
5 changed files with 32 additions and 110 deletions

View File

@ -28,6 +28,8 @@ class DaprStateAsyncProvider {
*/ */
private static final Charset CHARSET = Properties.STRING_CHARSET.get(); private static final Charset CHARSET = Properties.STRING_CHARSET.get();
public static final String JSON_CONTENT_TYPE = "application/json";
/** /**
* Handles special serialization cases. * Handles special serialization cases.
*/ */
@ -57,7 +59,7 @@ class DaprStateAsyncProvider {
DaprStateAsyncProvider(DaprClient daprClient, DaprObjectSerializer stateSerializer) { DaprStateAsyncProvider(DaprClient daprClient, DaprObjectSerializer stateSerializer) {
this.daprClient = daprClient; this.daprClient = daprClient;
this.stateSerializer = stateSerializer; this.stateSerializer = stateSerializer;
this.isStateSerializerJson = DefaultObjectSerializer.JSON_CONTENT_TYPE.equals(stateSerializer.getContentType()); this.isStateSerializerJson = JSON_CONTENT_TYPE.equals(stateSerializer.getContentType());
} }
<T> Mono<T> load(String actorType, ActorId actorId, String stateName, TypeRef<T> type) { <T> Mono<T> load(String actorType, ActorId actorId, String stateName, TypeRef<T> type) {

View File

@ -15,6 +15,7 @@ import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Objects; import java.util.Objects;
@ -28,18 +29,26 @@ public class DaprStateAsyncProviderTest {
private static final DaprObjectSerializer SERIALIZER = new DefaultObjectSerializer(); private static final DaprObjectSerializer SERIALIZER = new DefaultObjectSerializer();
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final double EPSILON = 1e-10; private static final double EPSILON = 1e-10;
class CustomJsonSerializer extends ObjectSerializer implements DaprObjectSerializer{ class CustomJsonSerializer implements DaprObjectSerializer{
CustomJsonSerializer() { private final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
super(DaprStateAsyncProviderTest.OBJECT_MAPPER);
} @Override
public byte[] serialize(Object o) throws IOException {
return OBJECT_MAPPER.writeValueAsBytes(o);
}
@Override
public <T> T deserialize(byte[] data, TypeRef<T> type) throws IOException {
return OBJECT_MAPPER.readValue(data, OBJECT_MAPPER.constructType(type.getType()));
}
@Override @Override
public String getContentType() { public String getContentType() {
return DefaultObjectSerializer.JSON_CONTENT_TYPE; return "application/json";
} }
} }

View File

@ -22,22 +22,17 @@ import java.lang.reflect.Method;
*/ */
public class ObjectSerializer { public class ObjectSerializer {
private final ObjectMapper objectMapper; /**
* Shared Json serializer/deserializer as per Jackson's documentation.
*/
protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
/** /**
* Default constructor to avoid class from being instantiated outside package but still inherited. * Default constructor to avoid class from being instantiated outside package but still inherited.
*/ */
protected ObjectSerializer() { protected ObjectSerializer() {
objectMapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
/**
* Default constructor to avoid class from being instantiated outside package but still inherited.
*/
protected ObjectSerializer(ObjectMapper mapper) {
objectMapper = mapper;
} }
/** /**
@ -67,7 +62,7 @@ public class ObjectSerializer {
} }
// Not string, not primitive, so it is a complex type: we use JSON for that. // Not string, not primitive, so it is a complex type: we use JSON for that.
return getObjectMapper().writeValueAsBytes(state); return OBJECT_MAPPER.writeValueAsBytes(state);
} }
/** /**
@ -80,7 +75,7 @@ public class ObjectSerializer {
* @throws IOException In case content cannot be deserialized. * @throws IOException In case content cannot be deserialized.
*/ */
public <T> T deserialize(byte[] content, TypeRef<T> type) throws IOException { public <T> T deserialize(byte[] content, TypeRef<T> type) throws IOException {
return deserialize(content, getObjectMapper().constructType(type.getType())); return deserialize(content, OBJECT_MAPPER.constructType(type.getType()));
} }
/** /**
@ -93,7 +88,7 @@ public class ObjectSerializer {
* @throws IOException In case content cannot be deserialized. * @throws IOException In case content cannot be deserialized.
*/ */
public <T> T deserialize(byte[] content, Class<T> clazz) throws IOException { public <T> T deserialize(byte[] content, Class<T> clazz) throws IOException {
return deserialize(content, getObjectMapper().constructType(clazz)); return deserialize(content, OBJECT_MAPPER.constructType(clazz));
} }
private <T> T deserialize(byte[] content, JavaType javaType) throws IOException { private <T> T deserialize(byte[] content, JavaType javaType) throws IOException {
@ -135,7 +130,7 @@ public class ObjectSerializer {
} }
} }
return getObjectMapper().readValue(content, javaType); return OBJECT_MAPPER.readValue(content, javaType);
} }
/** /**
@ -146,7 +141,7 @@ public class ObjectSerializer {
* @throws IOException In case content cannot be parsed. * @throws IOException In case content cannot be parsed.
*/ */
public JsonNode parseNode(byte[] content) throws IOException { public JsonNode parseNode(byte[] content) throws IOException {
return getObjectMapper().readTree(content); return OBJECT_MAPPER.readTree(content);
} }
/** /**
@ -158,7 +153,7 @@ public class ObjectSerializer {
* @return Result as corresponding type. * @return Result as corresponding type.
* @throws IOException if cannot deserialize primitive time. * @throws IOException if cannot deserialize primitive time.
*/ */
private <T> T deserializePrimitives(byte[] content, JavaType javaType) throws IOException { private static <T> T deserializePrimitives(byte[] content, JavaType javaType) throws IOException {
if ((content == null) || (content.length == 0)) { if ((content == null) || (content.length == 0)) {
if (javaType.hasRawClass(boolean.class)) { if (javaType.hasRawClass(boolean.class)) {
return (T) Boolean.FALSE; return (T) Boolean.FALSE;
@ -195,13 +190,6 @@ public class ObjectSerializer {
return null; return null;
} }
return getObjectMapper().readValue(content, javaType); return OBJECT_MAPPER.readValue(content, javaType);
}
/**
* Shared Json serializer/deserializer as per Jackson's documentation.
*/
protected ObjectMapper getObjectMapper() {
return objectMapper;
} }
} }

View File

@ -15,8 +15,6 @@ import java.io.IOException;
*/ */
public class DefaultObjectSerializer extends ObjectSerializer implements DaprObjectSerializer { public class DefaultObjectSerializer extends ObjectSerializer implements DaprObjectSerializer {
public static final String JSON_CONTENT_TYPE = "application/json";
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -38,6 +36,6 @@ public class DefaultObjectSerializer extends ObjectSerializer implements DaprObj
*/ */
@Override @Override
public String getContentType() { public String getContentType() {
return JSON_CONTENT_TYPE; return "application/json";
} }
} }

View File

@ -1,75 +0,0 @@
/*
* Copyright (c) Microsoft Corporation and Dapr Contributors.
* Licensed under the MIT License.
*/
package io.dapr.serializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.dapr.client.ObjectSerializer;
import io.dapr.utils.TypeRef;
import org.junit.Test;
import java.io.IOException;
import java.lang.reflect.Type;
import static org.junit.Assert.*;
public class CustomJsonObjectSerializerTest {
public static class CustomJsonSerializer extends ObjectSerializer implements DaprObjectSerializer {
CustomJsonSerializer() {
super(new ObjectMapper());
}
public ObjectMapper getObjectMapper() {
return super.getObjectMapper();
}
@Override
public String getContentType() {
return DefaultObjectSerializer.JSON_CONTENT_TYPE;
}
}
CustomJsonSerializer SERIALIZER = new CustomJsonSerializer();
@Test
public void serializeObjectTest() {
DefaultObjectSerializerTest.MyObjectTestToSerialize obj = new DefaultObjectSerializerTest.MyObjectTestToSerialize();
obj.setStringValue("A String");
obj.setIntValue(2147483647);
obj.setBoolValue(true);
obj.setCharValue('a');
obj.setByteValue((byte) 65);
obj.setShortValue((short) 32767);
obj.setLongValue(9223372036854775807L);
obj.setFloatValue(1.0f);
obj.setDoubleValue(1000.0);
//String expectedResult = "{\"stringValue\":\"A String\",\"intValue\":2147483647,\"boolValue\":true,\"charValue\":\"a\",\"byteValue\":65,\"shortValue\":32767,\"longValue\":9223372036854775807,\"floatValue\":1.0,\"doubleValue\":1000.0}";
byte[] serializedValue;
try {
serializedValue = SERIALIZER.serialize(obj);
assertNotNull(serializedValue);
DefaultObjectSerializerTest.MyObjectTestToSerialize deserializedValue = SERIALIZER.deserialize(serializedValue, DefaultObjectSerializerTest.MyObjectTestToSerialize.class);
assertEquals(obj, deserializedValue);
} catch (IOException exception) {
fail(exception.getMessage());
}
try {
serializedValue = SERIALIZER.serialize(obj);
assertNotNull(serializedValue);
Type t = DefaultObjectSerializerTest.MyObjectTestToSerialize.class;
TypeRef<DefaultObjectSerializerTest.MyObjectTestToSerialize> tr = TypeRef.get(t);
DefaultObjectSerializerTest.MyObjectTestToSerialize deserializedValue = SERIALIZER.deserialize(serializedValue, tr);
assertEquals(obj, deserializedValue);
} catch (IOException exception) {
fail(exception.getMessage());
}
assertNotNull(SERIALIZER.getObjectMapper());
}
}