Skip to content

Emit empty array in JsonPB if EmitDefaults is enabled #232

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
wants to merge 1 commit into from

Conversation

zackangelo
Copy link

Forces an empty-valued array field to be serialized if EmitDefaults is set to true.

Mirrors Java's protobuf to JSON implementation when includingDefaultValueFields is enabled.

@zackangelo
Copy link
Author

@dsymonds @zombiezen nudge :)

@zombiezen
Copy link
Contributor

LGTM from a casual glance. @bcmills, thoughts?

@bcmills
Copy link
Contributor

bcmills commented Sep 20, 2016

Unless there are conflicts with the proto3 spec, the behavior of jsonpb should arguably mimic the standard json package. (EmitDefaults: false should correspond to omitempty.)

By my reading of the spec, the non-EmitDefaults behavior is currently out-of-spec:

The default value for repeated fields is empty (generally an empty list in the appropriate language).
[...]
If a field has the default value in the protocol buffer, it will be omitted in the JSON-encoded data by default to save space.

We do not appear to omit empty non-nil slices when EmitDefaults is false, but we probably should. (Perhaps that's a separate issue?)

That doesn't give us much guidance for nil slices when EmitDefaults is true, though. I would argue that we should follow the json package and emit a null in this case rather than an empty list:
https://play.golang.org/p/r6LBqu4mLI

Per the proto3 spec, "null is accepted as the empty list []", and that would remove another inconsistency between jsonpb and json.

@hyangah
Copy link
Contributor

hyangah commented Oct 7, 2016

I agree with @bcmills on emitting null for an empty list. Any objection?

@zackangelo can you update your PR and also add a test?

@bcmills
Copy link
Contributor

bcmills commented Nov 7, 2016

@zackangelo Are you still working on this? Is the PR ready for another round of reviews?

@cybrcodr
Copy link
Contributor

I just reviewed and merged in 7a211bc. I didn't see this PR beforehand and wasn't aware that there's a preference for null over [] from this discussion. Note that [] uses less characters than null. Deserialization should handle either case anyways. Let me know if there is any strong objection to that.

@cybrcodr cybrcodr closed this May 23, 2017
@golang golang locked and limited conversation to collaborators Jun 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants