From 43af70de7da6aa2550fb3bab490164a85e3bbad1 Mon Sep 17 00:00:00 2001 From: Joshua Moldenhauer Date: Mon, 20 Feb 2023 12:45:03 -0600 Subject: [PATCH] Update to_json Indent Type Annotation Updated the to_json methods indent type annotation on the AWSObject, and AWSHelperFn to allow for None. Added unit tests to ensure the string representation is correct. Sending an integer, or string to 'json.dumps' indent parameter results in a pretty printed json string with new lines in it. Sending in None results in the most compact representation. --- awacs/__init__.py | 4 ++-- tests/test_base.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/awacs/__init__.py b/awacs/__init__.py index 2f9e687c..f0dd93df 100644 --- a/awacs/__init__.py +++ b/awacs/__init__.py @@ -111,7 +111,7 @@ def JSONrepr(self) -> dict: self.validate() return self.resource - def to_json(self, indent: int = 4, sort_keys: bool = True) -> str: + def to_json(self, indent: Optional[int] = 4, sort_keys: bool = True) -> str: p = self.properties return json.dumps(p, cls=awsencode, indent=indent, sort_keys=sort_keys) @@ -149,7 +149,7 @@ def getdata(self, data: Union[AWSObject, T]) -> Union[str, None, T]: else: return data - def to_json(self, indent: int = 4, sort_keys: bool = True) -> str: + def to_json(self, indent: Optional[int] = 4, sort_keys: bool = True) -> str: p = self return json.dumps(p, cls=awsencode, indent=indent, sort_keys=sort_keys) diff --git a/tests/test_base.py b/tests/test_base.py index dc44a499..8e881edf 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -50,6 +50,22 @@ def test_invalid_property(self): "'statement'", ) + def test_to_json(self): + p = PolicyDocument(Version="2012-10-17", Statement=[]) + + self.assertEqual( + p.to_json(), + '{\n "Statement": [],\n "Version": "2012-10-17"\n}', + ) + + def test_to_json_indent_non(self): + p = PolicyDocument(Version="2012-10-17", Statement=[]) + + self.assertEqual( + p.to_json(indent=None), + '{"Statement": [], "Version": "2012-10-17"}', + ) + class TestAWSProperty(unittest.TestCase): def test_prop_value_type_mismatch_expect_list(self):