I'm currently failing to unmarshal a JSON snippet which is generated by jsonpb. Maybe it's just some kind of misunderstanding on my side, but when looking at the tests I'd expect it to work somehow.
Here's the relevant snippet of the pb.proto
:
syntax = "proto3";
package pb;
message Parameter {
string name = 1;
oneof value {
string str_value = 2;
int32 int_value = 3;
bool bool_value = 4;
float float_value = 5;
}
}
message ParameterSet {
bytes raw = 1;
repeated Parameter parameters = 2;
}
message ParameterSets {
map<string,ParameterSet> sets = 1;
}
Testing marshaling/unmarshaling using this simple snippet fails:
package main
import (
"fmt"
"github.com/gogo/protobuf/jsonpb"
"strings"
)
func main() {
m := jsonpb.Marshaler{}
str, err := m.MarshalToString(&pb.ParameterSets{Sets: map[string]*pb.ParameterSet{
"parameter": {
Raw: []byte{0, 1, 2, 3, 4, 5, 6, 1, 2},
Parameters: []*pb.Parameter{
{Name: "itest", Value: &pb.Parameter_IntValue{42}},
{Name: "stest", Value: &pb.Parameter_StrValue{"Foobar"}},
{Name: "btest", Value: &pb.Parameter_BoolValue{true}},
{Name: "ftest", Value: &pb.Parameter_FloatValue{41.99}},
},
},
}},)
fmt.Println(str)
fmt.Println(err)
sets := pb.ParameterSets{}
err = jsonpb.Unmarshal(strings.NewReader(str), &sets)
fmt.Println(sets)
fmt.Println(err)
}
It results in:
{"sets":{"parameter":{"raw":"AAECAwQFBgEC","parameters":[{"name":"itest","intValue":42},{"name":"stest","strValue":"Foobar"},{"name":"btest","boolValue":true},{"name":"ftest","floatValue":41.99}]}}}
<nil>
{map[]}
unknown field "intValue" in pb.Parameter
How can I get the oneof values back in the proto object?