|
|
|
@ -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<Foo> 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<List<Foo>>() {
|
|
|
|
|
}));
|
|
|
|
|
Assert.assertNull(JSONUtil.parseObject(" ", new TypeReference<List<Foo>>() {
|
|
|
|
|
}));
|
|
|
|
|
Assert.assertEquals(
|
|
|
|
|
EXPECTED_FOO_ARRAY,
|
|
|
|
|
JSONUtil.parseObject(EXPECTED_FOO_JSON_ARRAY, new TypeReference<List<Foo>>() {
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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
|
|
|
|
|