Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSONB support to gRPC #1836

Merged
merged 4 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions grpc/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
</dependency>
<dependency>
tomas-langer marked this conversation as resolved.
Show resolved Hide resolved
<groupId>jakarta.json.bind</groupId>
<artifactId>jakarta.json.bind-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
72 changes: 72 additions & 0 deletions grpc/core/src/main/java/io/helidon/grpc/core/JsonbMarshaller.java
Original file line number Diff line number Diff line change
@@ -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 <T> the type of value to be marshalled
*/
public class JsonbMarshaller<T>
implements MethodDescriptor.Marshaller<T> {

private static final Jsonb JSONB = JsonbBuilder.create();

private final Class<T> clazz;

/**
* Construct {@code JsonbMarshaller} instance.
*
* @param clazz the type of object to marshall
*/
JsonbMarshaller(Class<T> 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 {
tomas-langer marked this conversation as resolved.
Show resolved Hide resolved
@Override
public <T> MethodDescriptor.Marshaller<T> get(Class<T> clazz) {
return new JsonbMarshaller<>(clazz);
}
}
}
11 changes: 11 additions & 0 deletions grpc/core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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