com.google.protobuf.NullValue Java Examples

The following examples show how to use com.google.protobuf.NullValue. 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: ListValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void itReadsMixedTypeValues() throws IOException {
  String json = "{\"listValue\":[null,1.5,\"test\",true,{\"key\":\"value\"},[\"nested\"]]}";
  HasListValue message = camelCase().readValue(json, HasListValue.class);
  Struct struct = Struct.newBuilder().putFields("key", Value.newBuilder().setStringValue("value").build()).build();
  ListValue list = ListValue.newBuilder().addValues(Value.newBuilder().setStringValue("nested")).build();
  ListValue expected = ListValue
          .newBuilder()
          .addValues(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build())
          .addValues(Value.newBuilder().setNumberValue(1.5d).build())
          .addValues(Value.newBuilder().setStringValue("test").build())
          .addValues(Value.newBuilder().setBoolValue(true).build())
          .addValues(Value.newBuilder().setStructValue(struct).build())
          .addValues(Value.newBuilder().setListValue(list).build())
          .build();
  assertThat(message.hasListValue()).isTrue();
  assertThat(message.getListValue()).isEqualTo(expected);
}
 
Example #2
Source File: ValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void itReadsMixedStruct() throws IOException {
  String json = "{\"value\":{\"null\":null,\"number\":1.5,\"string\":\"test\",\"boolean\":true,\"struct\":{\"key\":\"nested\"},\"list\":[\"nested\"]}}";
  HasValue message = camelCase().readValue(json, HasValue.class);
  assertThat(message.hasValue()).isTrue();
  Value value = message.getValue();
  switch (value.getKindCase()) {
    case STRUCT_VALUE:
      Map<String, Value> map = value.getStructValue().getFieldsMap();
      Value nested = Value.newBuilder().setStringValue("nested").build();
      Struct nestedStruct = Struct.newBuilder().putFields("key", nested).build();
      ListValue list = ListValue.newBuilder().addValues(nested).build();
      assertThat(map.size()).isEqualTo(6);
      assertThat(map.get("null")).isEqualTo(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
      assertThat(map.get("number")).isEqualTo(Value.newBuilder().setNumberValue(1.5).build());
      assertThat(map.get("string")).isEqualTo(Value.newBuilder().setStringValue("test").build());
      assertThat(map.get("boolean")).isEqualTo(Value.newBuilder().setBoolValue(true).build());
      assertThat(map.get("struct")).isEqualTo(Value.newBuilder().setStructValue(nestedStruct).build());
      assertThat(map.get("list")).isEqualTo(Value.newBuilder().setListValue(list).build());
      break;
    default:
      fail("Unexpected value kind: " + value.getKindCase());
  }
}
 
Example #3
Source File: ValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void itWritesMixedListValue() throws IOException {
  Value nestedValue = Value.newBuilder().setStringValue("nested").build();
  Struct struct = Struct.newBuilder().putFields("key", nestedValue).build();
  ListValue nestedList = ListValue.newBuilder().addValues(nestedValue).build();
  ListValue list = ListValue
          .newBuilder()
          .addValues(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build())
          .addValues(Value.newBuilder().setNumberValue(1.5d).build())
          .addValues(Value.newBuilder().setStringValue("test").build())
          .addValues(Value.newBuilder().setBoolValue(true).build())
          .addValues(Value.newBuilder().setStructValue(struct).build())
          .addValues(Value.newBuilder().setListValue(nestedList).build())
          .build();
  HasValue message = HasValue
          .newBuilder()
          .setValue(Value.newBuilder().setListValue(list).build())
          .build();
  String json = camelCase().writeValueAsString(message);
  assertThat(json).isEqualTo("{\"value\":[null,1.5,\"test\",true,{\"key\":\"nested\"},[\"nested\"]]}");
}
 
Example #4
Source File: StructTest.java    From jackson-datatype-protobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void itReadsAllStructValueTypes() throws IOException {
  String json = "{\"struct\":{\"null\":null,\"number\":1.5,\"string\":\"test\",\"boolean\":true,\"struct\":{\"key\":\"nested\"},\"list\":[\"nested\"]}}";
  HasStruct message = camelCase().readValue(json, HasStruct.class);
  assertThat(message.hasStruct()).isTrue();

  Map<String, Value> map = message.getStruct().getFieldsMap();
  Value nested = Value.newBuilder().setStringValue("nested").build();
  Struct nestedStruct = Struct.newBuilder().putFields("key", nested).build();
  ListValue list = ListValue.newBuilder().addValues(nested).build();

  assertThat(map.size()).isEqualTo(6);
  assertThat(map.get("null")).isEqualTo(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
  assertThat(map.get("number")).isEqualTo(Value.newBuilder().setNumberValue(1.5).build());
  assertThat(map.get("string")).isEqualTo(Value.newBuilder().setStringValue("test").build());
  assertThat(map.get("boolean")).isEqualTo(Value.newBuilder().setBoolValue(true).build());
  assertThat(map.get("struct")).isEqualTo(Value.newBuilder().setStructValue(nestedStruct).build());
  assertThat(map.get("list")).isEqualTo(Value.newBuilder().setListValue(list).build());
}
 
Example #5
Source File: ValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void itReadsMixedListValue() throws IOException {
  String json = "{\"value\":[null,1.5,\"test\",true,{\"key\":\"nested\"},[\"nested\"]]}";
  HasValue message = camelCase().readValue(json, HasValue.class);
  assertThat(message.hasValue()).isTrue();
  Value value = message.getValue();
  switch (value.getKindCase()) {
    case LIST_VALUE:
      ListValue list = value.getListValue();
      Value nested = Value.newBuilder().setStringValue("nested").build();
      Struct struct = Struct.newBuilder().putFields("key", nested).build();
      ListValue nestedList = ListValue.newBuilder().addValues(nested).build();
      assertThat(list.getValuesCount()).isEqualTo(6);
      assertThat(list.getValues(0)).isEqualTo(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
      assertThat(list.getValues(1)).isEqualTo(Value.newBuilder().setNumberValue(1.5).build());
      assertThat(list.getValues(2)).isEqualTo(Value.newBuilder().setStringValue("test").build());
      assertThat(list.getValues(3)).isEqualTo(Value.newBuilder().setBoolValue(true).build());
      assertThat(list.getValues(4)).isEqualTo(Value.newBuilder().setStructValue(struct).build());
      assertThat(list.getValues(5)).isEqualTo(Value.newBuilder().setListValue(nestedList).build());
      break;
    default:
      fail("Unexpected value kind: " + value.getKindCase());
  }
}
 
Example #6
Source File: MessageMarshallerTest.java    From curiostack with MIT License 6 votes vote down vote up
@Test
public void includingDefaultValueFields() throws Exception {
  TestAllTypes message = TestAllTypes.getDefaultInstance();
  assertMatchesUpstream(message);
  assertMatchesUpstream(message, true, false, false, false, false);

  TestMap mapMessage = TestMap.getDefaultInstance();
  assertMatchesUpstream(mapMessage);
  assertMatchesUpstream(mapMessage, true, false, false, false, false);

  TestOneof oneofMessage = TestOneof.getDefaultInstance();
  assertMatchesUpstream(oneofMessage);
  assertMatchesUpstream(oneofMessage, true, false, false, false, false);

  oneofMessage = TestOneof.newBuilder().setOneofInt32(42).build();
  assertMatchesUpstream(oneofMessage);
  assertMatchesUpstream(oneofMessage, true, false, false, false, false);

  oneofMessage = TestOneof.newBuilder().setOneofNullValue(NullValue.NULL_VALUE).build();
  assertMatchesUpstream(oneofMessage);
  assertMatchesUpstream(oneofMessage, true, false, false, false, false);
}
 
Example #7
Source File: MessageMarshallerTest.java    From curiostack with MIT License 6 votes vote down vote up
@Test
public void anyInMaps() throws Exception {
  TestAny.Builder testAny = TestAny.newBuilder();
  testAny.putAnyMap("int32_wrapper", Any.pack(Int32Value.newBuilder().setValue(123).build()));
  testAny.putAnyMap("int64_wrapper", Any.pack(Int64Value.newBuilder().setValue(456).build()));
  testAny.putAnyMap("timestamp", Any.pack(Timestamps.parse("1969-12-31T23:59:59Z")));
  testAny.putAnyMap("duration", Any.pack(Durations.parse("12345.1s")));
  testAny.putAnyMap("field_mask", Any.pack(FieldMaskUtil.fromString("foo.bar,baz")));
  Value numberValue = Value.newBuilder().setNumberValue(1.125).build();
  Struct.Builder struct = Struct.newBuilder();
  struct.putFields("number", numberValue);
  testAny.putAnyMap("struct", Any.pack(struct.build()));
  Value nullValue = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
  testAny.putAnyMap(
      "list_value",
      Any.pack(ListValue.newBuilder().addValues(numberValue).addValues(nullValue).build()));
  testAny.putAnyMap("number_value", Any.pack(numberValue));
  testAny.putAnyMap("any_value_number", Any.pack(Any.pack(numberValue)));
  testAny.putAnyMap("any_value_default", Any.pack(Any.getDefaultInstance()));
  testAny.putAnyMap("default", Any.getDefaultInstance());

  assertMatchesUpstream(testAny.build(), TestAllTypes.getDefaultInstance());
}
 
Example #8
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 #9
Source File: MessageMarshallerTest.java    From curiostack with MIT License 5 votes vote down vote up
@Test
public void nullInOneOf() throws Exception {
  TestOneof.Builder builder = TestOneof.newBuilder();
  mergeFromJson("{\n" + "  \"oneofNullValue\": null \n" + "}", builder);
  TestOneof message = builder.build();
  assertEquals(TestOneof.OneofFieldCase.ONEOF_NULL_VALUE, message.getOneofFieldCase());
  assertEquals(NullValue.NULL_VALUE, message.getOneofNullValue());
}
 
Example #10
Source File: DoParse.java    From curiostack with MIT License 5 votes vote down vote up
/**
 * Determines whether we skip processing of the field if it is null. We usually skip null values
 * in the JSON to treat them as default, but must actually process the null for {@link Value} and
 * {@link NullValue} because it means their value must be set.
 */
private static boolean mustSkipNull(FieldDescriptor field) {
  if (field.isRepeated()) {
    return true;
  }
  if (field.getJavaType() == JavaType.MESSAGE
      && field.getMessageType() == Value.getDescriptor()) {
    return false;
  }
  if (field.getJavaType() == JavaType.ENUM && field.getEnumType() == NullValue.getDescriptor()) {
    return false;
  }
  return true;
}
 
Example #11
Source File: TypesBuilderFromDescriptor.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Creates additional Enums (NullValue) to be added to the Service config.
 */
// TODO (guptasu): Fix this hack. Find a better way to add the predefined types.
// TODO (guptasu): Add them only when required and not in all cases.
static Iterable<Enum> createAdditionalServiceEnums() {
  // TODO (guptasu): Remove this hard coding. Without this, creation of Model from Service throws.
  // Needs investigation.
  String fileName = "struct.proto";
  List<Enum> additionalEnums = Lists.newArrayList();
  additionalEnums.add(TypesBuilderFromDescriptor.createEnum(NullValue.getDescriptor().getFullName(),
      NullValue.getDescriptor().toProto(), fileName));
  return additionalEnums;
}
 
Example #12
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 #13
Source File: ValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void itReadsNullValue() throws IOException {
  String json = "{\"value\":null}";
  HasValue valueWrapper = camelCase().readValue(json, HasValue.class);
  assertThat(valueWrapper.hasValue()).isTrue();

  Value value = valueWrapper.getValue();
  switch (value.getKindCase()) {
    case NULL_VALUE:
      assertThat(value.getNullValue()).isEqualTo(NullValue.NULL_VALUE);
      break;
    default:
      fail("Unexpected value kind: " + value.getKindCase());
  }
}
 
Example #14
Source File: NullValueDeserializer.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Override
public NullValue deserialize(JsonParser parser, DeserializationContext context) throws IOException {
  switch (parser.getCurrentToken()) {
    case VALUE_NULL:
      return NullValue.NULL_VALUE;
    default:
      context.reportWrongTokenException(NullValue.class, JsonToken.VALUE_NULL, wrongTokenMessage(context));
      // the previous method should have thrown
      throw new AssertionError();
  }
}
 
Example #15
Source File: BigQueryConvertersTest.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that BigQueryConverters.columnToValue() returns a null {@link Value} when the BigQuery
 * column is null.
 */
@Test
public void testColumnToValueNull() {
  TableFieldSchema column = new TableFieldSchema().setName(nullField).setType("STRING");
  Record record = generateSingleFieldAvroRecord(nullField, "null", nullFieldDesc, null);
  Value value = BigQueryConverters.columnToValue(column, record.get(nullField));
  assertEquals(NullValue.NULL_VALUE, value.getNullValue());
}
 
Example #16
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 #17
Source File: ValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void itWritesMixedStruct() throws IOException {
  Value nestedValue = Value.newBuilder().setStringValue("nested").build();
  Struct nestedStruct = Struct.newBuilder().putFields("key", nestedValue).build();
  ListValue list = ListValue.newBuilder().addValues(nestedValue).build();
  Struct struct = Struct
          .newBuilder()
          .putFields("null", Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build())
          .putFields("number", Value.newBuilder().setNumberValue(1.5d).build())
          .putFields("string", Value.newBuilder().setStringValue("test").build())
          .putFields("boolean", Value.newBuilder().setBoolValue(true).build())
          .putFields("struct", Value.newBuilder().setStructValue(nestedStruct).build())
          .putFields("list", Value.newBuilder().setListValue(list).build())
          .build();
  HasValue message = HasValue
          .newBuilder()
          .setValue(Value.newBuilder().setStructValue(struct).build())
          .build();
  String json = camelCase().writeValueAsString(message);
  JsonNode node = camelCase().readTree(json).get("value");
  assertThat(node.get("null").isNull()).isTrue();
  assertThat(node.get("number").isNumber()).isTrue();
  assertThat(node.get("number").numberValue().doubleValue()).isEqualTo(1.5d);
  assertThat(node.get("string").isTextual()).isTrue();
  assertThat(node.get("string").textValue()).isEqualTo("test");
  assertThat(node.get("boolean").isBoolean()).isTrue();
  assertThat(node.get("boolean").booleanValue()).isTrue();
  assertThat(node.get("struct").isObject()).isTrue();
  assertThat(node.get("struct").size()).isEqualTo(1);
  assertThat(node.get("struct").get("key").isTextual()).isTrue();
  assertThat(node.get("struct").get("key").textValue()).isEqualTo("nested");
  assertThat(node.get("list").isArray()).isTrue();
  assertThat(node.get("list").size()).isEqualTo(1);
  assertThat(node.get("list").get(0).isTextual()).isTrue();
  assertThat(node.get("list").get(0).textValue()).isEqualTo("nested");
}
 
Example #18
Source File: UserDataReader.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
private <T> Value parseList(List<T> list, ParseContext context) {
  ArrayValue.Builder arrayBuilder = ArrayValue.newBuilder();
  int entryIndex = 0;
  for (T entry : list) {
    @Nullable Value parsedEntry = parseData(entry, context.childContext(entryIndex));
    if (parsedEntry == null) {
      // Just include nulls in the array for fields being replaced with a sentinel.
      parsedEntry = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
    }
    arrayBuilder.addValues(parsedEntry);
    entryIndex++;
  }
  return Value.newBuilder().setArrayValue(arrayBuilder).build();
}
 
Example #19
Source File: RepeatedValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void itReadsMixedTypeValues() throws IOException {
  String json = "{\"values\":[null,1.5,\"test\",true,{\"key\":\"value\"},[\"nested\"]]}";
  RepeatedValue message = camelCase().readValue(json, RepeatedValue.class);
  assertThat(message.getValuesCount()).isEqualTo(6);
  assertThat(message.getValues(0)).isEqualTo(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
  assertThat(message.getValues(1)).isEqualTo(Value.newBuilder().setNumberValue(1.5d).build());
  assertThat(message.getValues(2)).isEqualTo(Value.newBuilder().setStringValue("test").build());
  assertThat(message.getValues(3)).isEqualTo(Value.newBuilder().setBoolValue(true).build());
  assertThat(message.getValues(4)).isEqualTo(Value.newBuilder().setStructValue(STRUCT).build());
  assertThat(message.getValues(5)).isEqualTo(LIST);
}
 
Example #20
Source File: ValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void itWritesNullValue() throws IOException {
  HasValue message = HasValue
          .newBuilder()
          .setValue(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build())
          .build();
  String json = camelCase().writeValueAsString(message);
  assertThat(json).isEqualTo("{\"value\":null}");
}
 
Example #21
Source File: StructTest.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void itWritesAllStructValueTypes() throws IOException {
  Value nestedValue = Value.newBuilder().setStringValue("nested").build();
  Struct nestedStruct = Struct.newBuilder().putFields("key", nestedValue).build();
  ListValue list = ListValue.newBuilder().addValues(nestedValue).build();
  Struct struct = Struct
          .newBuilder()
          .putFields("null", Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build())
          .putFields("number", Value.newBuilder().setNumberValue(1.5d).build())
          .putFields("string", Value.newBuilder().setStringValue("test").build())
          .putFields("boolean", Value.newBuilder().setBoolValue(true).build())
          .putFields("struct", Value.newBuilder().setStructValue(nestedStruct).build())
          .putFields("list", Value.newBuilder().setListValue(list).build())
          .build();
  HasStruct message = HasStruct
          .newBuilder()
          .setStruct(struct)
          .build();
  String json = camelCase().writeValueAsString(message);
  JsonNode node = camelCase().readTree(json).get("struct");
  assertThat(node.get("null").isNull()).isTrue();
  assertThat(node.get("number").isNumber()).isTrue();
  assertThat(node.get("number").numberValue().doubleValue()).isEqualTo(1.5d);
  assertThat(node.get("string").isTextual()).isTrue();
  assertThat(node.get("string").textValue()).isEqualTo("test");
  assertThat(node.get("boolean").isBoolean()).isTrue();
  assertThat(node.get("boolean").booleanValue()).isTrue();
  assertThat(node.get("struct").isObject()).isTrue();
  assertThat(node.get("struct").size()).isEqualTo(1);
  assertThat(node.get("struct").get("key").isTextual()).isTrue();
  assertThat(node.get("struct").get("key").textValue()).isEqualTo("nested");
  assertThat(node.get("list").isArray()).isTrue();
  assertThat(node.get("list").size()).isEqualTo(1);
  assertThat(node.get("list").get(0).isTextual()).isTrue();
  assertThat(node.get("list").get(0).textValue()).isEqualTo("nested");
}
 
Example #22
Source File: NullValueSerializer.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
public NullValueSerializer() {
  super(NullValue.class);
}
 
Example #23
Source File: NullValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
@Test
public void itReadsNullValueWhenSet() throws IOException {
  String json = "{\"nullValue\":null}";
  HasNullValue message = camelCase().readValue(json, HasNullValue.class);
  assertThat(message.getNullValue()).isEqualTo(NullValue.NULL_VALUE);
}
 
Example #24
Source File: NullValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
@Test
public void itReadsNullValueWhenNotSet() throws IOException {
  String json = "{}";
  HasNullValue message = camelCase().readValue(json, HasNullValue.class);
  assertThat(message.getNullValue()).isEqualTo(NullValue.NULL_VALUE);
}
 
Example #25
Source File: NullValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
@Test
public void itWritesNullValueWhenSetWithDefaultInclusion() throws IOException {
  HasNullValue message = HasNullValue.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
  String json = camelCase().writeValueAsString(message);
  assertThat(json).isEqualTo("{\"nullValue\":null}");
}
 
Example #26
Source File: NullValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
@Test
public void itOmitsNullValueWhenSetWithNonDefaultInclusion() throws IOException {
  HasNullValue message = HasNullValue.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
  String json = camelCase(Include.NON_DEFAULT).writeValueAsString(message);
  assertThat(json).isEqualTo("{}");
}
 
Example #27
Source File: NullValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
@Test
public void itWritesNullValueWhenSetWithAlwaysInclusion() throws IOException {
  HasNullValue message = HasNullValue.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
  String json = camelCase(Include.ALWAYS).writeValueAsString(message);
  assertThat(json).isEqualTo("{\"nullValue\":null}");
}
 
Example #28
Source File: NullValueTest.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
@Test
public void itWritesNullValueWhenSetWithNonNullInclusion() throws IOException {
  HasNullValue message = HasNullValue.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
  String json = camelCase(Include.NON_NULL).writeValueAsString(message);
  assertThat(json).isEqualTo("{\"nullValue\":null}");
}
 
Example #29
Source File: AllMapValuesTest.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
private static HasAllMapValues hasAllMapValues() {
  Value value = Value.newBuilder().setStringValue("test").build();
  ByteString byteString = ByteString.copyFromUtf8("test");
  Any any = Any
          .newBuilder()
          .setTypeUrl("type.googleapis.com/google.protobuf.Value")
          .setValue(value.toByteString())
          .build();
  return HasAllMapValues
          .newBuilder()
          .putDoubleMap("double", 1.5d)
          .putFloatMap("float", 2.5f)
          .putInt32Map("int32", 1)
          .putInt64Map("int64", 2)
          .putUint32Map("uint32", 3)
          .putUint64Map("uint64", 4)
          .putSint32Map("sint32", 5)
          .putSint64Map("sint64", 6)
          .putFixed32Map("fixed32", 7)
          .putFixed64Map("fixed64", 8)
          .putSfixed32Map("sfixed32", 9)
          .putSfixed64Map("sfixed64", 10)
          .putBoolMap("bool", true)
          .putStringMap("string", "test")
          .putBytesMap("bytes", byteString)
          .putAnyMap("any", any)
          .putDurationMap("duration", Duration.newBuilder().setSeconds(30).build())
          .putFieldMaskMap("field_mask", FieldMask.newBuilder().addPaths("path_one").addPaths("path_two").build())
          .putListValueMap("list_value", ListValue.newBuilder().addValues(value).build())
          .putNullValueMap("null_value", NullValue.NULL_VALUE)
          .putStructMap("struct", Struct.newBuilder().putFields("field", value).build())
          .putTimestampMap("timestamp", Timestamp.newBuilder().setSeconds(946684800).build())
          .putValueMap("value", value)
          .putDoubleWrapperMap("double_wrapper", DoubleValue.newBuilder().setValue(3.5d).build())
          .putFloatWrapperMap("float_wrapper", FloatValue.newBuilder().setValue(4.5f).build())
          .putInt32WrapperMap("int32_wrapper", Int32Value.newBuilder().setValue(11).build())
          .putInt64WrapperMap("int64_wrapper", Int64Value.newBuilder().setValue(12).build())
          .putUint32WrapperMap("uint32_wrapper", UInt32Value.newBuilder().setValue(13).build())
          .putUint64WrapperMap("uint64_wrapper", UInt64Value.newBuilder().setValue(14).build())
          .putBoolWrapperMap("bool_wrapper", BoolValue.newBuilder().setValue(true).build())
          .putStringWrapperMap("string_wrapper", StringValue.newBuilder().setValue("test").build())
          .putBytesWrapperMap("bytes_wrapper", BytesValue.newBuilder().setValue(byteString).build())
          .putEnumMap("enum", EnumProto3.FIRST)
          .putProto2MessageMap("proto2", AllFields.newBuilder().setString("proto2").build())
          .putProto3MessageMap("proto3", AllFieldsProto3.newBuilder().setString("proto3").build())
          .build();
}
 
Example #30
Source File: NullValueSerializer.java    From jackson-datatype-protobuf with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(NullValue value, JsonGenerator gen, SerializerProvider provider) throws IOException {
  gen.writeNull();
}