why does uint64, fixed64 map to string when converted to JSON

1,148 views
Skip to first unread message

Shashwat Agarwal

unread,
Jul 18, 2016, 1:25:41 PM7/18/16
to Protocol Buffers
Hi,

I was looking at this document: https://developers.google.com/protocol-buffers/docs/proto3#json and noticed that int64, fixed64, uint64 are mapped to string when converting to JSON. Any specific reason why this is done? This deviation breaks a lot of other JSON parsers and any code in JS that relies on the fact that *int64 are numbers rather than string. 

Jon Skeet

unread,
Jul 19, 2016, 8:58:22 AM7/19/16
to Protocol Buffers
On Monday, 18 July 2016 18:25:41 UTC+1, Shashwat Agarwal wrote:
I was looking at this document: https://developers.google.com/protocol-buffers/docs/proto3#json and noticed that int64, fixed64, uint64 are mapped to string when converting to JSON. Any specific reason why this is done? This deviation breaks a lot of other JSON parsers and any code in JS that relies on the fact that *int64 are numbers rather than string. 

It's due to the way that all numbers in JSON are basically IEEE-754 64-bit doubles... which means that any 64-bit integers can lose information. Using a string instead, all 64-bit integers can be round-tripped with no loss of information.

Jon
 

Shashwat Agarwal

unread,
Jul 21, 2016, 4:59:31 AM7/21/16
to Protocol Buffers
Thanks a lot Jon. I have been dealing with JSON usually in the backend far more than with Javascript. 
Reply all
Reply to author
Forward
0 new messages