Skip to content

Commit 9e674b6

Browse files
authored
Merge pull request #85 from reactivegroup/feature/serializer
feat: update serializer
2 parents 060d204 + 54ede05 commit 9e674b6

File tree

16 files changed

+110
-44
lines changed

16 files changed

+110
-44
lines changed

examples/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<parent>
2424
<artifactId>capa-parent</artifactId>
2525
<groupId>group.rxcloud</groupId>
26-
<version>1.10.10.RELEASE</version>
26+
<version>1.10.11.RELEASE</version>
2727
</parent>
2828

2929
<artifactId>capa-examples</artifactId>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<groupId>group.rxcloud</groupId>
2424
<artifactId>capa-parent</artifactId>
2525
<packaging>pom</packaging>
26-
<version>1.10.10.RELEASE</version>
26+
<version>1.10.11.RELEASE</version>
2727
<name>capa-sdk-parent</name>
2828
<description>SDK for Capa.</description>
2929
<url>https://github.com/reactivegroup</url>

sdk-component/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<parent>
2424
<groupId>group.rxcloud</groupId>
2525
<artifactId>capa-parent</artifactId>
26-
<version>1.10.10.RELEASE</version>
26+
<version>1.10.11.RELEASE</version>
2727
</parent>
2828

2929
<artifactId>capa-sdk-component</artifactId>
@@ -33,7 +33,7 @@
3333
<properties>
3434
<okhttp.version>4.9.1</okhttp.version>
3535
<kotlin-stdlib.version>1.4.10</kotlin-stdlib.version>
36-
<log4j.version>2.14.1</log4j.version>
36+
<log4j.version>2.15.0</log4j.version>
3737
<logback.version>1.2.3</logback.version>
3838
</properties>
3939

