io.grpc.MethodDescriptor.PrototypeMarshaller Java Examples

The following examples show how to use io.grpc.MethodDescriptor.PrototypeMarshaller. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GrpcMessageMarshaller.java    From armeria with Apache License 2.0 6 votes vote down vote up
public ByteBuf serializeRequest(I message) throws IOException {
    switch (requestType) {
        case PROTOBUF:
            final PrototypeMarshaller<I> marshaller = (PrototypeMarshaller<I>) requestMarshaller;
            return serializeProto(marshaller, (Message) message);
        default:
            final CompositeByteBuf out = alloc.compositeBuffer();
            try (ByteBufOutputStream os = new ByteBufOutputStream(out)) {
                if (isProto) {
                    ByteStreams.copy(method.streamRequest(message), os);
                } else {
                    jsonMarshaller.serializeMessage(requestMarshaller, message, os);
                }
            }
            return out;
    }
}
 
Example #2
Source File: GrpcMessageMarshaller.java    From armeria with Apache License 2.0 6 votes vote down vote up
public ByteBuf serializeResponse(O message) throws IOException {
    switch (responseType) {
        case PROTOBUF:
            final PrototypeMarshaller<O> marshaller =
                    (PrototypeMarshaller<O>) method.getResponseMarshaller();
            return serializeProto(marshaller, (Message) message);
        default:
            final CompositeByteBuf out = alloc.compositeBuffer();
            try (ByteBufOutputStream os = new ByteBufOutputStream(out)) {
                if (isProto) {
                    ByteStreams.copy(method.streamResponse(message), os);
                } else {
                    jsonMarshaller.serializeMessage(responseMarshaller, message, os);
                }
            }
            return out;
    }
}
 
Example #3
Source File: ProtoLiteUtilsTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void introspection() throws Exception {
  Marshaller<Enum> enumMarshaller = ProtoLiteUtils.marshaller(Enum.getDefaultInstance());
  PrototypeMarshaller<Enum> prototypeMarshaller = (PrototypeMarshaller<Enum>) enumMarshaller;
  assertSame(Enum.getDefaultInstance(), prototypeMarshaller.getMessagePrototype());
  assertSame(Enum.class, prototypeMarshaller.getMessageClass());
}
 
Example #4
Source File: DefaultJsonMarshaller.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T deserializeMessage(Marshaller<T> marshaller, InputStream is) throws IOException {
    final PrototypeMarshaller<T> prototypeMarshaller = (PrototypeMarshaller<T>) marshaller;
    final Message prototype = (Message) prototypeMarshaller.getMessagePrototype();
    final Message.Builder builder = prototype.newBuilderForType();
    delegate.mergeValue(is, builder);
    @SuppressWarnings("unchecked")
    final T cast = (T) builder.build();
    return cast;
}
 
Example #5
Source File: GrpcJsonUtil.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Nullable
private static Message marshallerPrototype(Marshaller<?> marshaller) {
    if (marshaller instanceof PrototypeMarshaller) {
        final Object prototype = ((PrototypeMarshaller<?>) marshaller).getMessagePrototype();
        if (prototype instanceof Message) {
            return (Message) prototype;
        }
    }
    return null;
}
 
Example #6
Source File: ProtoLiteUtilsTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void introspection() throws Exception {
  Marshaller<Enum> enumMarshaller = ProtoLiteUtils.marshaller(Enum.getDefaultInstance());
  PrototypeMarshaller<Enum> prototypeMarshaller = (PrototypeMarshaller<Enum>) enumMarshaller;
  assertSame(Enum.getDefaultInstance(), prototypeMarshaller.getMessagePrototype());
  assertSame(Enum.class, prototypeMarshaller.getMessageClass());
}
 
Example #7
Source File: GrpcMessageMarshaller.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static MessageType marshallerType(Marshaller<?> marshaller) {
    return marshaller instanceof PrototypeMarshaller ? MessageType.PROTOBUF : MessageType.UNKNOWN;
}