Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1971e45

Browse files
committed
Replace redundant ObjectMapper objects with JsonUtil
Change-Id: I152e528b13fcacfd543141b372999e74586c02ce
1 parent 892325d commit 1971e45

File tree

9 files changed

+52
-162
lines changed

9 files changed

+52
-162
lines changed

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5454
<compiler.source>1.8</compiler.source>
5555
<compiler.target>1.8</compiler.target>
56-
<hugegraph.common.version>1.5.6</hugegraph.common.version>
56+
<hugegraph.common.version>1.5.8</hugegraph.common.version>
5757
<jersey.version>2.25.1</jersey.version>
5858
<mockito.version>2.8.47</mockito.version>
5959
</properties>
@@ -98,6 +98,7 @@
9898
<plugin>
9999
<groupId>org.apache.maven.plugins</groupId>
100100
<artifactId>maven-jar-plugin</artifactId>
101+
<version>2.4</version>
101102
<configuration>
102103
<archive>
103104
<index>true</index>

src/main/java/com/baidu/hugegraph/api/graph/GraphAPI.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,13 @@
2626

2727
import com.baidu.hugegraph.api.API;
2828
import com.baidu.hugegraph.client.RestClient;
29-
import com.baidu.hugegraph.rest.ClientException;
3029
import com.baidu.hugegraph.util.E;
3130
import com.baidu.hugegraph.util.JsonUtil;
32-
import com.fasterxml.jackson.core.JsonProcessingException;
33-
import com.fasterxml.jackson.databind.ObjectMapper;
34-
import com.google.common.collect.ImmutableMap;
3531

