From 5a6d03d3e36eda645558f2db34f39d4377c7af52 Mon Sep 17 00:00:00 2001 From: vam-google Date: Fri, 26 Aug 2022 17:09:07 -0700 Subject: [PATCH 1/3] fix: Add no-pretty perint option This fixes https://github.com/googleapis/proto-plus-python/issues/284 and in general makes sense. --- proto/message.py | 4 ++++ tests/test_json.py | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/proto/message.py b/proto/message.py index 6726988a..8f42f6be 100644 --- a/proto/message.py +++ b/proto/message.py @@ -376,6 +376,7 @@ def to_json( use_integers_for_enums=True, including_default_value_fields=True, preserving_proto_field_name=False, + indent=2, ) -> str: """Given a message instance, serialize it to json @@ -388,6 +389,8 @@ def to_json( preserving_proto_field_name (Optional(bool)): An option that determines whether field name representations preserve proto case (snake_case) or use lowerCamelCase. Default is False. + indent: The JSON object will be pretty-printed with this indent level. + An indent level of 0 or negative will only insert newlines. Returns: str: The json string representation of the protocol buffer. @@ -397,6 +400,7 @@ def to_json( use_integers_for_enums=use_integers_for_enums, including_default_value_fields=including_default_value_fields, preserving_proto_field_name=preserving_proto_field_name, + indent=indent ) def from_json(cls, payload, *, ignore_unknown_fields=False) -> "Message": diff --git a/tests/test_json.py b/tests/test_json.py index 8faa96d4..31822e92 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -28,6 +28,15 @@ class Squid(proto.Message): assert json == '{"massKg":100}' +def test_message_to_json_no_indent(): + class Squid(proto.Message): + mass_kg = proto.Field(proto.INT32, number=1) + + s = Squid(mass_kg=100) + json = Squid.to_json(s, indent=None) + assert json == '{"massKg": 100}' + + def test_message_from_json(): class Squid(proto.Message): mass_kg = proto.Field(proto.INT32, number=1) From a453c5b3640b51298d02a8feb39e8e5f93006494 Mon Sep 17 00:00:00 2001 From: vam-google Date: Fri, 26 Aug 2022 17:11:15 -0700 Subject: [PATCH 2/3] reformat with black --- proto/message.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proto/message.py b/proto/message.py index 8f42f6be..827f3a34 100644 --- a/proto/message.py +++ b/proto/message.py @@ -400,7 +400,7 @@ def to_json( use_integers_for_enums=use_integers_for_enums, including_default_value_fields=including_default_value_fields, preserving_proto_field_name=preserving_proto_field_name, - indent=indent + indent=indent, ) def from_json(cls, payload, *, ignore_unknown_fields=False) -> "Message": From 834c1d920f964fd1d541c9a5149adc7987592cc9 Mon Sep 17 00:00:00 2001 From: vam-google Date: Mon, 29 Aug 2022 09:44:48 -0700 Subject: [PATCH 3/3] improve documentation about indent=None meaning no newlines. --- proto/message.py | 1 + tests/test_json.py | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/proto/message.py b/proto/message.py index 827f3a34..253e6240 100644 --- a/proto/message.py +++ b/proto/message.py @@ -391,6 +391,7 @@ def to_json( proto case (snake_case) or use lowerCamelCase. Default is False. indent: The JSON object will be pretty-printed with this indent level. An indent level of 0 or negative will only insert newlines. + Pass None for the most compact representation without newlines. Returns: str: The json string representation of the protocol buffer. diff --git a/tests/test_json.py b/tests/test_json.py index 31822e92..93ca936c 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -31,10 +31,11 @@ class Squid(proto.Message): def test_message_to_json_no_indent(): class Squid(proto.Message): mass_kg = proto.Field(proto.INT32, number=1) + name = proto.Field(proto.STRING, number=2) - s = Squid(mass_kg=100) + s = Squid(mass_kg=100, name="no_new_lines_squid") json = Squid.to_json(s, indent=None) - assert json == '{"massKg": 100}' + assert json == '{"massKg": 100, "name": "no_new_lines_squid"}' def test_message_from_json():