Java Code Examples for com.google.protobuf.Any#getTypeUrl()

The following examples show how to use com.google.protobuf.Any#getTypeUrl() . 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: WellKnownTypeMarshaller.java    From curiostack with MIT License 6 votes vote down vote up
@Override
public void writeValue(Any message, JsonGenerator gen) throws IOException {
  if (message.equals(Any.getDefaultInstance())) {
    // Note: empty Any is not indented the same way as an empty message, this is likely an
    // upstream bug.
    gen.writeRaw(": {}");
    return;
  }
  gen.writeStartObject();
  String typeUrl = message.getTypeUrl();
  TypeSpecificMarshaller<?> serializer = marshallerRegistry.findByTypeUrl(typeUrl);
  gen.writeFieldName("@type");
  gen.writeString(typeUrl);
  if (serializer instanceof WellKnownTypeMarshaller) {
    gen.writeFieldName("value");
    serializer.writeValue(message.getValue(), gen);
  } else {
    serializer.doWrite(message.getValue(), gen);
  }
  gen.writeEndObject();
}
 
Example 2
Source File: AnyToTypeConverter.java    From android-test with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("TypeParameterUnusedInFormals") // addressed in anyToProto(Any, Class,// Parser)
private <T extends MessageLite> T anyToProto(Any any) {
  RemoteDescriptor remoteDescriptor =
      remoteDescriptorRegistry.argForRemoteTypeUrl(any.getTypeUrl());

  try {
    @SuppressWarnings("unchecked") // safe covariant cast
    Class<T> messageType = (Class<T>) remoteDescriptor.getProtoType();
    return anyToProto(any, messageType, remoteDescriptor.getProtoParser());
  } catch (ClassCastException cce) {
    throw new RemoteProtocolException("Message cannot be casted to type T", cce);
  } catch (InvalidProtocolBufferException ipbe) {
    throw new RemoteProtocolException(
        "Invalid Protocol buffer for any type url: " + any.getTypeUrl(), ipbe);
  }
}
 
Example 3
Source File: MessagePayloadSerializerMultiLanguage.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
@Override
public Payload serialize(@Nonnull Object what) {
  final Any any = requireAny(what);
  final String className = any.getTypeUrl();
  final ByteString payloadBytes = any.getValue();
  return Payload.newBuilder().setClassName(className).setPayloadBytes(payloadBytes).build();
}
 
Example 4
Source File: MessagePayloadSerializerMultiLanguage.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
@Override
public Payload serialize(@Nonnull Object what) {
  final Any any = requireAny(what);
  final String className = any.getTypeUrl();
  final ByteString payloadBytes = any.getValue();
  return Payload.newBuilder().setClassName(className).setPayloadBytes(payloadBytes).build();
}
 
Example 5
Source File: SdsClient.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private void processSecretsFromDiscoveryResponse(DiscoveryResponse response)
    throws InvalidProtocolBufferException {
  List<Any> resources = response.getResourcesList();
  checkState(resources.size() == 1, "exactly one resource expected");
  Any any = resources.get(0);
  final String typeUrl = any.getTypeUrl();
  checkState(SECRET_TYPE_URL.equals(typeUrl), "wrong value for typeUrl %s", typeUrl);
  Secret secret = Secret.parseFrom(any.getValue());
  processSecret(secret);
}