|
17 | 17 |
|
18 | 18 | package org.apache.hugegraph.util; |
19 | 19 |
|
| 20 | +import java.io.IOException; |
| 21 | + |
| 22 | +import org.apache.hugegraph.rest.SerializeException; |
| 23 | + |
20 | 24 | import com.fasterxml.jackson.core.JsonProcessingException; |
21 | 25 | import com.fasterxml.jackson.databind.JsonNode; |
22 | 26 | import com.fasterxml.jackson.databind.Module; |
23 | 27 | import com.fasterxml.jackson.databind.ObjectMapper; |
24 | | -import org.apache.hugegraph.rest.SerializeException; |
25 | | - |
26 | | -import java.io.IOException; |
27 | 28 |
|
28 | | -public final class JsonUtil { |
| 29 | +/** |
| 30 | + * Utility class for JSON operations. |
| 31 | + */ |
| 32 | +public final class JsonUtilCommon { |
29 | 33 |
|
| 34 | + /** |
| 35 | + * ObjectMapper instance used for JSON operations. |
| 36 | + */ |
30 | 37 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
31 | 38 |
|
| 39 | + /** |
| 40 | + * Registers a module with the ObjectMapper. |
| 41 | + * |
| 42 | + * @param module the module to register |
| 43 | + */ |
32 | 44 | public static void registerModule(Module module) { |
33 | 45 | MAPPER.registerModule(module); |
34 | 46 | } |
35 | 47 |
|
| 48 | + /** |
| 49 | + * Converts an object to a JSON string. |
| 50 | + * |
| 51 | + * @param object the object to convert |
| 52 | + * @return the JSON string representation of the object |
| 53 | + * @throws SerializeException if the object cannot be serialized |
| 54 | + */ |
36 | 55 | public static String toJson(Object object) { |
37 | 56 | try { |
38 | 57 | return MAPPER.writeValueAsString(object); |
39 | 58 | } catch (JsonProcessingException e) { |
40 | | - throw new SerializeException("Failed to serialize object '%s'", |
41 | | - e, object); |
| 59 | + throw new SerializeException("Failed to serialize object '%s'", e, object); |
42 | 60 | } |
43 | 61 | } |
44 | 62 |
|
| 63 | + /** |
| 64 | + * Converts a JSON string to an object of the specified class. |
| 65 | + * |
| 66 | + * @param json the JSON string |
| 67 | + * @param clazz the class of the object |
| 68 | + * @return the object represented by the JSON string |
| 69 | + * @throws SerializeException if the JSON string cannot be deserialized |
| 70 | + */ |
45 | 71 | public static <T> T fromJson(String json, Class<T> clazz) { |
46 | 72 | try { |
47 | 73 | return MAPPER.readValue(json, clazz); |
48 | 74 | } catch (IOException e) { |
49 | | - throw new SerializeException("Failed to deserialize json '%s'", |
50 | | - e, json); |
| 75 | + throw new SerializeException("Failed to deserialize json '%s'", e, json); |
51 | 76 | } |
52 | 77 | } |
53 | 78 |
|
| 79 | + /** |
| 80 | + * Converts a JsonNode to an object of the specified class. |
| 81 | + * |
| 82 | + * @param node the JsonNode |
| 83 | + * @param clazz the class of the object |
| 84 | + * @return the object represented by the JsonNode |
| 85 | + * @throws SerializeException if the JsonNode cannot be deserialized |
| 86 | + */ |
54 | 87 | public static <T> T convertValue(JsonNode node, Class<T> clazz) { |
55 | 88 | try { |
56 | 89 | return MAPPER.convertValue(node, clazz); |
57 | 90 | } catch (IllegalArgumentException e) { |
58 | | - throw new SerializeException("Failed to deserialize json node '%s'", |
59 | | - e, node); |
| 91 | + throw new SerializeException("Failed to deserialize json node '%s'", e, node); |
60 | 92 | } |
61 | 93 | } |
62 | 94 | } |
0 commit comments