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

The following examples show how to use com.google.protobuf.Value#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 6 votes vote down vote up
@Override
public void doMerge(JsonParser parser, int currentDepth, Message.Builder messageBuilder)
    throws IOException {
  Value.Builder builder = (Value.Builder) messageBuilder;
  JsonToken token = parser.currentToken();
  if (token.isBoolean()) {
    builder.setBoolValue(ParseSupport.parseBool(parser));
  } else if (token.isNumeric()) {
    builder.setNumberValue(ParseSupport.parseDouble(parser));
  } else if (token == JsonToken.VALUE_NULL) {
    builder.setNullValue(NullValue.NULL_VALUE);
  } else if (token.isScalarValue()) {
    builder.setStringValue(ParseSupport.parseString(parser));
  } else if (token == JsonToken.START_OBJECT) {
    Struct.Builder structBuilder = builder.getStructValueBuilder();
    StructMarshaller.INSTANCE.mergeValue(parser, currentDepth + 1, structBuilder);
  } else if (token == JsonToken.START_ARRAY) {
    ListValue.Builder listValueBuilder = builder.getListValueBuilder();
    ListValueMarshaller.INSTANCE.mergeValue(parser, currentDepth + 1, listValueBuilder);
  } else {
    throw new IllegalStateException("Unexpected json data: " + parser.getText());
  }
}
 
Example 2
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.currentToken();
  if (token != JsonToken.START_ARRAY) {
    throw new InvalidProtocolBufferException("Expect an array but found: " + parser.getText());
  }
  ListValue.Builder builder = (ListValue.Builder) messageBuilder;
  while (parser.nextValue() != JsonToken.END_ARRAY) {
    Value.Builder valueBuilder = builder.addValuesBuilder();
    ValueMarshaller.INSTANCE.mergeValue(parser, currentDepth + 1, valueBuilder);
  }
}
 
Example 3
Source File: KeyValueEntity.java    From modeldb with Apache License 2.0 5 votes vote down vote up
public KeyValue getProtoKeyValue() throws InvalidProtocolBufferException {
  Value.Builder valueBuilder = Value.newBuilder();
  try {
    valueBuilder = (Builder) ModelDBUtils.getProtoObjectFromString(value, valueBuilder);
  } catch (InvalidProtocolBufferException e) {
    LOGGER.warn("Error generating builder for {}", value);
    throw e;
  }
  return KeyValue.newBuilder()
      .setKey(key)
      .setValue(valueBuilder.build())
      .setValueTypeValue(value_type)
      .build();
}
 
Example 4
Source File: Bootstrapper.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Converts Java representation of the given JSON value to protobuf's {@link
 * com.google.protobuf.Value} representation.
 *
 * <p>The given {@code rawObject} must be a valid JSON value in Java representation, which is
 * either a {@code Map<String, ?>}, {@code List<?>}, {@code String}, {@code Double},
 * {@code Boolean}, or {@code null}.
 */
private static Value convertToValue(Object rawObject) {
  Value.Builder valueBuilder = Value.newBuilder();
  if (rawObject == null) {
    valueBuilder.setNullValue(NullValue.NULL_VALUE);
  } else if (rawObject instanceof Double) {
    valueBuilder.setNumberValue((Double) rawObject);
  } else if (rawObject instanceof String) {
    valueBuilder.setStringValue((String) rawObject);
  } else if (rawObject instanceof Boolean) {
    valueBuilder.setBoolValue((Boolean) rawObject);
  } else if (rawObject instanceof Map) {
    Struct.Builder structBuilder = Struct.newBuilder();
    @SuppressWarnings("unchecked")
    Map<String, ?> map = (Map<String, ?>) rawObject;
    for (Map.Entry<String, ?> entry : map.entrySet()) {
      structBuilder.putFields(entry.getKey(), convertToValue(entry.getValue()));
    }
    valueBuilder.setStructValue(structBuilder);
  } else if (rawObject instanceof List) {
    ListValue.Builder listBuilder = ListValue.newBuilder();
    List<?> list = (List<?>) rawObject;
    for (Object obj : list) {
      listBuilder.addValues(convertToValue(obj));
    }
    valueBuilder.setListValue(listBuilder);
  }
  return valueBuilder.build();
}
 
