File tree 2 files changed +20
-0
lines changed
main/java/com/tencent/trpc/core/utils
test/java/com/tencent/trpc/core/utils
2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change 16
16
import com .fasterxml .jackson .databind .DeserializationFeature ;
17
17
import com .fasterxml .jackson .databind .JavaType ;
18
18
import com .fasterxml .jackson .databind .ObjectMapper ;
19
+ import com .fasterxml .jackson .databind .SerializationFeature ;
19
20
import com .tencent .trpc .core .exception .ErrorCode ;
20
21
import com .tencent .trpc .core .exception .TRpcException ;
21
22
import com .tencent .trpc .core .logger .Logger ;
@@ -39,6 +40,8 @@ public class JsonUtils {
39
40
objectMapper .setSerializationInclusion (Include .NON_NULL );
40
41
// Do not throw an error when deserializing if there are no corresponding properties
41
42
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 );
42
45
}
43
46
44
47
/**
Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ public class JsonUtilsTest {
32
32
private static final String JSON = "{\" test\" :123}" ;
33
33
private static final String JSON_LIST = "[{\" test\" :123},{\" test\" :123}]" ;
34
34
private static final String ERROR_JSON = "{\" test\" :123,A}" ;
35
+ private static final String EMPTY_JSON = "{}" ;
35
36
36
37
@ Test
37
38
public void testCopy () {
@@ -184,6 +185,12 @@ public void testConvertValue() {
184
185
}
185
186
186
187
188
+ @ Test
189
+ public void testEmptyBeanSerial () {
190
+ TestEmptyBean testEmptyBean = new TestEmptyBean (10 );
191
+ Assert .assertEquals (EMPTY_JSON , JsonUtils .toJson (testEmptyBean ));
192
+ }
193
+
187
194
public static class TestObj {
188
195
189
196
private int testA ;
@@ -232,4 +239,14 @@ public void setData(String data) {
232
239
this .data = data ;
233
240
}
234
241
}
242
+
243
+ public static class TestEmptyBean {
244
+
245
+ // without public getter method
246
+ private int field ;
247
+
248
+ public TestEmptyBean (int field ) {
249
+ this .field = field ;
250
+ }
251
+ }
235
252
}
You can’t perform that action at this time.
0 commit comments