Implementation of DataUnmarshaller for JSON

Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
Fabio José 2019-08-26 22:15:05 -03:00
parent d040bcef32
commit 0606036c31
1 changed files with 20 additions and 0 deletions

View File

@ -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 <T> The 'data' type
* @param <A> The attributes type
* @param type The type of 'data'
* @return A new instance of {@link DataUnmarshaller}
*/
public static <T, A extends Attributes>
DataUnmarshaller<String, T, A> umarshaller(Class<T> type) {
return new DataUnmarshaller<String, T, A>() {
@Override
public T unmarshall(String payload, A attributes) throws Exception {
return Json.decodeValue(payload, type);
}
};
}
private Json() {
// no-op