Rename the 'includingDefaultValueWithoutPresenceFields' and 'always_print_without_presence_fields' to 'alwaysPrintFieldsWithNoPresence' in the Cpp, Py and Java JSON serializers for consistency.

PiperOrigin-RevId: 604292220
pull/15703/head
Protobuf Team Bot 10 months ago committed by Copybara-Service
parent 4cbe13b5c6
commit 7d43131a0a
  1. 7
      java/util/src/main/java/com/google/protobuf/util/JsonFormat.java
  2. 17
      java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java
  3. 8
      python/google/protobuf/internal/json_format_test.py
  4. 30
      python/google/protobuf/json_format.py
  5. 4
      src/google/protobuf/json/internal/unparser.cc
  6. 4
      src/google/protobuf/json/internal/writer.h
  7. 8
      src/google/protobuf/json/json.cc
  8. 4
      src/google/protobuf/json/json.h
  9. 2
      src/google/protobuf/json/json_test.cc

@ -187,7 +187,7 @@ public class JsonFormat {
*
* <p>The new Printer clones all other configurations from the current {@link Printer}.
*
* @deprecated Prefer {@link #includingDefaultValueWithoutPresenceFields}
* @deprecated Prefer {@link #alwaysPrintFieldsWithNoPresence}
*/
@Deprecated
public Printer includingDefaultValueFields() {
@ -241,10 +241,9 @@ public class JsonFormat {
* presence scalars set to their default value). The new Printer clones all other configurations
* from the current {@link Printer}.
*/
public Printer includingDefaultValueWithoutPresenceFields() {
public Printer alwaysPrintFieldsWithNoPresence() {
if (shouldPrintDefaults != ShouldPrintDefaults.ONLY_IF_PRESENT) {
throw new IllegalStateException(
"JsonFormat includingDefaultValueFields has already been set.");
throw new IllegalStateException("Only one of the JsonFormat defaults options can be set.");
}
return new Printer(
registry,

@ -1527,11 +1527,11 @@ public class JsonFormatTest {
+ " \"repeatedRecursive\": []\n"
+ "}";
// includingDefaultValueFields() and includingDefaultValueWithoutPresenceFields() should
// includingDefaultValueFields() and alwaysPrintFieldsWithNoPresence() should
// behave identically on the proto3 test message:
assertThat(JsonFormat.printer().includingDefaultValueFields().print(message))
.isEqualTo(expectedJsonWithDefaults);
assertThat(JsonFormat.printer().includingDefaultValueWithoutPresenceFields().print(message))
assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(message))
.isEqualTo(expectedJsonWithDefaults);
}
@ -1742,16 +1742,14 @@ public class JsonFormatTest {
assertThat(JsonFormat.printer().print(oneofMessage)).isEqualTo("{\n}");
assertThat(JsonFormat.printer().includingDefaultValueFields().print(oneofMessage))
.isEqualTo("{\n}");
assertThat(
JsonFormat.printer().includingDefaultValueWithoutPresenceFields().print(oneofMessage))
assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(oneofMessage))
.isEqualTo("{\n}");
oneofMessage = TestOneof.newBuilder().setOneofInt32(42).build();
assertThat(JsonFormat.printer().print(oneofMessage)).isEqualTo("{\n \"oneofInt32\": 42\n}");
assertThat(JsonFormat.printer().includingDefaultValueFields().print(oneofMessage))
.isEqualTo("{\n \"oneofInt32\": 42\n}");
assertThat(
JsonFormat.printer().includingDefaultValueWithoutPresenceFields().print(oneofMessage))
assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(oneofMessage))
.isEqualTo("{\n \"oneofInt32\": 42\n}");
TestOneof.Builder oneofBuilder = TestOneof.newBuilder();
@ -1761,8 +1759,7 @@ public class JsonFormatTest {
.isEqualTo("{\n \"oneofNullValue\": null\n}");
assertThat(JsonFormat.printer().includingDefaultValueFields().print(oneofMessage))
.isEqualTo("{\n \"oneofNullValue\": null\n}");
assertThat(
JsonFormat.printer().includingDefaultValueWithoutPresenceFields().print(oneofMessage))
assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(oneofMessage))
.isEqualTo("{\n \"oneofNullValue\": null\n}");
}
@ -1770,7 +1767,7 @@ public class JsonFormatTest {
public void testIncludingDefaultValueOptionsWithProto2Optional() throws Exception {
TestAllTypesProto2 message = TestAllTypesProto2.getDefaultInstance();
assertThat(JsonFormat.printer().print(message)).isEqualTo("{\n}");
// includingDefaultValueFields() and includingDefaultValueWithoutPresenceFields()
// includingDefaultValueFields() and alwaysPrintFieldsWithNoPresence()
// behave differently on a proto2 message: the former includes the proto2 explicit presence
// fields and the latter does not.
assertThat(JsonFormat.printer().includingDefaultValueFields().print(message))
@ -1812,7 +1809,7 @@ public class JsonFormatTest {
+ " \"optionalAliasedEnum\": \"ALIAS_FOO\",\n"
+ " \"repeatedRecursive\": []\n"
+ "}");
assertThat(JsonFormat.printer().includingDefaultValueWithoutPresenceFields().print(message))
assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(message))
.isEqualTo(
"{\n"
+ " \"repeatedInt32\": [],\n"

@ -330,7 +330,7 @@ class JsonFormatTest(JsonFormatBase):
self.assertEqual(
json.loads(
json_format.MessageToJson(
message, including_default_value_without_presence_fields=True
message, always_print_fields_with_no_presence=True
)
),
json.loads('{"repeatedInt32": [], "repeatedNestedMessage": []}'),
@ -339,7 +339,7 @@ class JsonFormatTest(JsonFormatBase):
self.assertEqual(
json.loads(
json_format.MessageToJson(
message, including_default_value_without_presence_fields=True
message, always_print_fields_with_no_presence=True
)
),
json.loads(
@ -391,7 +391,7 @@ class JsonFormatTest(JsonFormatBase):
self.assertEqual(
json.loads(
json_format.MessageToJson(
message, including_default_value_without_presence_fields=True
message, always_print_fields_with_no_presence=True
)
),
json.loads('{"repeatedInt32": [], "repeatedNestedMessage": []}'),
@ -400,7 +400,7 @@ class JsonFormatTest(JsonFormatBase):
self.assertEqual(
json.loads(
json_format.MessageToJson(
message, including_default_value_without_presence_fields=True
message, always_print_fields_with_no_presence=True
)
),
json.loads(

@ -80,19 +80,19 @@ def MessageToJson(
descriptor_pool=None,
float_precision=None,
ensure_ascii=True,
including_default_value_without_presence_fields=False,
always_print_fields_with_no_presence=False,
):
"""Converts protobuf message to JSON format.
Args:
message: The protocol buffers message instance to serialize.
including_default_value_fields: (DEPRECATED: use
including_default_value_without_presence_fields to correctly treats proto2
and proto3 optional the same). If True, fields without presence (implicit
always_print_fields_with_no_presence which correctly treats proto2
and proto3 optionals the same). If True, fields without presence (implicit
presence scalars, repeated fields, and map fields) and Proto2 optional
scalars will always be serialized. Singular message fields, oneof fields
and Proto3 optional scalars are not affected by this option.
including_default_value_without_presence_fields: If True, fields without
always_print_fields_with_no_presence: If True, fields without
presence (implicit presence scalars, repeated fields, and map fields) will
always be serialized. Any field that supports presence is not affected by
this option (including singular message fields and oneof fields).
@ -119,7 +119,7 @@ def MessageToJson(
use_integers_for_enums,
descriptor_pool,
float_precision,
including_default_value_without_presence_fields
always_print_fields_with_no_presence
)
return printer.ToJsonString(message, indent, sort_keys, ensure_ascii)
@ -127,7 +127,7 @@ def MessageToJson(
def MessageToDict(
message,
including_default_value_fields=False,
including_default_value_without_presence_fields=False,
always_print_fields_with_no_presence=False,
preserving_proto_field_name=False,
use_integers_for_enums=False,
descriptor_pool=None,
@ -140,12 +140,12 @@ def MessageToDict(
Args:
message: The protocol buffers message instance to serialize.
including_default_value_fields: (DEPRECATED: use
including_default_value_without_presence_fields to correctly treats proto2
always_print_fields_with_no_presence to correctly treats proto2
and proto3 optional the same). If True, fields without presence (implicit
presence scalars, repeated fields, and map fields) and Proto2 optional
scalars will always be serialized. Singular message fields, oneof fields
and Proto3 optional scalars are not affected by this option.
including_default_value_without_presence_fields: If True, fields without
always_print_fields_with_no_presence: If True, fields without
presence (implicit presence scalars, repeated fields, and map fields) will
always be serialized. Any field that supports presence is not affected by
this option (including singular message fields and oneof fields).
@ -166,7 +166,7 @@ def MessageToDict(
use_integers_for_enums,
descriptor_pool,
float_precision,
including_default_value_without_presence_fields,
always_print_fields_with_no_presence,
)
# pylint: disable=protected-access
return printer._MessageToJsonObject(message)
@ -190,11 +190,11 @@ class _Printer(object):
use_integers_for_enums=False,
descriptor_pool=None,
float_precision=None,
including_default_value_without_presence_fields=False,
always_print_fields_with_no_presence=False,
):
self.including_default_value_fields = including_default_value_fields
self.including_default_value_without_presence_fields = (
including_default_value_without_presence_fields
self.always_print_fields_with_no_presence = (
always_print_fields_with_no_presence
)
self.preserving_proto_field_name = preserving_proto_field_name
self.use_integers_for_enums = use_integers_for_enums
@ -257,7 +257,7 @@ class _Printer(object):
# Serialize default value if including_default_value_fields is True.
if (
self.including_default_value_fields
or self.including_default_value_without_presence_fields
or self.always_print_fields_with_no_presence
):
message_descriptor = message.DESCRIPTOR
for field in message_descriptor.fields:
@ -274,10 +274,10 @@ class _Printer(object):
):
continue
# including_default_value_without_presence_fields doesn't apply to
# always_print_fields_with_no_presence doesn't apply to
# any field which supports presence.
if (
self.including_default_value_without_presence_fields
self.always_print_fields_with_no_presence
and field.has_presence
):
continue

@ -435,7 +435,7 @@ absl::Status WriteField(JsonWriter& writer, const Msg<Traits>& msg,
} else if (Traits::GetSize(field, msg) == 0) {
// We can only get here if one of the always_print options is true.
ABSL_DCHECK(writer.options().always_print_primitive_fields ||
writer.options().always_print_without_presence_fields);
writer.options().always_print_fields_with_no_presence);
if (Traits::FieldType(field) == FieldDescriptor::TYPE_GROUP) {
// We do not yet have full group support, but this is required so that we
@ -465,7 +465,7 @@ absl::Status WriteFields(JsonWriter& writer, const Msg<Traits>& msg,
Traits::FieldType(field) == FieldDescriptor::TYPE_MESSAGE;
has |= !is_singular_message && !Traits::IsOneof(field);
}
if (writer.options().always_print_without_presence_fields) {
if (writer.options().always_print_fields_with_no_presence) {
has |= Traits::IsRepeated(field) || Traits::IsImplicitPresence(field);
}

@ -43,14 +43,14 @@ struct WriterOptions {
// - Proto2 optional and required scalar fields which are not present (but not
// Proto3 optional scalar fields).
// Note: This option is deprecated in favor of
// always_print_without_presence_fields which treats proto2 and proto3
// always_print_fields_with_no_presence which treats proto2 and proto3
// optionals the same and will be removed in an upcoming release.
bool always_print_primitive_fields = false;
// Whether to always print fields which do not support presence if they would
// otherwise be omitted, namely:
// - Implicit presence fields set to their 0 value
// - Empty lists and maps
bool always_print_without_presence_fields = false;
bool always_print_fields_with_no_presence = false;
// Whether to always print enums as ints. By default they are rendered as
// strings.
bool always_print_enums_as_ints = false;

@ -34,8 +34,8 @@ absl::Status BinaryToJsonStream(google::protobuf::util::TypeResolver* resolver,
opts.preserve_proto_field_names = options.preserve_proto_field_names;
opts.always_print_enums_as_ints = options.always_print_enums_as_ints;
opts.always_print_primitive_fields = options.always_print_primitive_fields;
opts.always_print_without_presence_fields =
options.always_print_without_presence_fields;
opts.always_print_fields_with_no_presence =
options.always_print_fields_with_no_presence;
opts.unquote_int64_if_possible = options.unquote_int64_if_possible;
// TODO: Drop this setting.
@ -90,8 +90,8 @@ absl::Status MessageToJsonString(const Message& message, std::string* output,
opts.preserve_proto_field_names = options.preserve_proto_field_names;
opts.always_print_enums_as_ints = options.always_print_enums_as_ints;
opts.always_print_primitive_fields = options.always_print_primitive_fields;
opts.always_print_without_presence_fields =
options.always_print_without_presence_fields;
opts.always_print_fields_with_no_presence =
options.always_print_fields_with_no_presence;
opts.unquote_int64_if_possible = options.unquote_int64_if_possible;
// TODO: Drop this setting.

@ -46,14 +46,14 @@ struct PrintOptions {
// - Proto2 optional and required scalar fields which are not present (but not
// Proto3 optional scalar fields).
// Note: This option is deprecated in favor of
// always_print_without_presence_fields which treats proto2 and proto3
// always_print_fields_with_no_presence which treats proto2 and proto3
// optionals the same and will be removed in an upcoming release.
bool always_print_primitive_fields = false;
// Whether to always print fields which do not support presence if they would
// otherwise be omitted, namely:
// - Implicit presence fields set to their 0 value
// - Empty lists and maps
bool always_print_without_presence_fields = false;
bool always_print_fields_with_no_presence = false;
// Whether to always print enums as ints. By default they are rendered as
// strings.
bool always_print_enums_as_ints = false;

@ -278,7 +278,7 @@ TEST_P(JsonTest, TestAlwaysPrintWithoutPresenceFields) {
EXPECT_THAT(ToJson(m), IsOkAndHolds("{}"));
PrintOptions options;
options.always_print_without_presence_fields = true;
options.always_print_fields_with_no_presence = true;
EXPECT_THAT(ToJson(m, options), IsOkAndHolds(R"({"boolValue":false,)"
R"("int32Value":0,)"
R"("int64Value":"0",)"

Loading…
Cancel
Save