protoreflect icon indicating copy to clipboard operation
protoreflect copied to clipboard

an int64 value in pb is changed to string when use PbToJson Message.MarshalJSONPB

Open li-ming-lei opened this issue 2 years ago • 3 comments

li-ming-lei avatar Apr 07 '22 13:04 li-ming-lei

This is expected because the range of a 64-bit integer is not necessarily representable by JSON numbers (which are typically interpreted as 64-bit floats, so they cannot accurately represent all integers greater than 2^53).

See the docs on the JSON format for protocol buffers here, which specifies that 64-bit integers be marshaled as strings.

jhump avatar Apr 07 '22 14:04 jhump

This seems like a far issue. https://github.com/protocolbuffers/protobuf/issues/5015#issuecomment-411847550. Regard less of your reason, if i don't use the json output for a javascript interpreter , instead I only use it for show. i would perfer a numerical value. There are a lot of people strongly requesting this feature. One issue closed, another opened.

li-ming-lei avatar Apr 07 '22 16:04 li-ming-lei

change int64 to int32 instead, or use json.Marshal instead of jsonpb.Marshal.

hitzhangjie avatar Apr 20 '22 13:04 hitzhangjie

I don't see a compelling reason why this dynamic message package should provide different/more JSON marshaling capabilities than the main protobuf runtime. Sorry.

jhump avatar Aug 16 '22 21:08 jhump

change int64 to int32 instead, or use json.Marshal instead of jsonpb.Marshal.

Some people are using C++ rather than golang.

weiyinfu avatar Nov 20 '22 16:11 weiyinfu

I don't see a compelling reason why this dynamic message package should provide different/more JSON marshaling capabilities than the main protobuf runtime. Sorry.

I am using C++ in the client. The communication data with the server is JSON. The server-side developer don't want to use protobuffer. But I need a structure definition in the client. So I write proto and generate structure then serialize the structure as string. The server-side cannot parse string to int64. So I need to make some change.

In a word, I use protobuffer as a structure-to-json tool. I don't need ask other people follow the "int64 serialize as string" rule.

weiyinfu avatar Nov 20 '22 16:11 weiyinfu