diff --git a/infra/common/src/main/java/cn/hippo4j/common/toolkit/JacksonHandler.java b/infra/common/src/main/java/cn/hippo4j/common/toolkit/JacksonHandler.java index 5148c5a3..d4c43966 100644 --- a/infra/common/src/main/java/cn/hippo4j/common/toolkit/JacksonHandler.java +++ b/infra/common/src/main/java/cn/hippo4j/common/toolkit/JacksonHandler.java @@ -48,6 +48,7 @@ public class JacksonHandler implements JsonFacade { MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL); MAPPER.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + MAPPER.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true); } @Override diff --git a/infra/common/src/test/java/cn/hippo4j/common/toolkit/JSONUtilTest.java b/infra/common/src/test/java/cn/hippo4j/common/toolkit/JSONUtilTest.java index 05faebf1..ce87f2c3 100644 --- a/infra/common/src/test/java/cn/hippo4j/common/toolkit/JSONUtilTest.java +++ b/infra/common/src/test/java/cn/hippo4j/common/toolkit/JSONUtilTest.java @@ -15,8 +15,9 @@ * limitations under the License. */ -package cn.hippo4j.common.toolkit; + package cn.hippo4j.common.toolkit; +<<<<<<< HEAD:infra/common/src/test/java/cn/hippo4j/common/toolkit/JSONUtilTest.java import com.fasterxml.jackson.core.type.TypeReference; import lombok.AllArgsConstructor; import lombok.Data; @@ -92,3 +93,76 @@ public class JSONUtilTest { private Foo foo; } } +======= + import com.fasterxml.jackson.core.type.TypeReference; + import lombok.AllArgsConstructor; + import lombok.Data; + import lombok.EqualsAndHashCode; + import lombok.NoArgsConstructor; + import org.junit.Assert; + import org.junit.Test; + + import java.util.Arrays; + import java.util.Collections; + import java.util.List; + + public class JSONUtilTest { + + private static final Foo EXPECTED_FOO = new Foo(new Foo(null, 2, "foo2"), 1, "foo1"); + + private static final List EXPECTED_FOO_ARRAY = Arrays.asList(EXPECTED_FOO, EXPECTED_FOO); + + private static final String EXPECTED_FOO_JSON = "{\"foo\":{\"id\":2,\"name\":\"foo2\"},\"id\":1,\"name\":\"foo1\"}"; + + private static final String EXPECTED_FOO_JSON_ARRAY = "[" + EXPECTED_FOO_JSON + "," + EXPECTED_FOO_JSON + "]"; + + @Test + public void assertToJSONString() { + Assert.assertNull(JSONUtil.toJSONString(null)); + Assert.assertEquals(EXPECTED_FOO_JSON, JSONUtil.toJSONString(EXPECTED_FOO)); + } + + @Test + public void assertParseObject() { + Assert.assertNull(JSONUtil.parseObject(null, Foo.class)); + Assert.assertNull(JSONUtil.parseObject(" ", Foo.class)); + Assert.assertEquals(EXPECTED_FOO, JSONUtil.parseObject(EXPECTED_FOO_JSON, Foo.class)); + } + + @Test + public void assertParseObjectTypeReference() { + Assert.assertNull(JSONUtil.parseObject(null, new TypeReference>() { + })); + Assert.assertNull(JSONUtil.parseObject(" ", new TypeReference>() { + })); + Assert.assertEquals( + EXPECTED_FOO_ARRAY, + JSONUtil.parseObject(EXPECTED_FOO_JSON_ARRAY, new TypeReference>() { + })); + } + + @Test + public void assertParseArray() { + Assert.assertEquals(Collections.emptyList(), JSONUtil.parseArray(null, Foo.class)); + Assert.assertEquals(Collections.emptyList(), JSONUtil.parseArray(" ", Foo.class)); + Assert.assertEquals( + EXPECTED_FOO_ARRAY, + JSONUtil.parseArray(EXPECTED_FOO_JSON_ARRAY, Foo.class)); + } + + @EqualsAndHashCode + @AllArgsConstructor + @NoArgsConstructor + @Data + private static class Foo { + + private Foo foo; + + private Integer id; + + private String name; + + } + } + +>>>>>>> ed2db3f3 (potentially issue fixed):hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/JSONUtilTest.java