com.google.protobuf.Any类的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(421)

本文整理了Java中com.google.protobuf.Any类的一些代码示例,展示了Any类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Any类的具体详情如下:
包路径:com.google.protobuf.Any
类名称:Any

Any介绍

[英]```
Any contains an arbitrary serialized protocol buffer message along with a
URL that describes the type of the serialized message.
Protobuf library provides support to pack/unpack Any values in the form
of utility functions or additional generated methods of the Any type.
Example 1: Pack and unpack a message in C++.
Foo foo = ...;
Any any;
any.PackFrom(foo);
...
if (any.UnpackTo(&foo)) {
...
}
Example 2: Pack and unpack a message in Java.
Foo foo = ...;
Any any = Any.pack(foo);
...
if (any.is(Foo.class)) {
foo = any.unpack(Foo.class);
}
Example 3: Pack and unpack a message in Python.
foo = Foo(...)
any = Any()
any.Pack(foo)
...
if any.Is(Foo.DESCRIPTOR):
any.Unpack(foo)
...
The pack methods provided by protobuf library will by default use
'type.googleapis.com/full.type.name' as the type URL and the unpack
methods only use the fully qualified type name after the last '/'
in the type URL, for example "foo.bar.com/x/y.z" will yield type
name "y.z".
JSON

The JSON representation of an Any value uses the regular
representation of the deserialized, embedded message, with an
additional field @type which contains the type URL. Example:
package google.profile;
message Person {
string first_name = 1;
string last_name = 2;
}
{
"@type": "type.googleapis.com/google.profile.Person",
"firstName": <string>,
"lastName": <string>
}
If the embedded message type is well-known and has a custom JSON
representation, that representation will be embedded adding a field
value which holds the custom JSON in addition to the @type
field. Example (for message [google.protobuf.Duration][]):
{
"@type": "type.googleapis.com/google.protobuf.Duration",
"value": "1.212s"
}

 Protobuf type google.protobuf.Any
[中]```
`Any` contains an arbitrary serialized protocol buffer message along with a 
URL that describes the type of the serialized message. 
Protobuf library provides support to pack/unpack Any values in the form 
of utility functions or additional generated methods of the Any type. 
Example 1: Pack and unpack a message in C++. 
Foo foo = ...; 
Any any; 
any.PackFrom(foo); 
... 
if (any.UnpackTo(&foo)) { 
... 
} 
Example 2: Pack and unpack a message in Java. 
Foo foo = ...; 
Any any = Any.pack(foo); 
... 
if (any.is(Foo.class)) { 
foo = any.unpack(Foo.class); 
} 
Example 3: Pack and unpack a message in Python. 
foo = Foo(...) 
any = Any() 
any.Pack(foo) 
... 
if any.Is(Foo.DESCRIPTOR): 
any.Unpack(foo) 
... 
The pack methods provided by protobuf library will by default use 
'type.googleapis.com/full.type.name' as the type URL and the unpack 
methods only use the fully qualified type name after the last '/' 
in the type URL, for example "foo.bar.com/x/y.z" will yield type 
name "y.z". 
JSON 
==== 
The JSON representation of an `Any` value uses the regular 
representation of the deserialized, embedded message, with an 
additional field `@type` which contains the type URL. Example: 
package google.profile; 
message Person { 
string first_name = 1; 
string last_name = 2; 
} 
{ 
"@type": "type.googleapis.com/google.profile.Person", 
"firstName": <string>, 
"lastName": <string> 
} 
If the embedded message type is well-known and has a custom JSON 
representation, that representation will be embedded adding a field 
`value` which holds the custom JSON in addition to the `@type` 
field. Example (for message [google.protobuf.Duration][]): 
{ 
"@type": "type.googleapis.com/google.protobuf.Duration", 
"value": "1.212s" 
}

Protobuf类型谷歌。protobuf。任何

代码示例

代码示例来源:origin: googleapis/google-cloud-java

@Override
public Object getMetadata() {
 return Any.pack(metadata);
}

代码示例来源:origin: googleapis/google-cloud-java

