Skip to content

Commit 727a8ed

Browse files
Mark Py JSON float_precision deprecated.
This option does not exist on any of our other ProtoJSON serializers, and we have no plan to add it there, for two reasons: 1) We have found a swiss army knife of options is problematic for correctness and security and intend to support only the absolutely critical options on our JSON implementations. 2) We intend ProtoJSON to be focused on being a format for sending to clients that will perform either a parse via Protobuf library or a JSON.parse(). Human readability is considered a secondary concern, and so behavior/options that prioritizes human readbility are not prioritized. float_precision is mainly a human-readability topic (in theory it could also shrink JSON payload size, but where payload size is a concern we also would strongly recommend binary). We have no plan or timeline to remove this option, this only eagerly documents it as deprecated to make it clear that we may remove it eventually and that it isn't precedent / expected to be ported to other languages. PiperOrigin-RevId: 750259987
1 parent 893cc57 commit 727a8ed

File tree

2 files changed

+60
-43
lines changed

2 files changed

+60
-43
lines changed

python/google/protobuf/json_format.py

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class ParseError(Error):
7272

7373
class EnumStringValueParseError(ParseError):
7474
"""Thrown if unknown string enum value is encountered.
75+
7576
This exception is suppressed if ignore_unknown_fields is set.
7677
"""
7778

