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

bugfix: jackson serializing can compatible with empty beans #70

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.tencent.trpc.core.exception.ErrorCode;
import com.tencent.trpc.core.exception.TRpcException;
import com.tencent.trpc.core.logger.Logger;
Expand All @@ -39,6 +40,8 @@ public class JsonUtils {
objectMapper.setSerializationInclusion(Include.NON_NULL);
// Do not throw an error when deserializing if there are no corresponding properties
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
// Do not throw an error when serializing if there are no public fields
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ public void testJson2Pojo() {
*/
@Test
public void testJson2PojoEx() {
try {
JsonUtilsTest.TestObj1 obj1 = new JsonUtilsTest.TestObj1();
obj1.setTest(123);
serial.serialize(obj1);
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("json serialize ex:"));
}
try {
byte[] bytes = serial.serialize(obj);
GenericsObj deserialize = serial.deserialize(bytes, GenericsObj.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class JsonUtilsTest {
private static final String JSON = "{\"test\":123}";
private static final String JSON_LIST = "[{\"test\":123},{\"test\":123}]";
private static final String ERROR_JSON = "{\"test\":123,A}";
private static final String EMPTY_JSON = "{}";

@Test
public void testCopy() {
Expand Down Expand Up @@ -134,7 +135,7 @@ public void testToJsonWithDefaultValue() {
TestObj1 obj1 = new TestObj1();
obj1.setTest(123);
String aaa = JsonUtils.toJson(obj1, "aaa");
Assert.assertEquals("aaa", aaa);
Assert.assertEquals(EMPTY_JSON, aaa);
}

@Test
Expand Down
Loading