Skip to content
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

Optionally omit defaults in JSON #682

Closed
alexhayes opened this issue Oct 10, 2022 · 2 comments
Closed

Optionally omit defaults in JSON #682

alexhayes opened this issue Oct 10, 2022 · 2 comments

Comments

@alexhayes
Copy link

I'd really love a way of calling toJSON that omits the defaults from the output. Ideally this would be an argument supplied to the toJSON call because I have a use case for both.

  1. Use Case 1: Snapshots
    In this use case I'd like to omit defaults to remove noise from Jest snapshots.

  2. Use Case 2: Browser/Workshop Output
    In this use case I'd like to include defaults as it's easier when running a workshop if all defaults are included - rather than having to describe to attendees "oh, your attribute is not being set because it's the default"

Thus, my proposal would be that the signature for toJSON would take an optional second parameter omitDefaults which defaults to false (to maintain current behaviour).

This relates to this section in the docs;

When writing JSON, ts-proto currently does not normalize message when converting to JSON, other than omitting unset fields, but it may do so in the future.

// Current ts-proto behavior
Foo.toJSON({}); // => { }
Foo.toJSON({ bar: undefined }); // => { }
Foo.toJSON({ bar: '' }); // => { bar: '' } - note: this is the default value, but it's not omitted
Foo.toJSON({ bar: 'baz' }); // => { bar: 'baz' }
// Possible future behavior, where ts-proto would normalize message
Foo.toJSON({}); // => { }
Foo.toJSON({ bar: undefined }); // => { }
Foo.toJSON({ bar: '' }); // => { } - note: omitting the default value, as expected
Foo.toJSON({ bar: 'baz' }); // => { bar: 'baz' }
  • Please open an issue if you need this behavior.

Given it appears some thought has already gone into this, would this use case be supported or is the preference to use a --ts_proto_opt= value?

@stephenh
Copy link
Owner

Hey @alexhayes ; I don't have strong opinions about whether to just always add the omitDefaults flag, or to only output it behind a flag.

It seems fine to me to just always output it, with the default behavior as-is, and see if anyone complains.

If you'd like to submit a PR that'd be great!

@migmartri
Copy link

migmartri commented Feb 2, 2023

This feature would be very helpful for me too!

In Go proto marshaller there is an equivalent flag called EmitUnpopulated, but in contrast to this proposal, by default is set to false while our current behavior here is true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants