Skip to content

Commit 7a0906f

Browse files
committed
Added methods to unmarshal from JsonNode (#332).
1 parent 994493d commit 7a0906f

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/main/java/org/gitlab4j/api/utils/JacksonJson.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,48 @@ public ObjectMapper getObjectMapper() {
8888
return (objectMapper);
8989
}
9090

91+
/**
92+
* Reads and parses the String containing JSON data and returns a JsonNode tree representation.
93+
*
94+
* @param postData a String holding the POST data
95+
* @return a JsonNode instance containing the parsed JSON
96+
* @throws JsonParseException when an error occurs parsing the provided JSON
97+
* @throws JsonMappingException if a JSON error occurs
98+
* @throws IOException if an error occurs reading the JSON data
99+
*/
100+
public JsonNode readTree(String postData) throws JsonParseException, JsonMappingException, IOException {
101+
return (objectMapper.readTree(postData));
102+
}
103+
104+
/**
105+
* Reads and parses the JSON data on the specified Reader instance to a JsonNode tree representation.
106+
*
107+
* @param reader the Reader instance that contains the JSON data
108+
* @return a JsonNode instance containing the parsed JSON
109+
* @throws JsonParseException when an error occurs parsing the provided JSON
110+
* @throws JsonMappingException if a JSON error occurs
111+
* @throws IOException if an error occurs reading the JSON data
112+
*/
113+
public JsonNode readTree(Reader reader) throws JsonParseException, JsonMappingException, IOException {
114+
return (objectMapper.readTree(reader));
115+
}
116+
117+
/**
118+
* Unmarshal the JsonNode (tree) to an instance of the provided class.
119+
*
120+
* @param <T> the generics type for the return value
121+
* @param returnType an instance of this type class will be returned
122+
* @param tree the JsonNode instance that contains the JSON data
123+
* @return an instance of the provided class containing the data from the tree
124+
* @throws JsonParseException when an error occurs parsing the provided JSON
125+
* @throws JsonMappingException if a JSON error occurs
126+
* @throws IOException if an error occurs reading the JSON data
127+
*/
128+
public <T> T unmarshal(Class<T> returnType, JsonNode tree) throws JsonParseException, JsonMappingException, IOException {
129+
ObjectMapper objectMapper = getContext(returnType);
130+
return (objectMapper.treeToValue(tree, returnType));
131+
}
132+
91133
/**
92134
* Unmarshal the JSON data on the specified Reader instance to an instance of the provided class.
93135
*

0 commit comments

Comments
 (0)