This repository was archived by the owner on Feb 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
feat: add default values parameter to to_json #164
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -97,6 +97,29 @@ class Zone(proto.Enum): | |
| assert json2 == '{"zone":"EPIPELAGIC"}' | ||
|
|
||
|
|
||
| def test_json_default_values(): | ||
| class Squid(proto.Message): | ||
| mass_kg = proto.Field(proto.INT32, number=1) | ||
| name = proto.Field(proto.STRING, number=2) | ||
|
|
||
| s = Squid(name="Steve") | ||
| json1 = ( | ||
| Squid.to_json(s, including_default_value_fields=False) | ||
| .replace(" ", "") | ||
| .replace("\n", "") | ||
| ) | ||
| assert json1 == '{"name":"Steve"}' | ||
|
|
||
| json2 = Squid.to_json(s).replace(" ", "").replace("\n", "") | ||
| assert ( | ||
| json2 == '{"name":"Steve","massKg":0}' or json2 == '{"massKg":0,"name":"Steve"}' | ||
|
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. Is the ordering not stable?
Contributor
Author
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. No but it shouldn't matter for json objects right?
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. I don't think it should matter, I just was curious if you were running into problems during testing. |
||
| ) | ||
|
|
||
| s1 = Squid.from_json(json1) | ||
| s2 = Squid.from_json(json2) | ||
| assert s == s1 == s2 | ||
|
|
||
|
|
||
| def test_json_unknown_field(): | ||
| # Note that 'lengthCm' is unknown in the local definition. | ||
| # This could happen if the client is using an older proto definition | ||
|
|
||
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.
@yon-mg Could you open a follow up PR to update the docstring below?
This will result in a change in behavior for someone already using this method Protobuf defaults to
Falsefor this option. https://googleapis.dev/python/protobuf/latest/google/protobuf/json_format.html#google.protobuf.json_format.MessageToJsonThere 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.
I'm not sure I understand. I did not change the way
google.protobuf.json_format.MessageToJsonworks. In fact this methodmessage.MessageMeta.to_jsonshould callMessageToJsonin the same way as it did before if no changes are made to the additional parameter I added.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.
Although I did neglect to update the docstring for this method which I will update shortly if that's what you mean.