You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2020/12/19 07:47:39 UTC
[isis] branch master updated: ISIS-2033: convert _Json utility
methods to use 'Result' as the default result
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/master by this push:
new ac5add8 ISIS-2033: convert _Json utility methods to use 'Result' as the default result
ac5add8 is described below
commit ac5add84da8148a0ac28b84cc1b85c99d922533f
Author: Andi Huber <ah...@apache.org>
AuthorDate: Sat Dec 19 08:47:28 2020 +0100
ISIS-2033: convert _Json utility methods to use 'Result' as the default
result
---
.../isis/commons/internal/resources/_Json.java | 136 ++++-----------------
.../internal/resources/JsonYamlReaderTest.java | 3 +-
2 files changed, 26 insertions(+), 113 deletions(-)
diff --git a/commons/src/main/java/org/apache/isis/commons/internal/resources/_Json.java b/commons/src/main/java/org/apache/isis/commons/internal/resources/_Json.java
index 7184f93..1cf0d03 100644
--- a/commons/src/main/java/org/apache/isis/commons/internal/resources/_Json.java
+++ b/commons/src/main/java/org/apache/isis/commons/internal/resources/_Json.java
@@ -48,18 +48,7 @@ public class _Json {
// -- STREAM CONTENT
- /**
- * Deserialize JSON content from given JSON content InputStream into an instance of
- * given {@code clazz} type.
- * @param <T>
- * @param clazz
- * @param content
- * @return
- * @throws JsonParseException
- * @throws JsonMappingException
- * @throws IOException
- */
- public static <T> T readJson(final Class<T> clazz, InputStream content)
+ private static <T> T _readJson(final Class<T> clazz, InputStream content)
throws JsonParseException, JsonMappingException, IOException {
return (T) new ObjectMapper().readValue(content, clazz);
@@ -73,22 +62,11 @@ public class _Json {
* @param content
* @return
*/
- public static <T> Result<T> tryReadJson(final Class<T> clazz, InputStream content) {
- return Result.of(()->readJson(clazz, content));
+ public static <T> Result<T> readJson(final Class<T> clazz, InputStream content) {
+ return Result.of(()->_readJson(clazz, content));
}
- /**
- * Deserialize JSON content from given JSON content InputStream into an instance of List
- * with given {@code elementType}.
- * @param <T>
- * @param elementType
- * @param content
- * @return
- * @throws JsonParseException
- * @throws JsonMappingException
- * @throws IOException
- */
- public static <T> List<T> readJsonList(final Class<T> elementType, InputStream content)
+ private static <T> List<T> _readJsonList(final Class<T> elementType, InputStream content)
throws JsonParseException, JsonMappingException, IOException {
val mapper = new ObjectMapper();
@@ -104,25 +82,14 @@ public class _Json {
* @param content
* @return
*/
- public static <T> Result<List<T>> tryReadJsonList(final Class<T> clazz, InputStream content) {
- return Result.of(()->readJsonList(clazz, content));
+ public static <T> Result<List<T>> readJsonList(final Class<T> clazz, InputStream content) {
+ return Result.of(()->_readJsonList(clazz, content));
}
// -- STRING CONTENT
- /**
- * Deserialize JSON content from given JSON content String into an instance of
- * given {@code clazz} type.
- * @param <T>
- * @param clazz
- * @param content
- * @return
- * @throws JsonParseException
- * @throws JsonMappingException
- * @throws IOException
- */
- public static <T> T readJson(final Class<T> clazz, String content)
+ private static <T> T _readJson(final Class<T> clazz, String content)
throws JsonParseException, JsonMappingException, IOException {
return (T) new ObjectMapper().readValue(content, clazz);
@@ -136,22 +103,11 @@ public class _Json {
* @param content
* @return
*/
- public static <T> Result<T> tryReadJson(final Class<T> clazz, String content) {
- return Result.of(()->readJson(clazz, content));
+ public static <T> Result<T> readJson(final Class<T> clazz, String content) {
+ return Result.of(()->_readJson(clazz, content));
}
- /**
- * Deserialize JSON content from given JSON content String into an instance of List
- * with given {@code elementType}.
- * @param <T>
- * @param elementType
- * @param content
- * @return
- * @throws JsonParseException
- * @throws JsonMappingException
- * @throws IOException
- */
- public static <T> List<T> readJsonList(final Class<T> elementType, String content)
+ private static <T> List<T> _readJsonList(final Class<T> elementType, String content)
throws JsonParseException, JsonMappingException, IOException {
val mapper = new ObjectMapper();
@@ -167,25 +123,14 @@ public class _Json {
* @param content
* @return
*/
- public static <T> Result<List<T>> tryReadJsonList(final Class<T> clazz, String content) {
- return Result.of(()->readJsonList(clazz, content));
+ public static <T> Result<List<T>> readJsonList(final Class<T> clazz, String content) {
+ return Result.of(()->_readJsonList(clazz, content));
}
// -- FILE CONTENT
- /**
- * Deserialize JSON content from given JSON content File into an instance of
- * given {@code clazz} type.
- * @param <T>
- * @param clazz
- * @param content
- * @return
- * @throws JsonParseException
- * @throws JsonMappingException
- * @throws IOException
- */
- public static <T> T readJson(final Class<T> clazz, File content)
+ private static <T> T _readJson(final Class<T> clazz, File content)
throws JsonParseException, JsonMappingException, IOException {
return (T) new ObjectMapper().readValue(content, clazz);
@@ -199,22 +144,11 @@ public class _Json {
* @param content
* @return
*/
- public static <T> Result<T> tryReadJson(final Class<T> clazz, File content) {
- return Result.of(()->readJson(clazz, content));
+ public static <T> Result<T> readJson(final Class<T> clazz, File content) {
+ return Result.of(()->_readJson(clazz, content));
}
- /**
- * Deserialize JSON content from given JSON content File into an instance of List
- * with given {@code elementType}.
- * @param <T>
- * @param elementType
- * @param content
- * @return
- * @throws JsonParseException
- * @throws JsonMappingException
- * @throws IOException
- */
- public static <T> List<T> readJsonList(final Class<T> elementType, File content)
+ private static <T> List<T> _readJsonList(final Class<T> elementType, File content)
throws JsonParseException, JsonMappingException, IOException {
val mapper = new ObjectMapper();
@@ -230,24 +164,13 @@ public class _Json {
* @param content
* @return
*/
- public static <T> Result<List<T>> tryReadJsonList(final Class<T> clazz, File content) {
- return Result.of(()->readJsonList(clazz, content));
+ public static <T> Result<List<T>> readJsonList(final Class<T> clazz, File content) {
+ return Result.of(()->_readJsonList(clazz, content));
}
// -- BYTE CONTENT
- /**
- * Deserialize JSON content from given JSON content byte[] into an instance of
- * given {@code clazz} type.
- * @param <T>
- * @param clazz
- * @param content
- * @return
- * @throws JsonParseException
- * @throws JsonMappingException
- * @throws IOException
- */
- public static <T> T readJson(final Class<T> clazz, byte[] content)
+ private static <T> T _readJson(final Class<T> clazz, byte[] content)
throws JsonParseException, JsonMappingException, IOException {
return (T) new ObjectMapper().readValue(content, clazz);
@@ -261,22 +184,11 @@ public class _Json {
* @param content
* @return
*/
- public static <T> Result<T> tryReadJson(final Class<T> clazz, byte[] content) {
- return Result.of(()->readJson(clazz, content));
+ public static <T> Result<T> readJson(final Class<T> clazz, byte[] content) {
+ return Result.of(()->_readJson(clazz, content));
}
- /**
- * Deserialize JSON content from given JSON content byte[] into an instance of List
- * with given {@code elementType}.
- * @param <T>
- * @param elementType
- * @param content
- * @return
- * @throws JsonParseException
- * @throws JsonMappingException
- * @throws IOException
- */
- public static <T> List<T> readJsonList(final Class<T> elementType, byte[] content)
+ private static <T> List<T> _readJsonList(final Class<T> elementType, byte[] content)
throws JsonParseException, JsonMappingException, IOException {
val mapper = new ObjectMapper();
@@ -292,8 +204,8 @@ public class _Json {
* @param content
* @return
*/
- public static <T> Result<List<T>> tryReadJsonList(final Class<T> clazz, byte[] content) {
- return Result.of(()->readJsonList(clazz, content));
+ public static <T> Result<List<T>> readJsonList(final Class<T> clazz, byte[] content) {
+ return Result.of(()->_readJsonList(clazz, content));
}
// -- WRITING
diff --git a/commons/src/test/java/org/apache/isis/commons/internal/resources/JsonYamlReaderTest.java b/commons/src/test/java/org/apache/isis/commons/internal/resources/JsonYamlReaderTest.java
index 9c626bf..b5ea113 100644
--- a/commons/src/test/java/org/apache/isis/commons/internal/resources/JsonYamlReaderTest.java
+++ b/commons/src/test/java/org/apache/isis/commons/internal/resources/JsonYamlReaderTest.java
@@ -52,7 +52,8 @@ class JsonYamlReaderTest {
@Test
void loadCustomerFromJson() throws JsonParseException, JsonMappingException, IOException {
- val customer = _Json.readJson(Customer.class, this.getClass().getResourceAsStream("customer.json"));
+ val customer = _Json.readJson(Customer.class, this.getClass().getResourceAsStream("customer.json"))
+ .nullableOrElse(null);
assertCustomerIsJohnDoe(customer);
}