@@ -91,10 +92,10 @@ def MessageToJson(
9192
9293
Args:
9394
message: The protocol buffers message instance to serialize.
94-
always_print_fields_with_no_presence: If True, fields without
95-
presence (implicit presence scalars, repeated fields, and map fields) will
96-
always be serialized. Any field that supports presence is not affected by
97-
this option (including singular message fields and oneof fields).
95+
always_print_fields_with_no_presence: If True, fields without presence
96+
(implicit presence scalars, repeated fields, and map fields) will always
97+
be serialized. Any field that supports presence is not affected by this
98+
option (including singular message fields and oneof fields).
9899
preserving_proto_field_name: If True, use the original proto field names as
99100
defined in the .proto file. If False, convert the field names to
100101
lowerCamelCase.
@@ -105,7 +106,8 @@ def MessageToJson(
105106
use_integers_for_enums: If true, print integers instead of enum names.
106107
descriptor_pool: A Descriptor Pool for resolving types. If None use the
107108
default.
108-
float_precision: If set, use this to specify float field valid digits.
109+
float_precision: Deprecated. If set, use this to specify float field valid
110+
digits.
109111
ensure_ascii: If True, strings with non-ASCII characters are escaped. If
110112
False, Unicode strings are returned unchanged.
111113
@@ -117,7 +119,7 @@ def MessageToJson(
117119
use_integers_for_enums,
118120
descriptor_pool,
119121
float_precision,
120-
always_print_fields_with_no_presence
122+
always_print_fields_with_no_presence,
121123
)
122124
return printer.ToJsonString(message, indent, sort_keys, ensure_ascii)
123125

@@ -136,17 +138,18 @@ def MessageToDict(
136138
137139
Args:
138140
message: The protocol buffers message instance to serialize.
139-
always_print_fields_with_no_presence: If True, fields without
140-
presence (implicit presence scalars, repeated fields, and map fields) will
141-
always be serialized. Any field that supports presence is not affected by
142-
this option (including singular message fields and oneof fields).
141+
always_print_fields_with_no_presence: If True, fields without presence
142+
(implicit presence scalars, repeated fields, and map fields) will always
143+
be serialized. Any field that supports presence is not affected by this
144+
option (including singular message fields and oneof fields).
143145
preserving_proto_field_name: If True, use the original proto field names as
144146
defined in the .proto file. If False, convert the field names to
145147
lowerCamelCase.
146148
use_integers_for_enums: If true, print integers instead of enum names.
147149
descriptor_pool: A Descriptor Pool for resolving types. If None use the
148150
default.
149-
float_precision: If set, use this to specify float field valid digits.
151+
float_precision: Deprecated. If set, use this to specify float field valid
152+
digits.
150153
151154
Returns:
152155
A dict representation of the protocol buffer message.
@@ -251,10 +254,7 @@ def _RegularMessageToJsonObject(self, message, js):
251254

252255
# always_print_fields_with_no_presence doesn't apply to
253256
# any field which supports presence.
254-
if (
255-
self.always_print_fields_with_no_presence
256-
and field.has_presence
257-
):
257+
if self.always_print_fields_with_no_presence and field.has_presence:
258258
continue
259259

260260
if self.preserving_proto_field_name:
@@ -674,7 +674,8 @@ def _ConvertFieldValuePair(self, js, message, path):
674674
)
675675
)
676676
self._ConvertAndAppendScalar(
677-
message, field, item, '{0}.{1}[{2}]'.format(path, name, index))
677+
message, field, item, '{0}.{1}[{2}]'.format(path, name, index)
678+
)
678679
elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
679680
if field.is_extension:
680681
sub_message = message.Extensions[field]
@@ -684,9 +685,13 @@ def _ConvertFieldValuePair(self, js, message, path):
684685
self.ConvertMessage(value, sub_message, '{0}.{1}'.format(path, name))
685686
else:
686687
if field.is_extension:
687-
self._ConvertAndSetScalarExtension(message, field, value, '{0}.{1}'.format(path, name))
688+
self._ConvertAndSetScalarExtension(
689+
message, field, value, '{0}.{1}'.format(path, name)
690+
)
688691
else:
689-
self._ConvertAndSetScalar(message, field, value, '{0}.{1}'.format(path, name))
692+
self._ConvertAndSetScalar(
693+
message, field, value, '{0}.{1}'.format(path, name)
694+
)
690695
except ParseError as e:
691696
if field and field.containing_oneof is None:
692697
raise ParseError(
@@ -801,7 +806,9 @@ def _ConvertStructMessage(self, value, message, path):
801806
def _ConvertWrapperMessage(self, value, message, path):
802807
"""Convert a JSON representation into Wrapper message."""
803808
field = message.DESCRIPTOR.fields_by_name['value']
804-
self._ConvertAndSetScalar(message, field, value, path='{0}.value'.format(path))
809+
self._ConvertAndSetScalar(
810+
message, field, value, path='{0}.value'.format(path)
811+
)
805812

806813
def _ConvertMapFieldValue(self, value, message, field, path):
807814
"""Convert map field value for a message map field.
@@ -839,13 +846,17 @@ def _ConvertMapFieldValue(self, value, message, field, path):
839846
field,
840847
key_value,
841848
value[key],
842-
path='{0}[{1}]'.format(path, key_value))
849+
path='{0}[{1}]'.format(path, key_value),
850+
)
843851

844-
def _ConvertAndSetScalarExtension(self, message, extension_field, js_value, path):
852+
def _ConvertAndSetScalarExtension(
853+
self, message, extension_field, js_value, path
854+
):
845855
"""Convert scalar from js_value and assign it to message.Extensions[extension_field]."""
846856
try:
847857
message.Extensions[extension_field] = _ConvertScalarFieldValue(
848-
js_value, extension_field, path)
858+
js_value, extension_field, path
859+
)
849860
except EnumStringValueParseError:
850861
if not self.ignore_unknown_fields:
851862
raise
@@ -854,9 +865,8 @@ def _ConvertAndSetScalar(self, message, field, js_value, path):
854865
"""Convert scalar from js_value and assign it to message.field."""
855866
try:
856867
setattr(
857-
message,
858-
field.name,
859-
_ConvertScalarFieldValue(js_value, field, path))
868+
message, field.name, _ConvertScalarFieldValue(js_value, field, path)
869+
)
860870
except EnumStringValueParseError:
861871
if not self.ignore_unknown_fields:
862872
raise
@@ -865,16 +875,23 @@ def _ConvertAndAppendScalar(self, message, repeated_field, js_value, path):
865875
"""Convert scalar from js_value and append it to message.repeated_field."""
866876
try:
867877
getattr(message, repeated_field.name).append(
868-
_ConvertScalarFieldValue(js_value, repeated_field, path))
878+
_ConvertScalarFieldValue(js_value, repeated_field, path)
879+
)
869880
except EnumStringValueParseError:
870881
if not self.ignore_unknown_fields:
871882
raise
872883

873-
def _ConvertAndSetScalarToMapKey(self, message, map_field, converted_key, js_value, path):
884+
def _ConvertAndSetScalarToMapKey(
885+
self, message, map_field, converted_key, js_value, path
886+
):
874887
"""Convert scalar from 'js_value' and add it to message.map_field[converted_key]."""
875888
try:
876-
getattr(message, map_field.name)[converted_key] = _ConvertScalarFieldValue(
877-
js_value, map_field.message_type.fields_by_name['value'], path,
889+
getattr(message, map_field.name)[converted_key] = (
890+
_ConvertScalarFieldValue(
891+
js_value,
892+
map_field.message_type.fields_by_name['value'],
893+
path,
894+
)
878895
)
879896
except EnumStringValueParseError:
880897
if not self.ignore_unknown_fields:

python/google/protobuf/text_format.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ def MessageToString(
126126
will be printed at the end of the message and their relative order is
127127
determined by the extension number. By default, use the field number
128128
order.
129-
float_format (str): If set, use this to specify float field formatting
130-
(per the "Format Specification Mini-Language"); otherwise, shortest float
131-
that has same value in wire will be printed. Also affect double field
132-
if double_format is not set but float_format is set.
133-
double_format (str): If set, use this to specify double field formatting
134-
(per the "Format Specification Mini-Language"); if it is not set but
135-
float_format is set, use float_format. Otherwise, use ``str()``
129+
float_format (str): Deprecated. If set, use this to specify float field
130+
formatting (per the "Format Specification Mini-Language"); otherwise,
131+
shortest float that has same value in wire will be printed. Also affect
132+
double field if double_format is not set but float_format is set.
133+
double_format (str): Deprecated. If set, use this to specify double field
134+
formatting (per the "Format Specification Mini-Language"); if it is not
135+
set but float_format is set, use float_format. Otherwise, use ``str()``
136136
use_field_number: If True, print field numbers instead of names.
137137
descriptor_pool (DescriptorPool): Descriptor pool used to resolve Any types.
138138
indent (int): The initial indent level, in terms of spaces, for pretty
@@ -391,13 +391,13 @@ def __init__(
391391
use_index_order: If True, print fields of a proto message using the order
392392
defined in source code instead of the field number. By default, use the
393393
field number order.
394-
float_format: If set, use this to specify float field formatting
395-
(per the "Format Specification Mini-Language"); otherwise, shortest
396-
float that has same value in wire will be printed. Also affect double
397-
field if double_format is not set but float_format is set.
398-
double_format: If set, use this to specify double field formatting
399-
(per the "Format Specification Mini-Language"); if it is not set but
400-
float_format is set, use float_format. Otherwise, str() is used.
394+
float_format: Deprecated. If set, use this to specify float field
395+
formatting (per the "Format Specification Mini-Language"); otherwise,
396+
shortest float that has same value in wire will be printed. Also affect
397+
double field if double_format is not set but float_format is set.
398+
double_format: Deprecated. If set, use this to specify double field
399+
formatting (per the "Format Specification Mini-Language"); if it is not
400+
set but float_format is set, use float_format. Otherwise, str() is used.
401401
use_field_number: If True, print field numbers instead of names.
402402
descriptor_pool: A DescriptorPool used to resolve Any types.
403403
message_formatter: A function(message, indent, as_one_line): unicode|None

0 commit comments

Comments
 (0)