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

chore(client): json util supply method to parse json list #577

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -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 @@
e, node);
}
}

public static <T> List<T> fromJson2List(String json, Class<T> clazz) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to line 56?

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);
Comment on lines +66 to +76
Copy link
Member

@imbajin imbajin Feb 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we consider moving the JsonUtil to hugegraph-common module?

}
}
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 {
}
Loading