Search in sources :

Example 1 with ExperimentalApi

use of io.grpc.ExperimentalApi in project grpc-java by grpc.

the class ProtoUtils method 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.
   */
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1786")
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);
}
Also used : Printer(com.google.protobuf.util.JsonFormat.Printer) Parser(com.google.protobuf.util.JsonFormat.Parser) ExperimentalApi(io.grpc.ExperimentalApi)

Example 2 with ExperimentalApi

use of io.grpc.ExperimentalApi in project grpc-java by grpc.

the class ProtoUtils method 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.
   */
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1786")
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;
        }
    };
}
Also used : Marshaller(io.grpc.MethodDescriptor.Marshaller) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Builder(com.google.protobuf.Message.Builder) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Charset(java.nio.charset.Charset) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) ExperimentalApi(io.grpc.ExperimentalApi)

Aggregations

ExperimentalApi (io.grpc.ExperimentalApi)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Builder (com.google.protobuf.Message.Builder)1 Parser (com.google.protobuf.util.JsonFormat.Parser)1 Printer (com.google.protobuf.util.JsonFormat.Printer)1 Marshaller (io.grpc.MethodDescriptor.Marshaller)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 Charset (java.nio.charset.Charset)1