From 0606036c31a246a67132e95a566cd21b126b1cad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Mon, 26 Aug 2019 22:15:05 -0300 Subject: [PATCH] Implementation of DataUnmarshaller for JSON MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- .../main/java/io/cloudevents/json/Json.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/api/src/main/java/io/cloudevents/json/Json.java b/api/src/main/java/io/cloudevents/json/Json.java index d47b983b..1d9e170b 100644 --- a/api/src/main/java/io/cloudevents/json/Json.java +++ b/api/src/main/java/io/cloudevents/json/Json.java @@ -20,6 +20,9 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import io.cloudevents.Attributes; +import io.cloudevents.fun.DataUnmarshaller; + import java.io.InputStream; import java.time.ZonedDateTime; @@ -96,6 +99,23 @@ public final class Json { throw new IllegalStateException("Failed to decode: " + e.getMessage(), e); } } + + /** + * Creates a JSON Data Unmarshaller + * @param The 'data' type + * @param The attributes type + * @param type The type of 'data' + * @return A new instance of {@link DataUnmarshaller} + */ + public static + DataUnmarshaller umarshaller(Class type) { + return new DataUnmarshaller() { + @Override + public T unmarshall(String payload, A attributes) throws Exception { + return Json.decodeValue(payload, type); + } + }; + } private Json() { // no-op