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 2022/12/13 10:38:23 UTC
[isis] branch master updated: ISIS-3304: JsonUtils polishing
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 fe5aadbe69 ISIS-3304: JsonUtils polishing
fe5aadbe69 is described below
commit fe5aadbe698fa627231f11747f33ae917d32530f
Author: Andi Huber <ah...@apache.org>
AuthorDate: Tue Dec 13 11:38:15 2022 +0100
ISIS-3304: JsonUtils polishing
---
.../applib/util/schema/CommonDtoUtils.java | 9 ++++---
.../apache/causeway/commons/functional/Try.java | 5 ++++
.../org/apache/causeway/commons/io/DataSource.java | 13 ++++++----
.../org/apache/causeway/commons/io/JsonUtils.java | 28 +++++++++++++++++-----
.../valuetypes/vega/applib/value/Vega.java | 3 +--
.../restfulobjects/applib/JsonRepresentation.java | 3 +--
6 files changed, 42 insertions(+), 19 deletions(-)
diff --git a/api/applib/src/main/java/org/apache/causeway/applib/util/schema/CommonDtoUtils.java b/api/applib/src/main/java/org/apache/causeway/applib/util/schema/CommonDtoUtils.java
index e18e481160..7de01bbdde 100644
--- a/api/applib/src/main/java/org/apache/causeway/applib/util/schema/CommonDtoUtils.java
+++ b/api/applib/src/main/java/org/apache/causeway/applib/util/schema/CommonDtoUtils.java
@@ -44,7 +44,6 @@ import org.apache.causeway.commons.internal.base._NullSafe;
import org.apache.causeway.commons.internal.base._Strings;
import org.apache.causeway.commons.internal.context._Context;
import org.apache.causeway.commons.internal.exceptions._Exceptions;
-import org.apache.causeway.commons.io.DataSource;
import org.apache.causeway.commons.io.JsonUtils;
import org.apache.causeway.schema.cmd.v2.MapDto;
import org.apache.causeway.schema.cmd.v2.ParamDto;
@@ -162,21 +161,21 @@ public final class CommonDtoUtils {
return valueDto;
}
case ENUM: {
- final EnumDto enumDto = JsonUtils.tryRead(EnumDto.class, DataSource.ofStringUtf8(json))
+ final EnumDto enumDto = JsonUtils.tryRead(EnumDto.class, json)
.ifFailureFail()
.getValue().orElseThrow();
valueDto.setEnum(enumDto);
return valueDto;
}
case BLOB: {
- final BlobDto blobDto = JsonUtils.tryRead(BlobDto.class, DataSource.ofStringUtf8(json))
+ final BlobDto blobDto = JsonUtils.tryRead(BlobDto.class, json)
.ifFailureFail()
.getValue().orElseThrow();
valueDto.setBlob(blobDto);
return valueDto;
}
case CLOB: {
- final ClobDto clobDto = JsonUtils.tryRead(ClobDto.class, DataSource.ofStringUtf8(json))
+ final ClobDto clobDto = JsonUtils.tryRead(ClobDto.class, json)
.ifFailureFail()
.getValue().orElseThrow();
valueDto.setClob(clobDto);
@@ -273,7 +272,7 @@ public final class CommonDtoUtils {
@Nullable
public TypedTupleDto getCompositeValueFromJson(final @Nullable String json) {
return _Strings.isNotEmpty(json)
- ? JsonUtils.tryRead(TypedTupleDto.class, DataSource.ofStringUtf8(json), JsonUtils::jaxbAnnotationSupport)
+ ? JsonUtils.tryRead(TypedTupleDto.class, json, JsonUtils::jaxbAnnotationSupport)
.ifFailureFail()
.getValue().orElseThrow()
: null;
diff --git a/commons/src/main/java/org/apache/causeway/commons/functional/Try.java b/commons/src/main/java/org/apache/causeway/commons/functional/Try.java
index 0d9f9b0c43..ec6774c23e 100644
--- a/commons/src/main/java/org/apache/causeway/commons/functional/Try.java
+++ b/commons/src/main/java/org/apache/causeway/commons/functional/Try.java
@@ -50,6 +50,11 @@ public interface Try<T> {
// -- FACTORIES
+ /** success case with no value */
+ public static <T> Try<T> empty() {
+ return success(null);
+ }
+
public static <T> Try<T> call(final @NonNull Callable<T> callable) {
try {
return success(callable.call());
diff --git a/commons/src/main/java/org/apache/causeway/commons/io/DataSource.java b/commons/src/main/java/org/apache/causeway/commons/io/DataSource.java
index 153fa10290..56286387ed 100644
--- a/commons/src/main/java/org/apache/causeway/commons/io/DataSource.java
+++ b/commons/src/main/java/org/apache/causeway/commons/io/DataSource.java
@@ -27,7 +27,10 @@ import java.nio.charset.StandardCharsets;
import java.util.function.Function;
import java.util.function.Supplier;
+import org.springframework.lang.Nullable;
+
import org.apache.causeway.commons.functional.Try;
+import org.apache.causeway.commons.internal.base._Strings;
import lombok.NonNull;
@@ -49,7 +52,7 @@ public interface DataSource {
static DataSource none() {
return new DataSource() {
@Override public <T> Try<T> readAll(final @NonNull Function<InputStream, Try<T>> consumingMapper) {
- return Try.success(null);
+ return Try.empty();
}
};
}
@@ -76,11 +79,13 @@ public interface DataSource {
return fromInputStreamSupplier(()->Try.call(()->new FileInputStream(file)).ifFailureFail().getValue().orElseThrow());
}
- static DataSource ofString(final @NonNull String string, final Charset charset) {
- return fromInputStreamSupplier(()->new ByteArrayInputStream(string.getBytes(charset)));
+ static DataSource ofString(final @Nullable String string, final Charset charset) {
+ return _Strings.isNullOrEmpty(string)
+ ? none()
+ : fromInputStreamSupplier(()->new ByteArrayInputStream(string.getBytes(charset)));
}
- static DataSource ofStringUtf8(final @NonNull String string) {
+ static DataSource ofStringUtf8(final @Nullable String string) {
return ofString(string, StandardCharsets.UTF_8);
}
diff --git a/commons/src/main/java/org/apache/causeway/commons/io/JsonUtils.java b/commons/src/main/java/org/apache/causeway/commons/io/JsonUtils.java
index d6828ef972..a48340985d 100644
--- a/commons/src/main/java/org/apache/causeway/commons/io/JsonUtils.java
+++ b/commons/src/main/java/org/apache/causeway/commons/io/JsonUtils.java
@@ -49,16 +49,27 @@ public class JsonUtils {
// -- READING
+ /**
+ * Tries to deserialize JSON content from given UTF8 encoded {@link String}
+ * into an instance of given {@code mappedType}.
+ */
+ public <T> Try<T> tryRead(
+ final @NonNull Class<T> mappedType,
+ final @Nullable String stringUtf8,
+ final JsonUtils.JsonCustomizer ... customizers) {
+ return tryRead(mappedType, DataSource.ofStringUtf8(stringUtf8), customizers);
+ }
+
/**
* Tries to deserialize JSON content from given {@link DataSource} into an instance of
- * given {@code requiredType}.
+ * given {@code mappedType}.
*/
public <T> Try<T> tryRead(
- final @NonNull Class<T> requiredType,
+ final @NonNull Class<T> mappedType,
final @NonNull DataSource source,
final JsonUtils.JsonCustomizer ... customizers) {
return source.readAll((final InputStream is)->{
- return Try.call(()->createMapper(customizers).readValue(is, requiredType));
+ return Try.call(()->createMapper(customizers).readValue(is, mappedType));
});
}
@@ -81,15 +92,22 @@ public class JsonUtils {
// -- WRITING
+ /**
+ * Writes given {@code pojo} to given {@link DataSink}.
+ */
public void write(
final @Nullable Object pojo,
final @NonNull DataSink sink,
final JsonUtils.JsonCustomizer ... customizers) {
if(pojo==null) return;
sink.writeAll(os->
- Try.run(()->createMapper(customizers).writeValue(os, sink)));
+ Try.run(()->createMapper(customizers).writeValue(os, pojo)));
}
+ /**
+ * Converts given {@code pojo} to an UTF8 encoded {@link String}.
+ * @return <code>null</code> if pojo is <code>null</code>
+ */
@SneakyThrows
@Nullable
public static String toStringUtf8(
@@ -128,6 +146,4 @@ public class JsonUtils {
return mapper;
}
-
-
}
diff --git a/valuetypes/vega/applib/src/main/java/org/apache/causeway/valuetypes/vega/applib/value/Vega.java b/valuetypes/vega/applib/src/main/java/org/apache/causeway/valuetypes/vega/applib/value/Vega.java
index 32b9ca82db..5c00204b2f 100644
--- a/valuetypes/vega/applib/src/main/java/org/apache/causeway/valuetypes/vega/applib/value/Vega.java
+++ b/valuetypes/vega/applib/src/main/java/org/apache/causeway/valuetypes/vega/applib/value/Vega.java
@@ -28,7 +28,6 @@ import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.springframework.lang.Nullable;
import org.apache.causeway.commons.internal.base._Strings;
-import org.apache.causeway.commons.io.DataSource;
import org.apache.causeway.commons.io.JsonUtils;
import org.apache.causeway.valuetypes.vega.applib.CausewayModuleValVegaApplib;
import org.apache.causeway.valuetypes.vega.applib.jaxb.VegaJaxbAdapter;
@@ -74,7 +73,7 @@ public final class Vega implements Serializable {
* parses the json input for schema specification
*/
@NonNull static Schema valueOfJson(final @Nullable String json) {
- return JsonUtils.tryRead(Map.class, DataSource.ofStringUtf8(json))
+ return JsonUtils.tryRead(Map.class, json)
.getValue()
.map(map->map.get(key()))
.map(schemaValue->{
diff --git a/viewers/restfulobjects/applib/src/main/java/org/apache/causeway/viewer/restfulobjects/applib/JsonRepresentation.java b/viewers/restfulobjects/applib/src/main/java/org/apache/causeway/viewer/restfulobjects/applib/JsonRepresentation.java
index 4154afe844..5138553f40 100644
--- a/viewers/restfulobjects/applib/src/main/java/org/apache/causeway/viewer/restfulobjects/applib/JsonRepresentation.java
+++ b/viewers/restfulobjects/applib/src/main/java/org/apache/causeway/viewer/restfulobjects/applib/JsonRepresentation.java
@@ -52,7 +52,6 @@ import org.apache.causeway.commons.internal.base._Casts;
import org.apache.causeway.commons.internal.base._NullSafe;
import org.apache.causeway.commons.internal.base._Strings;
import org.apache.causeway.commons.internal.collections._Maps;
-import org.apache.causeway.commons.io.DataSource;
import org.apache.causeway.commons.io.JsonUtils;
import org.apache.causeway.viewer.restfulobjects.applib.util.JsonNodeUtils;
import org.apache.causeway.viewer.restfulobjects.applib.util.PathNode;
@@ -118,7 +117,7 @@ public class JsonRepresentation {
val repr = JsonRepresentation.newMap();
if(_Strings.isNotEmpty(keyValuePairsAsJson)) {
final Map<Object, Object> keyValuePairs = _Casts.uncheckedCast(
- JsonUtils.tryRead(Map.class, DataSource.ofStringUtf8(keyValuePairsAsJson))
+ JsonUtils.tryRead(Map.class, keyValuePairsAsJson)
.ifFailureFail()
.getValue().orElseThrow());