Skip to content

Commit 6cff118

Browse files
committed
chore: added method to parse json list
1 parent 2baf911 commit 6cff118

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

hugegraph-client/src/main/java/org/apache/hugegraph/util/JsonUtil.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
package org.apache.hugegraph.util;
1919

2020
import java.io.IOException;
21+
import java.util.List;
2122

2223
import org.apache.hugegraph.rest.SerializeException;
2324

2425
import com.fasterxml.jackson.core.JsonProcessingException;
26+
import com.fasterxml.jackson.databind.JavaType;
2527
import com.fasterxml.jackson.databind.JsonNode;
2628
import com.fasterxml.jackson.databind.Module;
2729
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -60,4 +62,17 @@ public static <T> T convertValue(JsonNode node, Class<T> clazz) {
6062
e, node);
6163
}
6264
}
65+
66+
public static <T> List<T> fromJson2List(String json, Class<T> clazz) {
67+
try {
68+
return MAPPER.readValue(json, getCollectionType(List.class, clazz));
69+
} catch (IOException e) {
70+
throw new SerializeException("Failed to deserialize json '%s'", e, json);
71+
}
72+
}
73+
74+
private static JavaType getCollectionType(Class<?> collectionClass,
75+
Class<?>... elementClasses) {
76+
return MAPPER.getTypeFactory().constructParametricType(collectionClass, elementClasses);
77+
}
6378
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.apache.hugegraph.unit;
2+
3+
import java.util.List;
4+
5+
import org.apache.hugegraph.util.JsonUtil;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import com.fasterxml.jackson.core.JsonProcessingException;
10+
import com.fasterxml.jackson.databind.JsonNode;
11+
import com.fasterxml.jackson.databind.ObjectMapper;
12+
13+
public class JsonUtilTest {
14+
15+
private static Cat Tom = new Cat("Tom", 3);
16+
private static Cat Butch = new Cat("Butch", 5);
17+
18+
private static String TOM_JSON = "{\"name\":\"Tom\",\"age\":3}";
19+
private static String BUTCH_JSON = "{\"name\":\"Butch\",\"age\":5}";
20+
21+
private static String JSON_LIST = "[" + TOM_JSON + ", " + BUTCH_JSON + "]";
22+
23+
@Test
24+
public void testToJson() {
25+
Assert.assertEquals(TOM_JSON, JsonUtil.toJson(Tom));
26+
}
27+
28+
@Test
29+
public void testFromJson() {
30+
Assert.assertEquals(Tom, JsonUtil.fromJson(TOM_JSON, Cat.class));
31+
}
32+
33+
@Test
34+
public void testConvertValue() throws JsonProcessingException {
35+
ObjectMapper objectMapper = new ObjectMapper();
36+
JsonNode jsonNode = objectMapper.readTree(TOM_JSON);
37+
Assert.assertEquals(Tom, JsonUtil.convertValue(jsonNode, Cat.class));
38+
}
39+
40+
@Test
41+
public void testFromJson2List() {
42+
List<Cat> cats = JsonUtil.fromJson2List(JSON_LIST, Cat.class);
43+
Assert.assertEquals(2, cats.size());
44+
Assert.assertEquals(Tom, cats.get(0));
45+
Assert.assertEquals(Butch, cats.get(1));
46+
}
47+
48+
static class Cat {
49+
50+
private String name;
51+
private Integer age;
52+
53+
public Cat() {
54+
}
55+
56+
public Cat(String name, Integer age) {
57+
this.name = name;
58+
this.age = age;
59+
}
60+
61+
public String getName() {
62+
return name;
63+
}
64+
65+
public void setName(String name) {
66+
this.name = name;
67+
}
68+
69+
public Integer getAge() {
70+
return age;
71+
}
72+
73+
public void setAge(Integer age) {
74+
this.age = age;
75+
}
76+
77+
@Override
78+
public boolean equals(Object obj) {
79+
return obj instanceof Cat &&
80+
this.name.equals(((Cat) obj).getName()) &&
81+
this.age == ((Cat) obj).getAge();
82+
}
83+
}
84+
}

hugegraph-client/src/test/java/org/apache/hugegraph/unit/UnitTestSuite.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
IndexLabelTest.class,
3131
CommonUtilTest.class,
3232
IdUtilTest.class,
33-
SplicingIdGeneratorTest.class
33+
SplicingIdGeneratorTest.class,
34+
JsonUtilTest.class
3435
})
3536
public class UnitTestSuite {
3637
}

0 commit comments

Comments
 (0)