diff options
author | Carl Mastrangelo <notcarl@google.com> | 2018-05-17 15:48:45 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-17 15:48:45 -0700 |
commit | e3f8891f579b779796bcbad0c43e8cd42650d3c0 (patch) | |
tree | f3acb394782cbc2c7a7b9daa9e2e6f0bb39140a0 /examples | |
parent | e085a0eca0c414bc992e2ec0eaa46a350580abb0 (diff) | |
download | grpc-grpc-java-e3f8891f579b779796bcbad0c43e8cd42650d3c0.tar.gz |
protobuf,examples: move json encoding to examples
Diffstat (limited to 'examples')
-rw-r--r-- | examples/build.gradle | 6 | ||||
-rw-r--r-- | examples/pom.xml | 9 | ||||
-rw-r--r-- | examples/src/main/java/io/grpc/examples/advanced/HelloJsonClient.java | 5 | ||||
-rw-r--r-- | examples/src/main/java/io/grpc/examples/advanced/JsonMarshaller.java | 99 |
4 files changed, 114 insertions, 5 deletions
diff --git a/examples/build.gradle b/examples/build.gradle index c41857e86..f71a8d731 100644 --- a/examples/build.gradle +++ b/examples/build.gradle @@ -30,6 +30,8 @@ repositories { // updating the version in our release process. def grpcVersion = '1.13.0-SNAPSHOT' // CURRENT_GRPC_VERSION def nettyTcNativeVersion = '2.0.7.Final' +def protobufVersion = '3.5.1' +def protocVersion = '3.5.1-1' dependencies { compile "com.google.api.grpc:proto-google-common-protos:1.0.0" @@ -42,6 +44,8 @@ dependencies { // Used for TLS in HelloWorldServerTls compile "io.netty:netty-tcnative-boringssl-static:${nettyTcNativeVersion}" + compile "com.google.protobuf:protobuf-java-util:${protobufVersion}" + testCompile "io.grpc:grpc-testing:${grpcVersion}" testCompile "junit:junit:4.12" testCompile "org.mockito:mockito-core:1.9.5" @@ -49,7 +53,7 @@ dependencies { protobuf { protoc { - artifact = 'com.google.protobuf:protoc:3.5.1-1' + artifact = "com.google.protobuf:protoc:${protocVersion}" } plugins { grpc { diff --git a/examples/pom.xml b/examples/pom.xml index c060f27ff..490685b36 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -12,6 +12,8 @@ <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <grpc.version>1.13.0-SNAPSHOT</grpc.version><!-- CURRENT_GRPC_VERSION --> + <protobuf.version>3.5.1</protobuf.version> + <protoc.version>3.5.1-1</protoc.version> <netty.tcnative.version>2.0.7.Final</netty.tcnative.version> </properties> <dependencies> @@ -52,6 +54,11 @@ <version>1.0.0</version> </dependency> <dependency> + <groupId>com.google.protobuf</groupId> + <artifactId>protobuf-java-util</artifactId> + <version>${protobuf.version}</version> + </dependency> + <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> @@ -78,7 +85,7 @@ <artifactId>protobuf-maven-plugin</artifactId> <version>0.5.1</version> <configuration> - <protocArtifact>com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}</protocArtifact> + <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact> </configuration> diff --git a/examples/src/main/java/io/grpc/examples/advanced/HelloJsonClient.java b/examples/src/main/java/io/grpc/examples/advanced/HelloJsonClient.java index 815734fb6..0e163168a 100644 --- a/examples/src/main/java/io/grpc/examples/advanced/HelloJsonClient.java +++ b/examples/src/main/java/io/grpc/examples/advanced/HelloJsonClient.java @@ -28,7 +28,6 @@ import io.grpc.examples.helloworld.GreeterGrpc; import io.grpc.examples.helloworld.HelloReply; import io.grpc.examples.helloworld.HelloRequest; import io.grpc.examples.helloworld.HelloWorldClient; -import io.grpc.protobuf.ProtoUtils; import io.grpc.stub.AbstractStub; import java.util.concurrent.TimeUnit; import java.util.logging.Level; @@ -98,8 +97,8 @@ public final class HelloJsonClient { static final MethodDescriptor<HelloRequest, HelloReply> METHOD_SAY_HELLO = GreeterGrpc.getSayHelloMethod() .toBuilder( - ProtoUtils.jsonMarshaller(HelloRequest.getDefaultInstance()), - ProtoUtils.jsonMarshaller(HelloReply.getDefaultInstance())) + JsonMarshaller.jsonMarshaller(HelloRequest.getDefaultInstance()), + JsonMarshaller.jsonMarshaller(HelloReply.getDefaultInstance())) .build(); protected HelloJsonStub(Channel channel) { diff --git a/examples/src/main/java/io/grpc/examples/advanced/JsonMarshaller.java b/examples/src/main/java/io/grpc/examples/advanced/JsonMarshaller.java new file mode 100644 index 000000000..7b9aae94f --- /dev/null +++ b/examples/src/main/java/io/grpc/examples/advanced/JsonMarshaller.java @@ -0,0 +1,99 @@ +/* + * Copyright 2018 The gRPC Authors + * + * 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.grpc.examples.advanced; + +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.protobuf.Message; +import com.google.protobuf.Message.Builder; +import com.google.protobuf.util.JsonFormat; +import com.google.protobuf.util.JsonFormat.Parser; +import com.google.protobuf.util.JsonFormat.Printer; +import io.grpc.ExperimentalApi; +import io.grpc.MethodDescriptor.Marshaller; +import io.grpc.Status; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.charset.Charset; + +/** + * A {@link Marshaller} for JSON. This marshals in the Protobuf 3 format described here: + * https://developers.google.com/protocol-buffers/docs/proto3#json + */ +final class JsonMarshaller { + + private JsonMarshaller() {} + + /** + * Create a {@code Marshaller} for json protos of the same type as {@code defaultInstance}. + * + * <p>This is an unstable API and has not been optimized yet for performance. + */ + public static <T extends Message> Marshaller<T> jsonMarshaller(final T defaultInstance) { + final Parser parser = JsonFormat.parser(); + final Printer printer = JsonFormat.printer(); + return jsonMarshaller(defaultInstance, parser, printer); + } + + /** + * Create a {@code Marshaller} for json protos of the same type as {@code defaultInstance}. + * + * <p>This is an unstable API and has not been optimized yet for performance. + */ + public static <T extends Message> Marshaller<T> jsonMarshaller( + final T defaultInstance, final Parser parser, final Printer printer) { + + final Charset charset = Charset.forName("UTF-8"); + + return new Marshaller<T>() { + @Override + public InputStream stream(T value) { + try { + return new ByteArrayInputStream(printer.print(value).getBytes(charset)); + } catch (InvalidProtocolBufferException e) { + throw Status.INTERNAL + .withCause(e) + .withDescription("Unable to print json proto") + .asRuntimeException(); + } + } + + @SuppressWarnings("unchecked") + @Override + public T parse(InputStream stream) { + Builder builder = defaultInstance.newBuilderForType(); + Reader reader = new InputStreamReader(stream, charset); + T proto; + try { + parser.merge(reader, builder); + proto = (T) builder.build(); + reader.close(); + } catch (InvalidProtocolBufferException e) { + throw Status.INTERNAL.withDescription("Invalid protobuf byte sequence") + .withCause(e).asRuntimeException(); + } catch (IOException e) { + // Same for now, might be unavailable + throw Status.INTERNAL.withDescription("Invalid protobuf byte sequence") + .withCause(e).asRuntimeException(); + } + return proto; + } + }; + } +} |