Skip to content
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

Marshal embedded message into a flat JSON string #211

Open
liranp opened this issue Oct 10, 2016 · 3 comments
Open

Marshal embedded message into a flat JSON string #211

liranp opened this issue Oct 10, 2016 · 3 comments

Comments

@liranp
Copy link

liranp commented Oct 10, 2016

I'm trying to embed two messages into one while using the jsontag extension to allow the marshaler to marshal it into a flat JSON string.

I have the following messages in my proto file:

message Meta {
    google.protobuf.Timestamp created_at = 1 [(gogoproto.stdtime) = true];
    google.protobuf.Timestamp updated_at = 2 [(gogoproto.stdtime) = true];
    google.protobuf.Timestamp deleted_at = 3;
}

message Person {
    string id = 1;
    Meta meta = 2 [(gogoproto.embed) = true, (gogoproto.jsontag) = ""];
}

In the generated .pb.go file, this is translated to types Meta and Person like this:

type Meta struct {
    CreatedAt *time.Time `protobuf:"bytes,1,opt,name=created_at,json=createdAt,stdtime" json:"created_at,omitempty"`
    UpdatedAt *time.Time `protobuf:"bytes,2,opt,name=updated_at,json=updatedAt,stdtime" json:"updated_at,omitempty"`
    DeletedAt time.Time  `protobuf:"bytes,3,opt,name=deleted_at,json=deletedAt,stdtime" json:"deleted_at,omitempty"`
}

type Person struct {
    Id    string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
    *Meta `protobuf:"bytes,2,opt,name=meta,embedded=meta" json:""`
}

But, the jsonpb.Marshaler produces the following JSON string:

{"id":"foo","meta":{"created_at":"2016-10-10T11:15:24.750978411Z"}}

While the expected result is:

{"id":"foo", "created_at":"2016-10-10T11:15:24.750978411Z"}

It is worth mentioning that with the standard library JSON marshaler it works fine, but it limits us to use only primitive types. Other proto types like StringValue, Int32Value, etc. produce the same issue in the opposite way (because they are just a wrapper on top of the primitive type).

Attached a gist with the relevant files.

Any help will be much appreciated!

@awalterschulze
Copy link
Member

So that jsontags work with the standard json library and jsonpb assume its a normal protobuf that does not allow this type of behaviour.

I don't know that this is something I want to support, but if you make a easily maintainable pull request I'll seriously consider it.

@awalterschulze
Copy link
Member

If you want to discuss further we can reopen

@atombender
Copy link
Contributor

We would also like to have this fixed, in the interest of being able to offer a more natural JSON API on top of our gRPC implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants