Skip to content

[jsonpb] Do not marshal empty slices, maps and objects #323

Closed
@fische

Description

@fische

Hi,

The Marshaler from jsonpb does not marshal empty slices, maps and objects even with the EmitDefaults option.

For example, with following protobuf message:

message Message {
	message SubMessage {
		string str = 1;
	}
	repeated string arr = 1;
	map<string, string> m = 2;
	SubMessage sub = 3;
}

... and the following go code:

func main() {
	var (
		msg       pb.Message
		marshaler = jsonpb.Marshaler{
			OrigName:     true,
			EmitDefaults: true,
		}
	)
	if s, err := marshaler.MarshalToString(&msg); err != nil {
		log.Fatalf("Could not marshal proto message : %v", err)
	} else {
		fmt.Println(s)
	}
}

I get:

{}

Is that done by design? Or can/will it be fixed?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions