Java Code Examples for com.google.protobuf.Any#Builder

The following examples show how to use com.google.protobuf.Any#Builder . 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 5 votes vote down vote up
@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());
  }
}
 
Example 2
Source File: MessageMarshallerTest.java    From curiostack with MIT License 5 votes vote down vote up
@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);
}
 
Example 3
Source File: MessageMarshallerTest.java    From curiostack with MIT License 5 votes vote down vote up
@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());
}
 
Example 4
Source File: MessageMarshallerTest.java    From curiostack with MIT License 4 votes vote down vote up
@Test
public void parserMissingTypeUrl() throws Exception {
  Any.Builder builder = Any.newBuilder();
  assertThatThrownBy(() -> mergeFromJson("{\n" + "  \"optionalInt32\": 1234\n" + "}", builder))
      .isInstanceOf(InvalidProtocolBufferException.class);
}