dswm97353 2018-07-04 13:27
浏览 761
已采纳

如何使用jsonpb将JSON解组到包含其中一个定义的protobuf中?

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?

  • 写回答

1条回答 默认 最新

  • donglu1971 2018-07-06 09:05
    关注

    You have a Type mix up due to the two vendor folders. A type declared in two vendor folders is not the same, even if the code for them is exactly the same.

    Thus for example: prototest-master/vendor/github.com/gogo/proto/messageSet (A) is not of the same type as prototest-master/proto/vendor/github.com/gogo/proto/messageSet (B) even as their imports are the same (github.com/gogo/proto)

    In your project's main.go the construction of the marshaler jsonpb.Marshaler{} uses the types from (A) where the actual messages like &pb.ParameterSets{... use the types from (B) to construct itself.

    Since jsonpb seems to do a lot of reflection stuff it breaks on the mixing of the two types.

    A better solution is to use just one vendor folder to be clear on the types everyone uses. I would just ditch the (B) vendor folder because it adds nothing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