if (payloadCase_ == 2 && payload_ != com.google.protobuf.Any.getDefaultInstance()) {
  payload_ =
    com.google.protobuf.Any.newBuilder((com.google.protobuf.Any) payload_)
      .mergeFrom(value)
      .buildPartial();
} else {
 if (payloadCase_ == 2) {
  protoPayloadBuilder_.mergeFrom(value);
 protoPayloadBuilder_.setMessage(value);

代码示例来源:origin: com.google.protobuf/protobuf-java

@java.lang.Override
public boolean equals(final java.lang.Object obj) {
 if (obj == this) {
  return true;
 }
 if (!(obj instanceof com.google.protobuf.Option)) {
  return super.equals(obj);
 }
 com.google.protobuf.Option other = (com.google.protobuf.Option) obj;
 boolean result = true;
 result = result && getName()
   .equals(other.getName());
 result = result && (hasValue() == other.hasValue());
 if (hasValue()) {
  result = result && getValue()
    .equals(other.getValue());
 }
 result = result && unknownFields.equals(other.unknownFields);
 return result;
}

代码示例来源:origin: com.google.protobuf/protobuf-java

@java.lang.Override
public int hashCode() {
 if (memoizedHashCode != 0) {
  return memoizedHashCode;
 }
 int hash = 41;
 hash = (19 * hash) + getDescriptor().hashCode();
 hash = (37 * hash) + TYPE_URL_FIELD_NUMBER;
 hash = (53 * hash) + getTypeUrl().hashCode();
 hash = (37 * hash) + VALUE_FIELD_NUMBER;
 hash = (53 * hash) + getValue().hashCode();
 hash = (29 * hash) + unknownFields.hashCode();
 memoizedHashCode = hash;
 return hash;
}

代码示例来源:origin: com.google.protobuf/protobuf-java

@java.lang.Override
public boolean equals(final java.lang.Object obj) {
 if (obj == this) {
  return true;
 }
 if (!(obj instanceof com.google.protobuf.Any)) {
  return super.equals(obj);
 }
 com.google.protobuf.Any other = (com.google.protobuf.Any) obj;
 boolean result = true;
 result = result && getTypeUrl()
   .equals(other.getTypeUrl());
 result = result && getValue()
   .equals(other.getValue());
 result = result && unknownFields.equals(other.unknownFields);
 return result;
}

代码示例来源:origin: com.google.protobuf/protobuf-java

/**
 * <pre>
 * Must be a valid serialized protocol buffer of the above specified type.
 * </pre>
 *
 * <code>bytes value = 2;</code>
 */
public Builder clearValue() {
 
 value_ = getDefaultInstance().getValue();
 onChanged();
 return this;
}
@java.lang.Override

代码示例来源:origin: com.google.protobuf/protobuf-java

/**
 * <pre>
 * The option's value packed in an Any message. If the value is a primitive,
 * the corresponding wrapper type defined in google/protobuf/wrappers.proto
 * should be used. If the value is an enum, it should be stored as an int32
 * value using the google.protobuf.Int32Value type.
 * </pre>
 *
 * <code>.google.protobuf.Any value = 2;</code>
 */
public com.google.protobuf.AnyOrBuilder getValueOrBuilder() {
 if (valueBuilder_ != null) {
  return valueBuilder_.getMessageOrBuilder();
 } else {
  return value_ == null ?
    com.google.protobuf.Any.getDefaultInstance() : value_;
 }
}
/**

代码示例来源:origin: com.google.protobuf/protobuf-java

/**
 * <pre>
 * The option's value packed in an Any message. If the value is a primitive,
 * the corresponding wrapper type defined in google/protobuf/wrappers.proto
 * should be used. If the value is an enum, it should be stored as an int32
 * value using the google.protobuf.Int32Value type.
 * </pre>
 *
 * <code>.google.protobuf.Any value = 2;</code>
 */
public com.google.protobuf.Any getValue() {
 if (valueBuilder_ == null) {
  return value_ == null ? com.google.protobuf.Any.getDefaultInstance() : value_;
 } else {
  return valueBuilder_.getMessage();
 }
}
/**

代码示例来源:origin: com.google.protobuf/protobuf-java

/**
 * <pre>
 * The option's value packed in an Any message. If the value is a primitive,
 * the corresponding wrapper type defined in google/protobuf/wrappers.proto
 * should be used. If the value is an enum, it should be stored as an int32
 * value using the google.protobuf.Int32Value type.
 * </pre>
 *
 * <code>.google.protobuf.Any value = 2;</code>
 */
public Builder mergeValue(com.google.protobuf.Any value) {
 if (valueBuilder_ == null) {
  if (value_ != null) {
   value_ =
    com.google.protobuf.Any.newBuilder(value_).mergeFrom(value).buildPartial();
  } else {
   value_ = value;
  }
  onChanged();
 } else {
  valueBuilder_.mergeFrom(value);
 }
 return this;
}
/**

代码示例来源:origin: googleapis/google-cloud-java

if (protoPayloadBuilder_ == null) {
 if (!(payloadCase_ == 2)) {
  payload_ = com.google.protobuf.Any.getDefaultInstance();
   new com.google.protobuf.SingleFieldBuilderV3<
     com.google.protobuf.Any,
     com.google.protobuf.Any.Builder,

代码示例来源:origin: com.google.protobuf/protobuf-java

com.google.protobuf.Any.Builder subBuilder = null;
if (value_ != null) {
 subBuilder = value_.toBuilder();
value_ = input.readMessage(com.google.protobuf.Any.parser(), extensionRegistry);
if (subBuilder != null) {
 subBuilder.mergeFrom(value_);

代码示例来源:origin: com.google.protobuf/protobuf-java

public Builder mergeFrom(com.google.protobuf.Any other) {
 if (other == com.google.protobuf.Any.getDefaultInstance()) return this;
 if (!other.getTypeUrl().isEmpty()) {
  typeUrl_ = other.typeUrl_;
  onChanged();
 }
 if (other.getValue() != com.google.protobuf.ByteString.EMPTY) {
  setValue(other.getValue());
 }
 this.mergeUnknownFields(other.unknownFields);
 onChanged();
 return this;
}

代码示例来源:origin: Netflix/conductor

@Test
  public void testSimpleMapping() throws JsonGenerationException, JsonMappingException, IOException {
    ObjectMapper m = new JsonMapperProvider().get();
    assertTrue(m.canSerialize(Any.class));

    Struct struct1 = Struct.newBuilder().putFields(
        "some-key", Value.newBuilder().setStringValue("some-value").build()
    ).build();

    Any source = Any.pack(struct1);

    StringWriter buf = new StringWriter();
    m.writer().writeValue(buf, source);

    Any dest = m.reader().forType(Any.class).readValue(buf.toString());
    assertEquals(source.getTypeUrl(), dest.getTypeUrl());

    Struct struct2 = dest.unpack(Struct.class);
    assertTrue(struct2.containsFields("some-key"));
    assertEquals(
        struct1.getFieldsOrThrow("some-key").getStringValue(),
        struct2.getFieldsOrThrow("some-key").getStringValue()
    );
  }
}

代码示例来源:origin: com.google.protobuf/protobuf-java

public static <T extends com.google.protobuf.Message> Any pack(
  T message) {
 return Any.newBuilder()
   .setTypeUrl(getTypeUrl("type.googleapis.com",
               message.getDescriptorForType()))
   .setValue(message.toByteString())
   .build();
}

代码示例来源:origin: com.google.protobuf/protobuf-java

@java.lang.Override
public int hashCode() {
 if (memoizedHashCode != 0) {
  return memoizedHashCode;
 }
 int hash = 41;
 hash = (19 * hash) + getDescriptor().hashCode();
 hash = (37 * hash) + NAME_FIELD_NUMBER;
 hash = (53 * hash) + getName().hashCode();
 if (hasValue()) {
  hash = (37 * hash) + VALUE_FIELD_NUMBER;
  hash = (53 * hash) + getValue().hashCode();
 }
 hash = (29 * hash) + unknownFields.hashCode();
 memoizedHashCode = hash;
 return hash;
}

代码示例来源:origin: com.google.protobuf/protobuf-java

typeUrl_ = getDefaultInstance().getTypeUrl();
onChanged();
return this;

代码示例来源:origin: com.google.protobuf/protobuf-java

@java.lang.Override
public com.google.protobuf.Any getDefaultInstanceForType() {
 return com.google.protobuf.Any.getDefaultInstance();
}

代码示例来源:origin: yeriomin/play-store-api

/**
 * <pre>
 * The option's value. For example, `"com.google.protobuf"`.
 * </pre>
 *
 * <code>optional .google.protobuf.Any value = 2;</code>
 */
private void mergeValue(com.google.protobuf.Any value) {
 if (value_ != null &&
   value_ != com.google.protobuf.Any.getDefaultInstance()) {
  value_ =
   com.google.protobuf.Any.newBuilder(value_).mergeFrom(value).buildPartial();
 } else {
  value_ = value;
 }
 
}
/**

代码示例来源:origin: com.google.api/gax-grpc

@Override
 public PackedT apply(Any input) {
  try {
   return input == null || packedClass == null ? null : input.unpack(packedClass);
  } catch (InvalidProtocolBufferException | ClassCastException e) {
   throw new IllegalStateException(
     "Failed to unpack object from 'any' field. Expected "
       + packedClass.getName()
       + ", found "
       + input.getTypeUrl());
  }
 }
}

代码示例来源:origin: com.google.protobuf/protobuf-java

@java.lang.Override
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder() {

相关文章