3632
public abstract class GraphAPI extends API {
3733

3834
private static final String PATH = "graphs/%s/graph/%s";
3935

40-
private static final ObjectMapper MAPPER = new ObjectMapper();
41-
4236
private final String batchPath;
4337

4438
public GraphAPI(RestClient client, String graph) {
@@ -73,13 +67,7 @@ public static String formatProperties(Map<String, Object> properties) {
7367
if (properties == null) {
7468
return null;
7569
}
76-
String json;
77-
try {
78-
json = MAPPER.writeValueAsString(properties);
79-
} catch (JsonProcessingException e) {
80-
throw new ClientException("Failed to serialize properties '%s'",
81-
properties);
82-
}
70+
String json = JsonUtil.toJson(properties);
8371
/*
8472
* Don't use UrlEncoder.encode, it encoded the space as `+`,
8573
* which will invalidate the jersey's automatic decoding

src/main/java/com/baidu/hugegraph/serializer/PathDeserializer.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,15 @@
2727
import com.baidu.hugegraph.structure.graph.Edge;
2828
import com.baidu.hugegraph.structure.graph.Path;
2929
import com.baidu.hugegraph.structure.graph.Vertex;
30+
import com.baidu.hugegraph.util.JsonUtil;
3031
import com.fasterxml.jackson.core.JsonParser;
3132
import com.fasterxml.jackson.databind.DeserializationContext;
3233
import com.fasterxml.jackson.databind.JsonDeserializer;
3334
import com.fasterxml.jackson.databind.JsonNode;
34-
import com.fasterxml.jackson.databind.ObjectMapper;
3535
import com.fasterxml.jackson.databind.node.JsonNodeType;
3636

3737
public class PathDeserializer extends JsonDeserializer<Path> {
3838

39-
private final ObjectMapper mapper;
40-
41-
public PathDeserializer() {
42-
this.mapper = new ObjectMapper();
43-
}
44-
4539
@Override
4640
public Path deserialize(JsonParser parser, DeserializationContext ctxt)
4741
throws IOException {
@@ -55,7 +49,7 @@ public Path deserialize(JsonParser parser, DeserializationContext ctxt)
5549
if (labelsNode.getNodeType() != JsonNodeType.ARRAY) {
5650
throw InvalidResponseException.expectField("labels", node);
5751
}
58-
Object labels = this.mapper.convertValue(labelsNode, Object.class);
52+
Object labels = JsonUtil.convertValue(labelsNode, Object.class);
5953
((List<?>) labels).forEach(path::labels);
6054
}
6155

@@ -74,15 +68,15 @@ public Path deserialize(JsonParser parser, DeserializationContext ctxt)
7468
if (typeNode != null) {
7569
object = parseTypedNode(objectNode, typeNode);
7670
} else {
77-
object = this.mapper.convertValue(objectNode, Object.class);
71+
object = JsonUtil.convertValue(objectNode, Object.class);
7872
}
7973
path.objects(object);
8074
}
8175

8276
// Parse node 'crosspoint'
8377
JsonNode crosspointNode = node.get("crosspoint");
8478
if (crosspointNode != null) {
85-
Object object = this.mapper.convertValue(crosspointNode, Object.class);
79+
Object object = JsonUtil.convertValue(crosspointNode, Object.class);
8680
path.crosspoint(object);
8781
}
8882
return path;
@@ -91,9 +85,9 @@ public Path deserialize(JsonParser parser, DeserializationContext ctxt)
9185
private Object parseTypedNode(JsonNode objectNode, JsonNode typeNode) {
9286
String type = typeNode.asText();
9387
if (type.equals("vertex")) {
94-
return this.mapper.convertValue(objectNode, Vertex.class);
88+
return JsonUtil.convertValue(objectNode, Vertex.class);
9589
} else if (type.equals("edge")) {
96-
return this.mapper.convertValue(objectNode, Edge.class);
90+
return JsonUtil.convertValue(objectNode, Edge.class);
9791
} else {
9892
throw InvalidResponseException.expectField("vertex/edge", type);
9993
}

src/main/java/com/baidu/hugegraph/serializer/VertexDeserializer.java

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/main/java/com/baidu/hugegraph/structure/gremlin/ResultSet.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
public class ResultSet {
3838

39-
private static ObjectMapper mapper = new ObjectMapper();
39+
private static final ObjectMapper MAPPER = new ObjectMapper();
4040

4141
@JsonProperty
4242
private List<Object> data;
@@ -46,7 +46,7 @@ public class ResultSet {
4646
static {
4747
SimpleModule module = new SimpleModule();
4848
module.addDeserializer(Path.class, new PathDeserializer());
49-
mapper.registerModule(module);
49+
MAPPER.registerModule(module);
5050
}
5151

5252
public List<Object> data() {
@@ -73,8 +73,8 @@ public Result get(int index) {
7373
}
7474

7575
try {
76-
String rawValue = mapper.writeValueAsString(object);
77-
return new Result(mapper.readValue(rawValue, clazz));
76+
String rawValue = MAPPER.writeValueAsString(object);
77+
return new Result(MAPPER.readValue(rawValue, clazz));
7878
} catch (Exception e) {
7979
throw new SerializeException(
8080
"Failed to deserialize: %s", e, object);

src/main/java/com/baidu/hugegraph/util/JsonUtil.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,42 @@
2323

2424
import com.baidu.hugegraph.rest.SerializeException;
2525
import com.fasterxml.jackson.core.JsonProcessingException;
26+
import com.fasterxml.jackson.databind.JsonNode;
27+
import com.fasterxml.jackson.databind.Module;
2628
import com.fasterxml.jackson.databind.ObjectMapper;
2729

28-
public class JsonUtil {
30+
public final class JsonUtil {
2931

30-
private static ObjectMapper mapper = new ObjectMapper();
32+
private static final ObjectMapper MAPPER = new ObjectMapper();
33+
34+
public static void registerModule(Module module) {
35+
MAPPER.registerModule(module);
36+
}
3137

3238
public static String toJson(Object object) {
3339
try {
34-
return mapper.writeValueAsString(object);
40+
return MAPPER.writeValueAsString(object);
3541
} catch (JsonProcessingException e) {
36-
throw new SerializeException("Failed to serialize objects", e);
42+
throw new SerializeException("Failed to serialize object '%s'",
43+
e, object);
3744
}
3845
}
3946

4047
public static <T> T fromJson(String json, Class<T> clazz) {
4148
try {
42-
return mapper.readValue(json, clazz);
49+
return MAPPER.readValue(json, clazz);
4350
} catch (IOException e) {
44-
throw new SerializeException("Failed to deserialize json", e);
51+
throw new SerializeException("Failed to deserialize json '%s'",
52+
e, json);
53+
}
54+
}
55+
56+
public static <T> T convertValue(JsonNode node, Class<T> clazz) {
57+
try {
58+
return MAPPER.convertValue(node, clazz);
59+
} catch (IllegalArgumentException e) {
60+
throw new SerializeException("Failed to deserialize json node '%s'",
61+
e, node);
4562
}
4663
}
4764
}

src/test/java/com/baidu/hugegraph/testutil/Utils.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import com.baidu.hugegraph.testutil.Assert.ThrowableRunnable;
4141
import com.google.common.collect.ImmutableList;
4242

43-
public class Utils {
43+
public final class Utils {
4444

4545
public static void assertResponseError(int status, ThrowableRunnable run) {
4646
Assert.assertThrows(ServerException.class, run, (e) -> {
@@ -57,10 +57,10 @@ public static void assertGraphEqual(ImmutableList<Vertex> vertices,
5757
for (Object object : objects) {
5858
Assert.assertTrue(object instanceof GraphElement);
5959
if (object instanceof Vertex) {
60-
Utils.contains(vertices, (Vertex) object);
60+
Assert.assertTrue(Utils.contains(vertices, (Vertex) object));
6161
} else {
6262
Assert.assertTrue(object instanceof Edge);
63-
Utils.contains(edges, (Edge) object);
63+
Assert.assertTrue(Utils.contains(edges, (Edge) object));
6464
}
6565
}
6666
}
@@ -285,13 +285,15 @@ public static Optional<String> getLabelValue(final Object... keyValues) {
285285

286286
public static Map<String, Object> asMap(Object... keyValues) {
287287
return Utils.asPairs(keyValues).stream()
288-
.collect(Collectors.toMap(Pair::getLeft, Pair::getRight));
288+
.collect(Collectors.toMap(Pair::getLeft, Pair::getRight));
289289
}
290290

291291
public static List<Pair<String, Object>> asPairs(Object... keyValues) {
292292
final List<Object> list = Arrays.asList(keyValues);
293-
return IntStream.range(1, list.size()).filter(i -> i % 2 != 0)
294-
.mapToObj(i -> Pair.of(list.get(i - 1).toString(), list.get(i)))
295-
.collect(Collectors.toList());
293+
return IntStream.range(1, list.size())
294+
.filter(i -> i % 2 != 0)
295+
.mapToObj(i -> Pair.of(list.get(i - 1).toString(),
296+
list.get(i)))
297+
.collect(Collectors.toList());
296298
}
297299
}
Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,14 @@
11
package com.baidu.hugegraph.unit;
22

3-
import java.io.IOException;
4-
5-
import com.baidu.hugegraph.rest.SerializeException;
6-
import com.fasterxml.jackson.databind.ObjectMapper;
3+
import com.baidu.hugegraph.util.JsonUtil;
74

85
public class BaseUnitTest {
96

10-
private static final ObjectMapper mapper = new ObjectMapper();
11-
127
public static <T> String serialize(T data) {
13-
try {
14-
return mapper.writeValueAsString(data);
15-
} catch (IOException e) {
16-
throw new SerializeException("Failed to serialize '%s'", e, data);
17-
}
8+
return JsonUtil.toJson(data);
189
}
1910

2011
public static <T> T deserialize(String json, Class<T> clazz) {
21-
try {
22-
return mapper.readValue(json, clazz);
23-
} catch (IOException e) {
24-
throw new SerializeException(
25-
"Failed to deserialize instance of '%s' from %s",
26-
e, clazz, json);
27-
}
12+
return JsonUtil.fromJson(json, clazz);
2813
}
29-
3014
}

src/test/java/com/baidu/hugegraph/unit/RestResultTest.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -764,24 +764,9 @@ public void testReadGremlinPathWithVertexAndEdge() {
764764
+ "\"label\": \"person\","
765765
+ "\"type\": \"vertex\","
766766
+ "\"properties\":{"
767-
+ "\"city\":["
768-
+ "{"
769-
+ "\"id\": \"person:marko>city\","
770-
+ "\"value\": \"Beijing\""
771-
+ "}"
772-
+ "],"
773-
+ "\"name\":["
774-
+ "{"
775-
+ "\"id\": \"person:marko>name\","
776-
+ "\"value\": \"marko\""
777-
+ "}"
778-
+ "],"
779-
+ "\"age\":["
780-
+ "{"
781-
+ "\"id\": \"person:marko>age\","
782-
+ "\"value\": 29"
783-
+ "}"
784-
+ "]"
767+
+ "\"city\":\"Beijing\","
768+
+ "\"name\":\"marko\","
769+
+ "\"age\":29"
785770
+ "}"
786771
+ "},"
787772
+ "{"
@@ -808,7 +793,7 @@ public void testReadGremlinPathWithVertexAndEdge() {
808793
Mockito.when(this.mockResponse.getStatus()).thenReturn(200);
809794
Mockito.when(this.mockResponse.getHeaders()).thenReturn(null);
810795
Mockito.when(this.mockResponse.readEntity(String.class))
811-
.thenReturn(json);
796+
.thenReturn(json);
812797
RestResult restResult = new RestResult(this.mockResponse);
813798
Assert.assertEquals(200, restResult.status());
814799
Assert.assertNull(restResult.headers());

0 commit comments

Comments
 (0)