sdk-component/src/main/java/group/rxcloud/capa/component/log/CapaLogbackAppenderAgent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* The agent of the logback impl.
2525
*/
2626
public class CapaLogbackAppenderAgent extends UnsynchronizedAppenderBase<ILoggingEvent> {
27+
2728
/**
2829
* The log component type.
2930
*/

sdk-component/src/test/java/group/rxcloud/capa/component/configstore/TestObjectSerializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
package group.rxcloud.capa.component.configstore;
1818

1919
import group.rxcloud.capa.infrastructure.serializer.CapaObjectSerializer;
20-
import group.rxcloud.capa.infrastructure.serializer.ObjectSerializer;
20+
import group.rxcloud.capa.infrastructure.serializer.ExtensionObjectSerializer;
2121

2222
/**
2323
* serializer/deserializer for request/response objects used in tests only
2424
*/
25-
public class TestObjectSerializer extends ObjectSerializer implements CapaObjectSerializer {
25+
public class TestObjectSerializer extends ExtensionObjectSerializer implements CapaObjectSerializer {
2626

2727
/**
2828
* {@inheritDoc}

sdk-component/src/test/java/group/rxcloud/capa/component/http/CapaHttpBuilderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import group.rxcloud.capa.infrastructure.serializer.CapaObjectSerializer;
2020
import group.rxcloud.capa.infrastructure.serializer.DefaultObjectSerializer;
21-
import group.rxcloud.capa.infrastructure.serializer.ObjectSerializer;
21+
import group.rxcloud.capa.infrastructure.serializer.ExtensionObjectSerializer;
2222
import org.junit.jupiter.api.Assertions;
2323
import org.junit.jupiter.api.Test;
2424

@@ -53,7 +53,7 @@ public void testBuild_Success() {
5353
/**
5454
* serializer/deserializer for request/response objects used in tests only
5555
*/
56-
private class TestObjectSerializer extends ObjectSerializer implements CapaObjectSerializer {
56+
private class TestObjectSerializer extends ExtensionObjectSerializer implements CapaObjectSerializer {
5757

5858
/**
5959
* {@inheritDoc}

sdk-infrastructure/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<parent>
2424
<artifactId>capa-parent</artifactId>
2525
<groupId>group.rxcloud</groupId>
26-
<version>1.10.10.RELEASE</version>
26+
<version>1.10.11.RELEASE</version>
2727
</parent>
2828

2929
<artifactId>capa-sdk-infrastructure</artifactId>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package group.rxcloud.capa.infrastructure.serializer;
18+
19+
import group.rxcloud.cloudruntimes.utils.TypeRef;
20+
21+
import java.io.IOException;
22+
23+
public abstract class AbstractObjectSerializer implements CapaObjectSerializer {
24+
25+
/**
26+
* {@inheritDoc}
27+
*/
28+
@Override
29+
public byte[] serialize(Object o) throws IOException {
30+
if (o == null) {
31+
return null;
32+
}
33+
if (o instanceof byte[]) {
34+
return (byte[]) o;
35+
}
36+
if (o instanceof String) {
37+
return ((String) o).getBytes();
38+
}
39+
40+
return doSerialize(o);
41+
}
42+
43+
protected abstract byte[] doSerialize(Object o) throws IOException;
44+
45+
/**
46+
* {@inheritDoc}
47+
*/
48+
@Override
49+
public <T> T deserialize(byte[] data, TypeRef<T> type) throws IOException {
50+
Class<T> clazz = (Class<T>) type.getType();
51+
if (data == null) {
52+
return null;
53+
}
54+
if (clazz == byte[].class) {
55+
return (T) data;
56+
}
57+
if (clazz == String.class) {
58+
return (T) new String(data);
59+
}
60+
61+
return doDeserialize(data, type);
62+
}
63+
64+
protected abstract <T> T doDeserialize(byte[] data, TypeRef<T> type) throws IOException;
65+
}

sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/serializer/DefaultObjectSerializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/**
2525
* Default serializer/deserializer for request/response objects.
2626
*/
27-
public class DefaultObjectSerializer extends ObjectSerializer implements CapaObjectSerializer {
27+
public class DefaultObjectSerializer extends ExtensionObjectSerializer implements CapaObjectSerializer {
2828

2929
/**
3030
* {@inheritDoc}

sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/serializer/ObjectSerializer.java renamed to sdk-infrastructure/src/main/java/group/rxcloud/capa/infrastructure/serializer/ExtensionObjectSerializer.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
/**
3232
* Serializes and deserializes an internal object.
3333
*/
34-
public class ObjectSerializer {
34+
public abstract class ExtensionObjectSerializer extends AbstractObjectSerializer {
3535

3636
/**
3737
* Shared Json serializer/deserializer as per Jackson's documentation.
@@ -44,7 +44,7 @@ public class ObjectSerializer {
4444
/**
4545
* Default constructor to avoid class from being instantiated outside package but still inherited.
4646
*/
47-
protected ObjectSerializer() {
47+
protected ExtensionObjectSerializer() {
4848
}
4949

5050
/**
@@ -55,33 +55,31 @@ protected ObjectMapper getObjectMapper() {
5555
}
5656

5757
/**
58-
* Serializes a given state object into byte array.
58+
* Serializes a given object into byte array.
5959
*
60-
* @param state State object to be serialized.
60+
* @param o object to be serialized.
6161
* @return Array of bytes[] with the serialized content.
62-
* @throws IOException In case state cannot be serialized.
62+
* @throws IOException In case o cannot be serialized.
6363
*/
64-
public byte[] serialize(Object state) throws IOException {
65-
if (state == null) {
64+
@Override
65+
public byte[] doSerialize(Object o) throws IOException {
66+
if (o == null) {
6667
return null;
6768
}
68-
69-
if (state.getClass() == Void.class) {
69+
if (o.getClass() == Void.class) {
7070
return null;
7171
}
72-
7372
// Have this check here to be consistent with deserialization (see deserialize() method below).
74-
if (state instanceof byte[]) {
75-
return (byte[]) state;
73+
if (o instanceof byte[]) {
74+
return (byte[]) o;
7675
}
77-
7876
// Proto buffer class is serialized directly.
79-
if (state instanceof MessageLite) {
80-
return ((MessageLite) state).toByteArray();
77+
if (o instanceof MessageLite) {
78+
return ((MessageLite) o).toByteArray();
8179
}
8280

8381
// Not string, not primitive, so it is a complex type: we use JSON for that.
84-
return getObjectMapper().writeValueAsBytes(state);
82+
return getObjectMapper().writeValueAsBytes(o);
8583
}
8684

8785
/**
@@ -93,8 +91,9 @@ public byte[] serialize(Object state) throws IOException {
9391
* @return Object of type T.
9492
* @throws IOException In case content cannot be deserialized.
9593
*/
96-
public <T> T deserialize(byte[] content, TypeRef<T> type) throws IOException {
97-
return deserialize(content, getObjectMapper().constructType(type.getType()));
94+
@Override
95+
public <T> T doDeserialize(byte[] content, TypeRef<T> type) throws IOException {
96+
return doDeserialize(content, getObjectMapper().constructType(type.getType()));
9897
}
9998

10099
/**
@@ -106,11 +105,11 @@ public <T> T deserialize(byte[] content, TypeRef<T> type) throws IOException {
106105
* @return Object of type T.
107106
* @throws IOException In case content cannot be deserialized.
108107
*/
109-
public <T> T deserialize(byte[] content, Class<T> clazz) throws IOException {
110-
return deserialize(content, getObjectMapper().constructType(clazz));
108+
public <T> T doDeserialize(byte[] content, Class<T> clazz) throws IOException {
109+
return doDeserialize(content, getObjectMapper().constructType(clazz));
111110
}
112111

113-
private <T> T deserialize(byte[] content, JavaType javaType) throws IOException {
112+
private <T> T doDeserialize(byte[] content, JavaType javaType) throws IOException {
114113
if ((javaType == null) || javaType.isTypeOrSubTypeOf(Void.class)) {
115114
return null;
116115
}

0 commit comments

Comments
 (0)