Skip to content

Commit fc41e78

Browse files
protobuf-github-botzhangskz
authored andcommitted
Rename the 'includingDefaultValueWithoutPresenceFields' and 'always_print_without_presence_fields' to 'alwaysPrintFieldsWithNoPresence' in the Cpp, Py and Java JSON serializers for consistency.
PiperOrigin-RevId: 604292220
1 parent bc99ca2 commit fc41e78

File tree

9 files changed

+40
-44
lines changed

9 files changed

+40
-44
lines changed

java/util/src/main/java/com/google/protobuf/util/JsonFormat.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public Printer usingTypeRegistry(com.google.protobuf.TypeRegistry registry) {
187187
*
188188
* <p>The new Printer clones all other configurations from the current {@link Printer}.
189189
*
190-
* @deprecated Prefer {@link #includingDefaultValueWithoutPresenceFields}
190+
* @deprecated Prefer {@link #alwaysPrintFieldsWithNoPresence}
191191
*/
192192
@Deprecated
193193
public Printer includingDefaultValueFields() {
@@ -241,10 +241,9 @@ public Printer includingDefaultValueFields(Set<FieldDescriptor> fieldsToAlwaysOu
241241
* presence scalars set to their default value). The new Printer clones all other configurations
242242
* from the current {@link Printer}.
243243
*/
244-
public Printer includingDefaultValueWithoutPresenceFields() {
244+
public Printer alwaysPrintFieldsWithNoPresence() {
245245
if (shouldPrintDefaults != ShouldPrintDefaults.ONLY_IF_PRESENT) {
246-
throw new IllegalStateException(
247-
"JsonFormat includingDefaultValueFields has already been set.");
246+
throw new IllegalStateException("Only one of the JsonFormat defaults options can be set.");
248247
}
249248
return new Printer(
250249
registry,

java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,11 +1527,11 @@ public void testDefaultValueOptionsProto3() throws Exception {
15271527
+ " \"repeatedRecursive\": []\n"
15281528
+ "}";
15291529

1530-
// includingDefaultValueFields() and includingDefaultValueWithoutPresenceFields() should
1530+
// includingDefaultValueFields() and alwaysPrintFieldsWithNoPresence() should
15311531
// behave identically on the proto3 test message:
15321532
assertThat(JsonFormat.printer().includingDefaultValueFields().print(message))
15331533
.isEqualTo(expectedJsonWithDefaults);
1534-
assertThat(JsonFormat.printer().includingDefaultValueWithoutPresenceFields().print(message))
1534+
assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(message))
15351535
.isEqualTo(expectedJsonWithDefaults);
15361536
}
15371537

@@ -1742,16 +1742,14 @@ public void testDefaultValueOptionsProto3Oneofs() throws Exception {
17421742
assertThat(JsonFormat.printer().print(oneofMessage)).isEqualTo("{\n}");
17431743
assertThat(JsonFormat.printer().includingDefaultValueFields().print(oneofMessage))
17441744
.isEqualTo("{\n}");
1745-
assertThat(
1746-
JsonFormat.printer().includingDefaultValueWithoutPresenceFields().print(oneofMessage))
1745+
assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(oneofMessage))
17471746
.isEqualTo("{\n}");
17481747

17491748
oneofMessage = TestOneof.newBuilder().setOneofInt32(42).build();
17501749
assertThat(JsonFormat.printer().print(oneofMessage)).isEqualTo("{\n \"oneofInt32\": 42\n}");
17511750
assertThat(JsonFormat.printer().includingDefaultValueFields().print(oneofMessage))
17521751
.isEqualTo("{\n \"oneofInt32\": 42\n}");
1753-
assertThat(
1754-
JsonFormat.printer().includingDefaultValueWithoutPresenceFields().print(oneofMessage))
1752+
assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(oneofMessage))
17551753
.isEqualTo("{\n \"oneofInt32\": 42\n}");
17561754

17571755
TestOneof.Builder oneofBuilder = TestOneof.newBuilder();
@@ -1761,16 +1759,15 @@ public void testDefaultValueOptionsProto3Oneofs() throws Exception {
17611759
.isEqualTo("{\n \"oneofNullValue\": null\n}");
17621760
assertThat(JsonFormat.printer().includingDefaultValueFields().print(oneofMessage))
17631761
.isEqualTo("{\n \"oneofNullValue\": null\n}");
1764-
assertThat(
1765-
JsonFormat.printer().includingDefaultValueWithoutPresenceFields().print(oneofMessage))
1762+
assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(oneofMessage))
17661763
.isEqualTo("{\n \"oneofNullValue\": null\n}");
17671764
}
17681765

17691766
@Test
17701767
public void testIncludingDefaultValueOptionsWithProto2Optional() throws Exception {
17711768
TestAllTypesProto2 message = TestAllTypesProto2.getDefaultInstance();
17721769
assertThat(JsonFormat.printer().print(message)).isEqualTo("{\n}");
1773-
// includingDefaultValueFields() and includingDefaultValueWithoutPresenceFields()
1770+
// includingDefaultValueFields() and alwaysPrintFieldsWithNoPresence()
17741771
// behave differently on a proto2 message: the former includes the proto2 explicit presence
17751772
// fields and the latter does not.
17761773
assertThat(JsonFormat.printer().includingDefaultValueFields().print(message))
@@ -1812,7 +1809,7 @@ public void testIncludingDefaultValueOptionsWithProto2Optional() throws Exceptio
18121809
+ " \"optionalAliasedEnum\": \"ALIAS_FOO\",\n"
18131810
+ " \"repeatedRecursive\": []\n"
18141811
+ "}");
1815-
assertThat(JsonFormat.printer().includingDefaultValueWithoutPresenceFields().print(message))
1812+
assertThat(JsonFormat.printer().alwaysPrintFieldsWithNoPresence().print(message))
18161813
.isEqualTo(
18171814
"{\n"
18181815
+ " \"repeatedInt32\": [],\n"

python/google/protobuf/internal/json_format_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ def testProto3Optional_IncludingDefaultValueWithoutPresenceFields(self):
330330
self.assertEqual(
331331
json.loads(
332332
json_format.MessageToJson(
333-
message, including_default_value_without_presence_fields=True
333+
message, always_print_fields_with_no_presence=True
334334
)
335335
),
336336
json.loads('{"repeatedInt32": [], "repeatedNestedMessage": []}'),
@@ -339,7 +339,7 @@ def testProto3Optional_IncludingDefaultValueWithoutPresenceFields(self):
339339
self.assertEqual(
340340
json.loads(
341341
json_format.MessageToJson(
342-
message, including_default_value_without_presence_fields=True
342+
message, always_print_fields_with_no_presence=True
343343
)
344344
),
345345
json.loads(
@@ -391,7 +391,7 @@ def testProto2_IncludingDefaultValueWithoutPresenceFields(self):
391391
self.assertEqual(
392392
json.loads(
393393
json_format.MessageToJson(
394-
message, including_default_value_without_presence_fields=True
394+
message, always_print_fields_with_no_presence=True
395395
)
396396
),
397397
json.loads('{"repeatedInt32": [], "repeatedNestedMessage": []}'),
@@ -400,7 +400,7 @@ def testProto2_IncludingDefaultValueWithoutPresenceFields(self):
400400
self.assertEqual(
401401
json.loads(
402402
json_format.MessageToJson(
403-
message, including_default_value_without_presence_fields=True
403+
message, always_print_fields_with_no_presence=True
404404
)
405405
),
406406
json.loads(

python/google/protobuf/json_format.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,19 @@ def MessageToJson(
8080
descriptor_pool=None,
8181
float_precision=None,
8282
ensure_ascii=True,
83-
including_default_value_without_presence_fields=False,
83+
always_print_fields_with_no_presence=False,
8484
):
8585
"""Converts protobuf message to JSON format.
8686
8787
Args:
8888
message: The protocol buffers message instance to serialize.
8989
including_default_value_fields: (DEPRECATED: use
90-
including_default_value_without_presence_fields to correctly treats proto2
91-
and proto3 optional the same). If True, fields without presence (implicit
90+
always_print_fields_with_no_presence which correctly treats proto2
91+
and proto3 optionals the same). If True, fields without presence (implicit
9292
presence scalars, repeated fields, and map fields) and Proto2 optional
9393
scalars will always be serialized. Singular message fields, oneof fields
9494
and Proto3 optional scalars are not affected by this option.
95-
including_default_value_without_presence_fields: If True, fields without
95+
always_print_fields_with_no_presence: If True, fields without
9696
presence (implicit presence scalars, repeated fields, and map fields) will
9797
always be serialized. Any field that supports presence is not affected by
9898
this option (including singular message fields and oneof fields).
@@ -119,15 +119,15 @@ def MessageToJson(
119119
use_integers_for_enums,
120120
descriptor_pool,
121121
float_precision,
122-
including_default_value_without_presence_fields
122+
always_print_fields_with_no_presence
123123
)
124124
return printer.ToJsonString(message, indent, sort_keys, ensure_ascii)
125125

126126

127127
def MessageToDict(
128128
message,
129129
including_default_value_fields=False,
130-
including_default_value_without_presence_fields=False,
130+
always_print_fields_with_no_presence=False,
131131
preserving_proto_field_name=False,
132132
use_integers_for_enums=False,
133133
descriptor_pool=None,
@@ -140,12 +140,12 @@ def MessageToDict(
140140
Args:
141141
message: The protocol buffers message instance to serialize.
142142
including_default_value_fields: (DEPRECATED: use
143-
including_default_value_without_presence_fields to correctly treats proto2
143+
always_print_fields_with_no_presence to correctly treats proto2
144144
and proto3 optional the same). If True, fields without presence (implicit
145145
presence scalars, repeated fields, and map fields) and Proto2 optional
146146
scalars will always be serialized. Singular message fields, oneof fields
147147
and Proto3 optional scalars are not affected by this option.
148-
including_default_value_without_presence_fields: If True, fields without
148+
always_print_fields_with_no_presence: If True, fields without
149149
presence (implicit presence scalars, repeated fields, and map fields) will
150150
always be serialized. Any field that supports presence is not affected by
151151
this option (including singular message fields and oneof fields).
@@ -166,7 +166,7 @@ def MessageToDict(
166166
use_integers_for_enums,
167167
descriptor_pool,
168168
float_precision,
169-
including_default_value_without_presence_fields,
169+
always_print_fields_with_no_presence,
170170
)
171171
# pylint: disable=protected-access
172172
return printer._MessageToJsonObject(message)
@@ -190,11 +190,11 @@ def __init__(
190190
use_integers_for_enums=False,
191191
descriptor_pool=None,
192192
float_precision=None,
193-
including_default_value_without_presence_fields=False,
193+
always_print_fields_with_no_presence=False,
194194
):
195195
self.including_default_value_fields = including_default_value_fields
196-
self.including_default_value_without_presence_fields = (
197-
including_default_value_without_presence_fields
196+
self.always_print_fields_with_no_presence = (
197+
always_print_fields_with_no_presence
198198
)
199199
self.preserving_proto_field_name = preserving_proto_field_name
200200
self.use_integers_for_enums = use_integers_for_enums
@@ -257,7 +257,7 @@ def _RegularMessageToJsonObject(self, message, js):
257257
# Serialize default value if including_default_value_fields is True.
258258
if (
259259
self.including_default_value_fields
260-
or self.including_default_value_without_presence_fields
260+
or self.always_print_fields_with_no_presence
261261
):
262262
message_descriptor = message.DESCRIPTOR
263263
for field in message_descriptor.fields:
@@ -274,10 +274,10 @@ def _RegularMessageToJsonObject(self, message, js):
274274
):
275275
continue
276276

277-
# including_default_value_without_presence_fields doesn't apply to
277+
# always_print_fields_with_no_presence doesn't apply to
278278
# any field which supports presence.
279279
if (
280-
self.including_default_value_without_presence_fields
280+
self.always_print_fields_with_no_presence
281281
and field.has_presence
282282
):
283283
continue

src/google/protobuf/json/internal/unparser.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ absl::Status WriteField(JsonWriter& writer, const Msg<Traits>& msg,
435435
} else if (Traits::GetSize(field, msg) == 0) {
436436
// We can only get here if one of the always_print options is true.
437437
ABSL_DCHECK(writer.options().always_print_primitive_fields ||
438-
writer.options().always_print_without_presence_fields);
438+
writer.options().always_print_fields_with_no_presence);
439439

440440
if (Traits::FieldType(field) == FieldDescriptor::TYPE_GROUP) {
441441
// 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,
465465
Traits::FieldType(field) == FieldDescriptor::TYPE_MESSAGE;
466466
has |= !is_singular_message && !Traits::IsOneof(field);
467467
}
468-
if (writer.options().always_print_without_presence_fields) {
468+
if (writer.options().always_print_fields_with_no_presence) {
469469
has |= Traits::IsRepeated(field) || Traits::IsImplicitPresence(field);
470470
}
471471

src/google/protobuf/json/internal/writer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ struct WriterOptions {
4343
// - Proto2 optional and required scalar fields which are not present (but not
4444
// Proto3 optional scalar fields).
4545
// Note: This option is deprecated in favor of
46-
// always_print_without_presence_fields which treats proto2 and proto3
46+
// always_print_fields_with_no_presence which treats proto2 and proto3
4747
// optionals the same and will be removed in an upcoming release.
4848
bool always_print_primitive_fields = false;
4949
// Whether to always print fields which do not support presence if they would
5050
// otherwise be omitted, namely:
5151
// - Implicit presence fields set to their 0 value
5252
// - Empty lists and maps
53-
bool always_print_without_presence_fields = false;
53+
bool always_print_fields_with_no_presence = false;
5454
// Whether to always print enums as ints. By default they are rendered as
5555
// strings.
5656
bool always_print_enums_as_ints = false;

src/google/protobuf/json/json.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ absl::Status BinaryToJsonStream(google::protobuf::util::TypeResolver* resolver,
3434
opts.preserve_proto_field_names = options.preserve_proto_field_names;
3535
opts.always_print_enums_as_ints = options.always_print_enums_as_ints;
3636
opts.always_print_primitive_fields = options.always_print_primitive_fields;
37-
opts.always_print_without_presence_fields =
38-
options.always_print_without_presence_fields;
37+
opts.always_print_fields_with_no_presence =
38+
options.always_print_fields_with_no_presence;
3939
opts.unquote_int64_if_possible = options.unquote_int64_if_possible;
4040

4141
// TODO: Drop this setting.
@@ -90,8 +90,8 @@ absl::Status MessageToJsonString(const Message& message, std::string* output,
9090
opts.preserve_proto_field_names = options.preserve_proto_field_names;
9191
opts.always_print_enums_as_ints = options.always_print_enums_as_ints;
9292
opts.always_print_primitive_fields = options.always_print_primitive_fields;
93-
opts.always_print_without_presence_fields =
94-
options.always_print_without_presence_fields;
93+
opts.always_print_fields_with_no_presence =
94+
options.always_print_fields_with_no_presence;
9595
opts.unquote_int64_if_possible = options.unquote_int64_if_possible;
9696

9797
// TODO: Drop this setting.

src/google/protobuf/json/json.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ struct PrintOptions {
4646
// - Proto2 optional and required scalar fields which are not present (but not
4747
// Proto3 optional scalar fields).
4848
// Note: This option is deprecated in favor of
49-
// always_print_without_presence_fields which treats proto2 and proto3
49+
// always_print_fields_with_no_presence which treats proto2 and proto3
5050
// optionals the same and will be removed in an upcoming release.
5151
bool always_print_primitive_fields = false;
5252
// Whether to always print fields which do not support presence if they would
5353
// otherwise be omitted, namely:
5454
// - Implicit presence fields set to their 0 value
5555
// - Empty lists and maps
56-
bool always_print_without_presence_fields = false;
56+
bool always_print_fields_with_no_presence = false;
5757
// Whether to always print enums as ints. By default they are rendered as
5858
// strings.
5959
bool always_print_enums_as_ints = false;

src/google/protobuf/json/json_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ TEST_P(JsonTest, TestAlwaysPrintWithoutPresenceFields) {
278278
EXPECT_THAT(ToJson(m), IsOkAndHolds("{}"));
279279

280280
PrintOptions options;
281-
options.always_print_without_presence_fields = true;
281+
options.always_print_fields_with_no_presence = true;
282282
EXPECT_THAT(ToJson(m, options), IsOkAndHolds(R"({"boolValue":false,)"
283283
R"("int32Value":0,)"
284284
R"("int64Value":"0",)"

0 commit comments

Comments
 (0)