com.google.protobuf.Any#Builder ( )源码实例Demo

下面列出了com.google.protobuf.Any#Builder ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: curiostack   文件: WellKnownTypeMarshaller.java
@Override
public void doMerge(JsonParser parser, int currentDepth, Message.Builder messageBuilder)
    throws IOException {
  JsonToken token = parser.nextValue();
  if (token == JsonToken.END_OBJECT) {
    return;
  }
  Any.Builder builder = (Any.Builder) messageBuilder;
  if (!parser.getCurrentName().equals("@type")) {
    throw new InvalidProtocolBufferException(
        "MessageMarshaller requires @type to must be the "
            + "first field of an Any. If you need to support @type in any location, use "
            + "upstream JsonFormat. Found: "
            + parser.getText());
  }
  String typeUrl = ParseSupport.parseString(parser);
  TypeSpecificMarshaller<?> contentMarshaller = marshallerRegistry.findByTypeUrl(typeUrl);
  builder.setTypeUrl(typeUrl);
  if (contentMarshaller instanceof WellKnownTypeMarshaller) {
    parser.nextValue();
    if (parser.getCurrentName().equals("value")) {
      builder.setValue(contentMarshaller.readValue(parser, currentDepth).toByteString());
    }
    // Well-known types will not finish parsing the current object (they don't readValue
    // objects),
    // so we close it here.
    if (parser.nextValue() != JsonToken.END_OBJECT) {
      throw new InvalidProtocolBufferException(
          "Expected end of object, got: " + parser.getText());
    }
  } else {
    builder.setValue(
        contentMarshaller
            .parseRemainingFieldsOfObjectAsMessage(parser, currentDepth + 1)
            .toByteString());
  }
}
 
源代码2 项目: curiostack   文件: MessageMarshallerTest.java
@Test
public void parserUnexpectedTypeUrl() throws Exception {
  Any.Builder builder = Any.newBuilder();
  assertThatThrownBy(
          () ->
              mergeFromJson(
                  "{\n"
                      + "  \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n"
                      + "  \"optionalInt32\": 12345\n"
                      + "}",
                  builder))
      .isInstanceOf(InvalidProtocolBufferException.class);
}
 
源代码3 项目: curiostack   文件: MessageMarshallerTest.java
@Test
public void emptyWrapperTypesInAny() throws Exception {
  Any.Builder builder = Any.newBuilder();
  mergeFromJson(
      "{\n"
          + "  \"@type\": \"type.googleapis.com/google.protobuf.BoolValue\",\n"
          + "  \"value\": false\n"
          + "}\n",
      builder,
      TestAllTypes.getDefaultInstance());
  Any any = builder.build();
  assertEquals(0, any.getValue().size());
}
 
源代码4 项目: curiostack   文件: MessageMarshallerTest.java
@Test
public void parserMissingTypeUrl() throws Exception {
  Any.Builder builder = Any.newBuilder();
  assertThatThrownBy(() -> mergeFromJson("{\n" + "  \"optionalInt32\": 1234\n" + "}", builder))
      .isInstanceOf(InvalidProtocolBufferException.class);
}