-
Notifications
You must be signed in to change notification settings - Fork 104
feat: add default parameter to JsonObject.serialize() #1508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
waiho-gumloop
wants to merge
1
commit into
googleapis:main
from
waiho-gumloop:feat/json-object-serialize-default
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,3 +96,51 @@ def test_w_JsonObject_of_list_of_simple_JsonData(self): | |
| expected = json.dumps(data, sort_keys=True, separators=(",", ":")) | ||
| data_jsonobject = JsonObject(JsonObject(data)) | ||
| self.assertEqual(data_jsonobject.serialize(), expected) | ||
|
|
||
|
|
||
| class Test_JsonObject_serialize_default(unittest.TestCase): | ||
| """Tests for the ``default`` parameter of ``JsonObject.serialize()``.""" | ||
|
|
||
| def test_dict_with_custom_type_and_default(self): | ||
| from datetime import datetime | ||
|
|
||
| dt = datetime(2023, 6, 15, 9, 30, 0) | ||
| data = {"ts": dt, "name": "test"} | ||
| obj = JsonObject(data) | ||
| result = obj.serialize(default=lambda o: o.isoformat() if isinstance(o, datetime) else str(o)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This lambda function is repeated in For example: class Test_JsonObject_serialize_default(unittest.TestCase):
_DATETIME_SERIALIZER = lambda o: o.isoformat() if isinstance(o, datetime) else str(o)
def test_dict_with_custom_type_and_default(self):
# ...
result = obj.serialize(default=self._DATETIME_SERIALIZER)
# ... |
||
| parsed = json.loads(result) | ||
| self.assertEqual(parsed["ts"], "2023-06-15T09:30:00") | ||
| self.assertEqual(parsed["name"], "test") | ||
|
|
||
| def test_array_with_custom_type_and_default(self): | ||
| from datetime import datetime | ||
|
|
||
| dt = datetime(2023, 1, 1) | ||
| data = [dt, "hello"] | ||
| obj = JsonObject(data) | ||
| result = obj.serialize(default=lambda o: o.isoformat() if isinstance(o, datetime) else str(o)) | ||
| parsed = json.loads(result) | ||
| self.assertEqual(parsed[0], "2023-01-01T00:00:00") | ||
| self.assertEqual(parsed[1], "hello") | ||
|
|
||
| def test_without_default_raises_on_custom_type(self): | ||
| from datetime import datetime | ||
|
|
||
| data = {"ts": datetime(2023, 1, 1)} | ||
| obj = JsonObject(data) | ||
| with self.assertRaises(TypeError): | ||
| obj.serialize() | ||
|
|
||
| def test_default_none_preserves_existing_behavior(self): | ||
| data = {"foo": "bar"} | ||
| expected = json.dumps(data, sort_keys=True, separators=(",", ":")) | ||
| obj = JsonObject(data) | ||
| self.assertEqual(obj.serialize(default=None), expected) | ||
|
|
||
| def test_scalar_with_default(self): | ||
| from datetime import datetime | ||
|
|
||
| dt = datetime(2023, 6, 15) | ||
| obj = JsonObject(dt) | ||
| result = obj.serialize(default=lambda o: o.isoformat() if isinstance(o, datetime) else str(o)) | ||
| self.assertEqual(json.loads(result), "2023-06-15T00:00:00") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example
defaultfunction can lead to subtle bugs. Theelse str(o)clause will convert any unhandled non-serializable object into its string representation (e.g.,'<MyObject object at 0x...>'), which might not be the desired behavior and can mask serialization errors. A more robust approach is to handle only the expected types and let other types raise aTypeError, which is the standard behavior ofjson.dumps.Consider providing a more explicit example that promotes this safer pattern, even if it's more verbose: