Field name mismatch while deserialising json to golang struct generated from proto

huangapple go评论85阅读模式
英文:

Field name mismatch while deserialising json to golang struct generated from proto

问题

我有一个类似下面的原型:

message HeaderAttributes {
  int32 page_count = 1;
  int32 total_count = 2;
}

message Header {
  HeaderAttributes header_attributes = 1;
}

我使用 protoc 生成了对应该原型的结构体,如下所示:

type Header struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	HeaderAttributes *HeaderAttributes `protobuf:"bytes,1,opt,name=header_attributes,json=headerAttributes,proto3" json:"header_attributes,omitempty"`
}

type HeaderAttributes struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	PageCount  int32 `protobuf:"varint,1,opt,name=page_count,json=pageCount,proto3" json:"page_count,omitempty"`
	TotalCount int32 `protobuf:"varint,2,opt,name=total_count,json=totalCount,proto3" json:"total_count,omitempty"`
}

当我尝试将下面的 JSON 响应反序列化为结构体时,由于字段名称不匹配,它无法正常工作:

"header": {
    "headerAttributes": {
        "pageCount": 1,
        "totalCount": 1
    }
}

有人能否建议如何将生成的结构体的字段名称转换为驼峰命名法?

英文:

I've a proto like below

message HeaderAttributes {
  int32 page_count = 1;
  int32 total_count = 2;
}

message Header {
  HeaderAttributes header_attributes = 1;
}

I've generated the struct corresponding to this proto using protoc like below

type Header struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	HeaderAttributes *HeaderAttributes `protobuf:"bytes,1,opt,name=header_attributes,json=headerAttributes,proto3" json:"header_attributes,omitempty"`
}

type HeaderAttributes struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	PageCount  int32 `protobuf:"varint,1,opt,name=page_count,json=pageCount,proto3" json:"page_count,omitempty"`
	TotalCount int32 `protobuf:"varint,2,opt,name=total_count,json=totalCount,proto3" json:"total_count,omitempty"`
}

When I try to deserialise below json http response to struct it does not work due to field name mismatch

"header": {
        "headerAttributes": {
            "pageCount": 1,
            "totalCount": 1
        }
    }

Can someone suggest on how to convert field names in generated struct to be camel case ?

答案1

得分: 1

你的消息的JSON编码版本不应以"header"为根。

package main

import (
	"fmt"

	pb "github.com/.../76280593/protos"
	"google.golang.org/protobuf/encoding/protojson"
)

func main() {
	j := []byte(`
{
	"headerAttributes": {
		"pageCount": 1,
		"totalCount": 1
	}
}`)

	message := &pb.Header{}
	if err := protojson.Unmarshal(j, message); err != nil {
		log.Fatal(err)
	}
	log.Printf("+%v", m2)
}

成功:

header_attributes:{page_count:1  total_count:1}
英文:

The JSON-encoded version of your message should not be rooted on "header"

package main

import (
	"fmt"

	pb "github.com/.../76280593/protos"
	"google.golang.org/protobuf/encoding/protojson"
)

func main() {
	j = []byte(`
{
	"headerAttributes": {
		"pageCount": 1,
		"totalCount": 1
	}
}`)

	message := &pb.Header{}
	if err := protojson.Unmarshal(j, message); err != nil {
		log.Fatal(err)
	}
	log.Printf("+%v", m2)
}

Succeeds:

header_attributes:{page_count:1  total_count:1}

huangapple
  • 本文由 发表于 2023年5月18日 19:53:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76280593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定