@@ -457,6 +457,46 @@ def _normalize_print_fields_without_presence(
457457 or including_default_value_fields
458458 )
459459
460+ @staticmethod
461+ def _to_map (
462+ cls ,
463+ map_fn ,
464+ instance ,
465+ * ,
466+ including_default_value_fields = None ,
467+ float_precision = None ,
468+ always_print_fields_with_no_presence = None ,
469+ ** kwargs ,
470+ ):
471+ """
472+ Helper for logic for to_dict and to_json
473+ """
474+ print_fields = cls ._normalize_print_fields_without_presence (
475+ always_print_fields_with_no_presence , including_default_value_fields
476+ )
477+
478+ # The `including_default_value_fields` argument was removed from protobuf 5.x
479+ # and replaced with `always_print_fields_with_no_presence` which very similar but has
480+ # handles optional fields consistently by not affecting them.
481+ # The old flag accidentally had inconsistent behavior between proto2
482+ # optional and proto3 optional fields.
483+ if PROTOBUF_VERSION [0 ] in ("3" , "4" ):
484+ kwargs ["including_default_value_fields" ] = print_fields
485+ else :
486+ kwargs ["always_print_fields_with_no_presence" ] = print_fields
487+
488+ if float_precision :
489+ # float_precision removed in protobuf 7
490+ if int (PROTOBUF_VERSION [0 ]) < 7 :
491+ kwargs ["float_precision" ] = float_precision
492+ else :
493+ warnings .warn (
494+ "The argument `float_precision` has been removed from Protobuf 7.x." ,
495+ DeprecationWarning ,
496+ )
497+
498+ return map_fn (cls .pb (instance ), ** kwargs )
499+
460500 def to_json (
461501 cls ,
462502 instance ,
@@ -491,7 +531,7 @@ def to_json(
491531 An indent level of 0 or negative will only insert newlines.
492532 Pass None for the most compact representation without newlines.
493533 float_precision (Optional(int)): If set, use this to specify float field valid digits.
494- Default is None.
534+ Default is None. [DEPRECATED] float_precision was removed in Protobuf 7.x.
495535 always_print_fields_with_no_presence (Optional(bool)): If True, fields without
496536 presence (implicit presence scalars, repeated fields, and map fields) will
497537 always be serialized. Any field that supports presence is not affected by
@@ -501,36 +541,7 @@ def to_json(
501541 Returns:
502542 str: The json string representation of the protocol buffer.
503543 """
504-
505- print_fields = cls ._normalize_print_fields_without_presence (
506- always_print_fields_with_no_presence , including_default_value_fields
507- )
508-
509- if PROTOBUF_VERSION [0 ] in ("3" , "4" ):
510- return MessageToJson (
511- cls .pb (instance ),
512- use_integers_for_enums = use_integers_for_enums ,
513- including_default_value_fields = print_fields ,
514- preserving_proto_field_name = preserving_proto_field_name ,
515- sort_keys = sort_keys ,
516- indent = indent ,
517- float_precision = float_precision ,
518- )
519- else :
520- # The `including_default_value_fields` argument was removed from protobuf 5.x
521- # and replaced with `always_print_fields_with_no_presence` which very similar but has
522- # handles optional fields consistently by not affecting them.
523- # The old flag accidentally had inconsistent behavior between proto2
524- # optional and proto3 optional fields.
525- return MessageToJson (
526- cls .pb (instance ),
527- use_integers_for_enums = use_integers_for_enums ,
528- always_print_fields_with_no_presence = print_fields ,
529- preserving_proto_field_name = preserving_proto_field_name ,
530- sort_keys = sort_keys ,
531- indent = indent ,
532- float_precision = float_precision ,
533- )
544+ return cls ._to_map (map_fn = MessageToJson , ** locals ())
534545
535546 def from_json (cls , payload , * , ignore_unknown_fields = False ) -> "Message" :
536547 """Given a json string representing an instance,
@@ -576,9 +587,7 @@ def to_dict(
576587 This value must match `always_print_fields_with_no_presence`,
577588 if both arguments are explicitly set.
578589 float_precision (Optional(int)): If set, use this to specify float field valid digits.
579- Default is None.
580- [DEPRECATED] float_precision was removed in Protobuf 7.x, and will be ignored
581- in those versions
590+ Default is None. [DEPRECATED] float_precision was removed in Protobuf 7.x.
582591 always_print_fields_with_no_presence (Optional(bool)): If True, fields without
583592 presence (implicit presence scalars, repeated fields, and map fields) will
584593 always be serialized. Any field that supports presence is not affected by
@@ -590,37 +599,7 @@ def to_dict(
590599 Messages and map fields are represented as dicts,
591600 repeated fields are represented as lists.
592601 """
593-
594- print_fields = cls ._normalize_print_fields_without_presence (
595- always_print_fields_with_no_presence , including_default_value_fields
596- )
597- kwargs = {
598- "preserving_proto_field_name" :preserving_proto_field_name ,
599- "use_integers_for_enums" :use_integers_for_enums ,
600- "float_precision" : float_precision ,
601- }
602-
603- # The `including_default_value_fields` argument was removed from protobuf 5.x
604- # and replaced with `always_print_fields_with_no_presence` which very similar but has
605- # handles optional fields consistently by not affecting them.
606- # The old flag accidentally had inconsistent behavior between proto2
607- # optional and proto3 optional fields.
608- if PROTOBUF_VERSION [0 ] in ("3" , "4" ):
609- kwargs ["including_default_value_fields" ] = print_fields
610- del kwargs [always_print_fields_with_no_presence ]
611- else :
612- kwargs ["always_print_fields_with_no_presence" ] = print_fields
613- del kwargs ["including_default_value_fields]"
614-
615- # float_precision removed in protobuf 7
616- if int (PROTOBUF_VERSION [0 ]) > 7 and float_precision is not None :
617- warnings .warn (
618- "The argument `float_precision` has been removed from Protobuf 7.x." ,
619- DeprecationWarning ,
620- )
621- del kwargs ["float_precision" ]
622-
623- return MessageToDict (cls .pb (instance ), ** kwargs )
602+ return cls ._to_map (map_fn = MessageToDict , ** locals ())
624603
625604 def copy_from (cls , instance , other ):
626605 """Equivalent for protobuf.Message.CopyFrom
0 commit comments