Example 5
Source File: ValueDeserializer.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Override
protected void populate(
        Value.Builder builder,
        JsonParser parser,
        DeserializationContext context
) throws IOException {
  switch (parser.getCurrentToken()) {
    case START_OBJECT:
      Object structValue = readValue(builder, STRUCT_FIELD, null, parser, context);
      builder.setField(STRUCT_FIELD, structValue);
      return;
    case START_ARRAY:
      Object listValue = readValue(builder, LIST_FIELD, null, parser, context);
      builder.setField(LIST_FIELD, listValue);
      return;
    case VALUE_STRING:
      builder.setStringValue(parser.getText());
      return;
    case VALUE_NUMBER_INT:
    case VALUE_NUMBER_FLOAT:
      builder.setNumberValue(parser.getValueAsDouble());
      return;
    case VALUE_TRUE:
      builder.setBoolValue(true);
      return;
    case VALUE_FALSE:
      builder.setBoolValue(false);
      return;
    case VALUE_NULL:
      builder.setNullValue(NullValue.NULL_VALUE);
      return;
    default:
      String message = "Can not deserialize instance of com.google.protobuf.Value out of " + parser.currentToken() + " token";
      context.reportInputMismatch(Value.class, message);
      // the previous method should have thrown
      throw new AssertionError();
  }
}
 
Example 6
Source File: MessageMarshallerTest.java    From curiostack with MIT License 4 votes vote down vote up
@Test
public void anyFields() throws Exception {
  TestAllTypes content = TestAllTypes.newBuilder().setOptionalInt32(1234).build();
  TestAny message = TestAny.newBuilder().setAnyValue(Any.pack(content)).build();
  assertMatchesUpstream(message, TestAllTypes.getDefaultInstance());

  TestAny messageWithDefaultAnyValue =
      TestAny.newBuilder().setAnyValue(Any.getDefaultInstance()).build();
  assertMatchesUpstream(messageWithDefaultAnyValue);

  // Well-known types have a special formatting when embedded in Any.
  //
  // 1. Any in Any.
  Any anyMessage = Any.pack(Any.pack(content));
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 2. Wrappers in Any.
  anyMessage = Any.pack(Int32Value.newBuilder().setValue(12345).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(UInt32Value.newBuilder().setValue(12345).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(Int64Value.newBuilder().setValue(12345).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(UInt64Value.newBuilder().setValue(12345).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(FloatValue.newBuilder().setValue(12345).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(DoubleValue.newBuilder().setValue(12345).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(BoolValue.newBuilder().setValue(true).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage = Any.pack(StringValue.newBuilder().setValue("Hello").build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
  anyMessage =
      Any.pack(BytesValue.newBuilder().setValue(ByteString.copyFrom(new byte[] {1, 2})).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 3. Timestamp in Any.
  anyMessage = Any.pack(Timestamps.parse("1969-12-31T23:59:59Z"));
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 4. Duration in Any
  anyMessage = Any.pack(Durations.parse("12345.10s"));
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 5. FieldMask in Any
  anyMessage = Any.pack(FieldMaskUtil.fromString("foo.bar,baz"));
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 6. Struct in Any
  Struct.Builder structBuilder = Struct.newBuilder();
  structBuilder.putFields("number", Value.newBuilder().setNumberValue(1.125).build());
  anyMessage = Any.pack(structBuilder.build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 7. Value (number type) in Any
  Value.Builder valueBuilder = Value.newBuilder();
  valueBuilder.setNumberValue(1);
  anyMessage = Any.pack(valueBuilder.build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());

  // 8. Value (null type) in Any
  anyMessage = Any.pack(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
  assertMatchesUpstream(anyMessage, TestAllTypes.getDefaultInstance());
}
 
Example 7
Source File: ValueDeserializer.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
@Override
public Value.Builder getNullValue(DeserializationContext ctxt) {
  return Value.newBuilder().setNullValue(NullValue.NULL_VALUE);
}