Skip to content

Commit

Permalink
chore: added method to parse json list
Browse files Browse the repository at this point in the history
  • Loading branch information
diaohancai committed Feb 7, 2024
1 parent 2baf911 commit 6cff118
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
package org.apache.hugegraph.util;

import java.io.IOException;
import java.util.List;

import org.apache.hugegraph.rest.SerializeException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -60,4 +62,17 @@ public static <T> T convertValue(JsonNode node, Class<T> clazz) {
e, node);
}
}

public static <T> List<T> fromJson2List(String json, Class<T> clazz) {
try {
return MAPPER.readValue(json, getCollectionType(List.class, clazz));
} catch (IOException e) {
throw new SerializeException("Failed to deserialize json '%s'", e, json);

Check warning on line 70 in hugegraph-client/src/main/java/org/apache/hugegraph/util/JsonUtil.java

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L69 - L70 were not covered by tests
}
}

private static JavaType getCollectionType(Class<?> collectionClass,
Class<?>... elementClasses) {
return MAPPER.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.apache.hugegraph.unit;

import java.util.List;

import org.apache.hugegraph.util.JsonUtil;
import org.junit.Assert;
import org.junit.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtilTest {

private static Cat Tom = new Cat("Tom", 3);
private static Cat Butch = new Cat("Butch", 5);

private static String TOM_JSON = "{\"name\":\"Tom\",\"age\":3}";
private static String BUTCH_JSON = "{\"name\":\"Butch\",\"age\":5}";

private static String JSON_LIST = "[" + TOM_JSON + ", " + BUTCH_JSON + "]";

@Test
public void testToJson() {
Assert.assertEquals(TOM_JSON, JsonUtil.toJson(Tom));
}

@Test
public void testFromJson() {
Assert.assertEquals(Tom, JsonUtil.fromJson(TOM_JSON, Cat.class));
}

@Test
public void testConvertValue() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(TOM_JSON);
Assert.assertEquals(Tom, JsonUtil.convertValue(jsonNode, Cat.class));
}

@Test
public void testFromJson2List() {
List<Cat> cats = JsonUtil.fromJson2List(JSON_LIST, Cat.class);
Assert.assertEquals(2, cats.size());
Assert.assertEquals(Tom, cats.get(0));
Assert.assertEquals(Butch, cats.get(1));
}

static class Cat {

private String name;
private Integer age;

public Cat() {
}

public Cat(String name, Integer age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

@Override
public boolean equals(Object obj) {
return obj instanceof Cat &&
this.name.equals(((Cat) obj).getName()) &&
this.age == ((Cat) obj).getAge();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
IndexLabelTest.class,
CommonUtilTest.class,
IdUtilTest.class,
SplicingIdGeneratorTest.class
SplicingIdGeneratorTest.class,
JsonUtilTest.class
})
public class UnitTestSuite {
}

0 comments on commit 6cff118

Please sign in to comment.