Skip to content

jsonpb: Honor EmitDefaults for nilable types #287

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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions jsonpb/jsonpb.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,13 @@ func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent, typeU
continue
}

// IsNil will panic on most value kinds.
switch value.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
if value.IsNil() {
continue
}
}

if !m.EmitDefaults {
switch value.Kind() {
// IsNil will panic on most value kinds.
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
if value.IsNil() {
continue
}
case reflect.Bool:
if !value.Bool() {
continue
Expand Down
1 change: 1 addition & 0 deletions jsonpb/jsonpb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ var marshalingTests = []struct {
`{"rFunny":[1,2]}`},
{"empty value", marshaler, &pb.Simple3{}, `{}`},
{"empty value emitted", Marshaler{EmitDefaults: true}, &pb.Simple3{}, `{"dub":0}`},
{"empty value emitted", Marshaler{EmitDefaults: true}, &pb.Slicy{}, `{"slicy":[]}`},
{"map<int64, int32>", marshaler, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}, `{"nummy":{"1":2,"3":4}}`},
{"map<int64, int32>", marshalerAllOptions, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}, nummyPrettyJSON},
{"map<string, string>", marshaler,
Expand Down
75 changes: 47 additions & 28 deletions jsonpb/jsonpb_test_proto/more_test_objects.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions jsonpb/jsonpb_test_proto/more_test_objects.proto
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ message Mappy {
map<uint32, bool> u32booly = 9;
map<uint64, bool> u64booly = 10;
}

message Slicy {
repeated string slicy = 1;
}
Loading