Closed Big-big-orange closed 6 months ago
I think I have found the root cause:
according to https://github.com/jhump/protoreflect/blob/main/dynamic/json.go#L386,
if dataType=int64, it will become strings due to strconv.FormatInt()
, So I have a question, why we don't use https://github.com/jhump/protoreflect/blob/main/dynamic/json.go#L399 for dataType=int64
btw, I found the similar issue for uint64, https://github.com/jhump/protoreflect/blob/main/dynamic/json.go#L403
@Big-big-orange, this is part of the spec for the JSON format of protobuf messages: https://protobuf.dev/programming-guides/proto3/#json
64-bit integers are represented using JSON strings because JSON numbers are usually stored as IEEE 64-bit floats, which cannot represent all 64-bit integers. This is related to https://github.com/protocolbuffers/protobuf/issues/12994.
Also, FWIW, I recommend users migrate their usage of "github.com/jhump/protoreflect/dynamic" to instead use "google.golang.org/protobuf/types/dynamicpb". A (hopefully soon) v2 of this repo will remove the dynamic package (and others) since this functionality is now provided by the core Protobuf runtime for Go.
Hi, I found if the message is nested, the Integer fields are recognised as strings. Could you help to solve this problem?
Here is my proto file example
as you can see, the
Pagination
message is nested in theAddFriendReq
, when I setAddFriendReq
like this, theLimit
is set as int64(6)Then I use
dmsg.Unmarshal
anddmsg.MarshalJSONPB
to tranform the proto data into Json stringFinally, the jsStr will be
jsStr={"phone":["13145990022","131313233"],"keyword":"I am good","number":3,"pagination":{"cursor":"cursor","limit":"6"}}
the limit become "6", rather than int64(6)