Skip to content

Commit b1c8a46

Browse files
authored
bugfix: jackson serializing can compatible with empty beans (#70)
* bugfix: jackson serializing can compatible with empty beans
1 parent 3586eb1 commit b1c8a46

File tree

3 files changed

+5
-9
lines changed

3 files changed

+5
-9
lines changed

trpc-core/src/main/java/com/tencent/trpc/core/utils/JsonUtils.java

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.fasterxml.jackson.databind.DeserializationFeature;
1717
import com.fasterxml.jackson.databind.JavaType;
1818
import com.fasterxml.jackson.databind.ObjectMapper;
19+
import com.fasterxml.jackson.databind.SerializationFeature;
1920
import com.tencent.trpc.core.exception.ErrorCode;
2021
import com.tencent.trpc.core.exception.TRpcException;
2122
import com.tencent.trpc.core.logger.Logger;
@@ -39,6 +40,8 @@ public class JsonUtils {
3940
objectMapper.setSerializationInclusion(Include.NON_NULL);
4041
// Do not throw an error when deserializing if there are no corresponding properties
4142
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
43+
// Do not throw an error when serializing if there are no public fields
44+
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
4245
}
4346

4447
/**

trpc-core/src/test/java/com/tencent/trpc/core/serialization/JsonSerializationTest.java

-8
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,6 @@ public void testJson2Pojo() {
6161
*/
6262
@Test
6363
public void testJson2PojoEx() {
64-
try {
65-
JsonUtilsTest.TestObj1 obj1 = new JsonUtilsTest.TestObj1();
66-
obj1.setTest(123);
67-
serial.serialize(obj1);
68-
Assert.fail();
69-
} catch (Exception e) {
70-
Assert.assertTrue(e.getMessage().contains("json serialize ex:"));
71-
}
7264
try {
7365
byte[] bytes = serial.serialize(obj);
7466
GenericsObj deserialize = serial.deserialize(bytes, GenericsObj.class);

trpc-core/src/test/java/com/tencent/trpc/core/utils/JsonUtilsTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class JsonUtilsTest {
3232
private static final String JSON = "{\"test\":123}";
3333
private static final String JSON_LIST = "[{\"test\":123},{\"test\":123}]";
3434
private static final String ERROR_JSON = "{\"test\":123,A}";
35+
private static final String EMPTY_JSON = "{}";
3536

3637
@Test
3738
public void testCopy() {
@@ -134,7 +135,7 @@ public void testToJsonWithDefaultValue() {
134135
TestObj1 obj1 = new TestObj1();
135136
obj1.setTest(123);
136137
String aaa = JsonUtils.toJson(obj1, "aaa");
137-
Assert.assertEquals("aaa", aaa);
138+
Assert.assertEquals(EMPTY_JSON, aaa);
138139
}
139140

140141
@Test

0 commit comments

Comments
 (0)