@@ -86,6 +86,8 @@ def MessageToJson(
8686 descriptor_pool = None ,
8787 ensure_ascii = True ,
8888 always_print_fields_with_no_presence = False ,
89+ * ,
90+ unquote_int64_if_possible = False ,
8991):
9092 """Converts protobuf message to JSON format.
9193
@@ -107,6 +109,9 @@ def MessageToJson(
107109 default.
108110 ensure_ascii: If True, strings with non-ASCII characters are escaped. If
109111 False, Unicode strings are returned unchanged.
112+ unquote_int64_if_possible: If True, unquote int64 fields for values that
113+ are safe to emit as numbers (all values smaller than 2^53 and a sparse
114+ set of values that are larger).
110115
111116 Returns:
112117 A string containing the JSON formatted protocol buffer message.
@@ -116,6 +121,7 @@ def MessageToJson(
116121 use_integers_for_enums ,
117122 descriptor_pool ,
118123 always_print_fields_with_no_presence ,
124+ unquote_int64_if_possible = unquote_int64_if_possible ,
119125 )
120126 return printer .ToJsonString (message , indent , sort_keys , ensure_ascii )
121127
@@ -126,6 +132,8 @@ def MessageToDict(
126132 preserving_proto_field_name = False ,
127133 use_integers_for_enums = False ,
128134 descriptor_pool = None ,
135+ * ,
136+ unquote_int64_if_possible = False ,
129137):
130138 """Converts protobuf message to a dictionary.
131139
@@ -143,6 +151,9 @@ def MessageToDict(
143151 use_integers_for_enums: If true, print integers instead of enum names.
144152 descriptor_pool: A Descriptor Pool for resolving types. If None use the
145153 default.
154+ unquote_int64_if_possible: If True, unquote int64 fields for values that
155+ are safe to emit as numbers (all values smaller than 2^53 and a sparse
156+ set of values that are larger).
146157
147158 Returns:
148159 A dict representation of the protocol buffer message.
@@ -152,6 +163,7 @@ def MessageToDict(
152163 use_integers_for_enums ,
153164 descriptor_pool ,
154165 always_print_fields_with_no_presence ,
166+ unquote_int64_if_possible = unquote_int64_if_possible ,
155167 )
156168 # pylint: disable=protected-access
157169 return printer ._MessageToJsonObject (message )
@@ -174,13 +186,16 @@ def __init__(
174186 use_integers_for_enums = False ,
175187 descriptor_pool = None ,
176188 always_print_fields_with_no_presence = False ,
189+ * ,
190+ unquote_int64_if_possible = False ,
177191 ):
178192 self .always_print_fields_with_no_presence = (
179193 always_print_fields_with_no_presence
180194 )
181195 self .preserving_proto_field_name = preserving_proto_field_name
182196 self .use_integers_for_enums = use_integers_for_enums
183197 self .descriptor_pool = descriptor_pool
198+ self .unquote_int64_if_possible = unquote_int64_if_possible
184199
185200 def ToJsonString (self , message , indent , sort_keys , ensure_ascii ):
186201 js = self ._MessageToJsonObject (message )
@@ -294,7 +309,10 @@ def _FieldToJsonObject(self, field, value):
294309 elif field .cpp_type == descriptor .FieldDescriptor .CPPTYPE_BOOL :
295310 return bool (value )
296311 elif field .cpp_type in _INT64_TYPES :
297- return str (value )
312+ if self .unquote_int64_if_possible and float (value ) == value :
313+ return value
314+ else :
315+ return str (value )
298316 elif field .cpp_type in _FLOAT_TYPES :
299317 if math .isinf (value ):
300318 if value < 0.0 :
0 commit comments