You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Use Case 1: Snapshots
In this use case I'd like to omit defaults to remove noise from Jest snapshots.
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).
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 behaviorFoo.toJSON({});// => { }Foo.toJSON({bar: undefined});// => { }Foo.toJSON({bar: ''});// => { bar: '' } - note: this is the default value, but it's not omittedFoo.toJSON({bar: 'baz'});// => { bar: 'baz' }
// Possible future behavior, where ts-proto would normalize messageFoo.toJSON({});// => { }Foo.toJSON({bar: undefined});// => { }Foo.toJSON({bar: ''});// => { } - note: omitting the default value, as expectedFoo.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?
The text was updated successfully, but these errors were encountered:
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
I'd really love a way of calling
toJSON
that omits the defaults from the output. Ideally this would be an argument supplied to thetoJSON
call because I have a use case for both.Use Case 1: Snapshots
In this use case I'd like to omit defaults to remove noise from Jest snapshots.
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 parameteromitDefaults
which defaults to false (to maintain current behaviour).This relates to this section in the docs;
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?The text was updated successfully, but these errors were encountered: