Skip to content

Commit

Permalink
Fix jsonpb to emit zero values for Map, Slice, Ptr when EmitDefaults=…
Browse files Browse the repository at this point in the history
…true

When jsonpb.Marshaler.EmitDefaults is set to true, marshal zero values of the 
following types accordingly:
Map to {}
Slice to []
Ptr to null
  • Loading branch information
ewang authored and cybrcodr committed May 23, 2017
1 parent a4e8f93 commit 7a211bc
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 31 deletions.
12 changes: 11 additions & 1 deletion jsonpb/jsonpb.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent, typeU

// IsNil will panic on most value kinds.
switch value.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
case reflect.Chan, reflect.Func, reflect.Interface:
if value.IsNil() {
continue
}
Expand Down Expand Up @@ -235,6 +235,10 @@ func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent, typeU
if value.Len() == 0 {
continue
}
case reflect.Map, reflect.Ptr, reflect.Slice:
if value.IsNil() {
continue
}
}
}

Expand Down Expand Up @@ -402,6 +406,12 @@ func (m *Marshaler) marshalValue(out *errWriter, prop *proto.Properties, v refle
var err error
v = reflect.Indirect(v)

// Handle nil pointer
if v.Kind() == reflect.Invalid {
out.write("null")
return out.err
}

// Handle repeated elements.
if v.Kind() == reflect.Slice && v.Type().Elem().Kind() != reflect.Uint8 {
out.write("[")
Expand Down
3 changes: 3 additions & 0 deletions jsonpb/jsonpb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ var marshalingTests = []struct {
`{"rFunny":[1,2]}`},
{"empty value", marshaler, &pb.Simple3{}, `{}`},
{"empty value emitted", Marshaler{EmitDefaults: true}, &pb.Simple3{}, `{"dub":0}`},
{"empty repeated emitted", Marshaler{EmitDefaults: true}, &pb.SimpleSlice3{}, `{"slices":[]}`},
{"empty map emitted", Marshaler{EmitDefaults: true}, &pb.SimpleMap3{}, `{"stringy":{}}`},
{"nested struct null", Marshaler{EmitDefaults: true}, &pb.SimpleNull3{}, `{"simple":null}`},
{"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
119 changes: 89 additions & 30 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.

12 changes: 12 additions & 0 deletions jsonpb/jsonpb_test_proto/more_test_objects.proto
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ message Simple3 {
double dub = 1;
}

message SimpleSlice3 {
repeated string slices = 1;
}

message SimpleMap3 {
map<string,string> stringy = 1;
}

message SimpleNull3 {
Simple3 simple = 1;
}

enum Numeral {
UNKNOWN = 0;
ARABIC = 1;
Expand Down

1 comment on commit 7a211bc

@AkihiroSuda
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

carrying this into gogo/protobuf gogo/protobuf#296

Please sign in to comment.