diff --git a/grpc/core/pom.xml b/grpc/core/pom.xml index ede6b6bc188..5a22c85d96b 100644 --- a/grpc/core/pom.xml +++ b/grpc/core/pom.xml @@ -116,6 +116,11 @@ jakarta.annotation jakarta.annotation-api + + jakarta.json.bind + jakarta.json.bind-api + provided + org.junit.jupiter diff --git a/grpc/core/src/main/java/io/helidon/grpc/core/JsonbMarshaller.java b/grpc/core/src/main/java/io/helidon/grpc/core/JsonbMarshaller.java new file mode 100644 index 00000000000..e94e05e951e --- /dev/null +++ b/grpc/core/src/main/java/io/helidon/grpc/core/JsonbMarshaller.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2020 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.helidon.grpc.core; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +import javax.inject.Named; +import javax.json.bind.Jsonb; +import javax.json.bind.JsonbBuilder; + +import io.grpc.MethodDescriptor; + +/** + * An implementation of a gRPC {@link MethodDescriptor.Marshaller} that + * uses JSONB for serialization. + * + * @param the type of value to be marshalled + */ +public class JsonbMarshaller + implements MethodDescriptor.Marshaller { + + private static final Jsonb JSONB = JsonbBuilder.create(); + + private final Class clazz; + + /** + * Construct {@code JsonbMarshaller} instance. + * + * @param clazz the type of object to marshall + */ + JsonbMarshaller(Class clazz) { + this.clazz = clazz; + } + + @Override + public InputStream stream(T obj) { + return new ByteArrayInputStream(JSONB.toJson(obj).getBytes(StandardCharsets.UTF_8)); + } + + @Override + public T parse(InputStream in) { + return JSONB.fromJson(in, clazz); + } + + /** + * A {@link MarshallerSupplier} implementation that supplies + * instances of {@link JsonbMarshaller}. + */ + @Named("jsonb") + public static class Supplier implements MarshallerSupplier { + @Override + public MethodDescriptor.Marshaller get(Class clazz) { + return new JsonbMarshaller<>(clazz); + } + } +} diff --git a/grpc/core/src/main/java/module-info.java b/grpc/core/src/main/java/module-info.java index 35eaa944c26..7228d18c663 100644 --- a/grpc/core/src/main/java/module-info.java +++ b/grpc/core/src/main/java/module-info.java @@ -14,6 +14,10 @@ * limitations under the License. */ +import io.helidon.grpc.core.JavaMarshaller; +import io.helidon.grpc.core.JsonbMarshaller; +import io.helidon.grpc.core.MarshallerSupplier; + /** * gRPC Core Module. */ @@ -37,8 +41,15 @@ requires transitive com.google.protobuf; requires java.annotation; + requires static java.json.bind; requires java.logging; requires java.naming; requires jakarta.inject.api; + + provides MarshallerSupplier with + MarshallerSupplier.DefaultMarshallerSupplier, + MarshallerSupplier.ProtoMarshallerSupplier, + JavaMarshaller.Supplier, + JsonbMarshaller.Supplier; } diff --git a/grpc/core/src/main/resources/META-INF/services/io.helidon.grpc.core.MarshallerSupplier b/grpc/core/src/main/resources/META-INF/services/io.helidon.grpc.core.MarshallerSupplier index ce7d9b18304..23fcb5bda15 100644 --- a/grpc/core/src/main/resources/META-INF/services/io.helidon.grpc.core.MarshallerSupplier +++ b/grpc/core/src/main/resources/META-INF/services/io.helidon.grpc.core.MarshallerSupplier @@ -1,5 +1,5 @@ # -# Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2019, 2020 Oracle and/or its affiliates. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -16,3 +16,4 @@ io.helidon.grpc.core.MarshallerSupplier$DefaultMarshallerSupplier io.helidon.grpc.core.MarshallerSupplier$ProtoMarshallerSupplier io.helidon.grpc.core.JavaMarshaller$Supplier +io.helidon.grpc.core.JsonbMarshaller$Supplier