-
Notifications
You must be signed in to change notification settings - Fork 1.6k
jsonpb: how to handle array of proto message #675
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
Comments
Have you tried github.com/gogo/protobuf/jsonpb rather than github.com/golang/protobuf/jsonpb ? |
@awalterschulze thanks, I did miss the gogo/protobuf/jsonpb ( my fault I should have known that you did add jsonpb to gogo too ) but sadly even switching to gogo/jsonpb does not fix the error |
Nothing to be sorry about, it is a shame that there are jsonpb packages in the first place. Ok so that is good to know. I suspect that you might have to pass &CreateObjectRequest{} instead of []*api.Specificity to jsonpb. |
with the &CreateObjectRequest it works fine, but the json will be in fact the whole CreateObjectRequest ( I have filtered out useless field to be clearer on the issue ) when I only want one field to be marshaled :/ |
I don't think marshaling a list is supported by jsonpb. You could also marshal each item in the list *api.Specificity and then concat them into a json compatible list yourself, not ideal, but it should work? |
Yep, this is what I though when trying to do this. I wasn't sure if it was the best idea, so I opened the issue to see if there was a native way to do it. thanks ( it is worth the time to try and think about implementing a support of this case ? ) |
If you want this to be supported I would suggest asking golang/protobuf. |
I sorry I see this is on golang/protobuf |
yes no problem :) , I understand, I will let the issue opened, so they can judge if it is worth or not. btw, I can think of a way to marshal the list ( like you suggest ). I will have a json object ( valid tho ) but to unmarshal the list, I will have to create a dedicate struct and then iterate over it to be able to unmarshal the correct value ? |
Another option, which the jsonpb interface almost seems built for, is to use the json.Decoder stuff. Sadly, you end up having to worry about tokens, but it does work: jsonDecoder := json.NewDecoder(strings.NewReader(<...insert jsonpb here...>))
// read open bracket
_, err = jsonDecoder.Token()
if err != nil {
log.Fatal(err)
}
var protoMessages []*my.ProtoMessage
for jsonDecoder.More() {
protoMessage := my.ProtoMessage{}
err := jsonpb.UnmarshalNext(jsonDecoder, &protoMessage)
if err != nil {
log.Fatal(err)
}
protoMessages = append(protoMessages, &protoMessage)
} |
What you are seeing is an oddity in the protobuf type system where "repeated" is a property of a field not a property of the type. Thus, unlike many other languages (e.g., Go) or serialization formats (e.g., JSON), there is no such thing as a top-level list type. The only top-level data structure in protobufs are messages. Since the Thus, you can't serialize out a JSON array directly with the Also, if we end up adding an option to generate
The |
Hello guys,
I am struggling with a use case that I don't find any reference on any issue.
My use case is :
I've a message request that contains :
the generated code look like this :
When I create the object, I want to store it into a postgres db.
So I try to do this
I got an error
same goes if use
json.Marshal
instead of jsonpb to bypass the create, when I want tojson.Unmarshal
it does not work.Do I have to create a specific Marshal/UnMarshal to handle the array, Or I am missing something ( or do something wrong ) ?
PS:
I use
gogoproto
but I think it is linked tojsonpb
The text was updated successfully, but these errors were encountered: