The JSON Data Marshaller implementation

Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
Fabio José 2019-08-26 22:23:36 -03:00
parent dccb1cd57c
commit 3a48e913d0
2 changed files with 18 additions and 3 deletions

View File

@ -146,9 +146,7 @@ public class BinaryMarshaller<P, T, A extends Attributes> {
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
})
.headers(BinaryFormatHeaderMapperImpl.mapper()::map)
.payload((data, headers) -> {
return Json.encode(data);
})
.payload(Json.marshaller()::marshal)
.build((payload, headers) -> {
return new Format<>(payload, headers);
});

View File

@ -21,10 +21,12 @@ import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import io.cloudevents.Attributes;
import io.cloudevents.fun.DataMarshaller;
import io.cloudevents.fun.DataUnmarshaller;
import java.io.InputStream;
import java.time.ZonedDateTime;
import java.util.Map;
public final class Json {
@ -116,6 +118,21 @@ public final class Json {
}
};
}
/**
* Creates a JSON Data Marshaller
* @param <T> The 'data' type
* @return A new instance of {@link DataMarshaller}
*/
public static <T> DataMarshaller<String, T> marshaller() {
return new DataMarshaller<String, T>() {
@Override
public String marshal(T data, Map<String, Object> headers)
throws Exception {
return Json.encode(data);
}
};
}
private Json() {
// no-op