Open jmartin127 opened 2 years ago
Note: While testing a solution for this, I found that this package actually doesn't follow the protobuf spec. The spec states that: Fields with default values are omitted by default in proto3 JSON output
. It also states that For strings, the default value is the empty string
. This package, on the other hand will emit the JSON key as long as the field was set.
Example 1 (currencyCode is set to 'USD', serialized JSON contains this key as expected):
final Money amount = Money.create()
..currencyCode = 'USD' // String type
..units = $fixnum.Int64.parseInt("1")
..nanos = 750000000;
Output 1:
Serialized JSON: {"currencyCode":"USD","units":"1","nanos":750000000}
Example 2 (currencyCode is set to the Default value '', serialized JSON should not emit this key according to the spec, but it does):
final Money amount = Money.create()
..currencyCode = '' // String type
..units = $fixnum.Int64.parseInt("1")
..nanos = 750000000;
Output 2:
Serialized JSON: {"currencyCode":"","units":"1","nanos":750000000}
Example 3 (currencyCode is never set, serialized JSON is as expected, and does not emit this key):
final Money amount = Money.create()
..units = $fixnum.Int64.parseInt("1")
..nanos = 750000000;
Output 3:
Serialized JSON: {"units":"1","nanos":750000000}
The proto3 spec states:
An example of this for the protoc-gen-go plugin is
EmitDefaults
option: https://pkg.go.dev/github.com/golang/protobuf/jsonpb#MarshalerI think it would be great if we could achieve this same functionality via the protoc-gen-dart plugin.
As far as I can tell, this functionality is not currently supported by this plugin. Please confirm if that is the case. If this functionality is not currently supported, I would like to look into adding it, as I think it could be a useful feature for other uses of the plugin as well.
Steps to reproduce:
message Money { string currency_code = 1; int64 units = 2; int32 nanos = 3; }
final Money amount = Money.create() ..currencyCode = "USD" ..units = $fixnum.Int64.parseInt("1") ..nanos = 750000000; var json = jsonEncode(amount.toProto3Json()); print('Serialized JSON: $json\n');
final Money amount = Money.create() ..currencyCode = "USD" ..nanos = 750000000;