4

I am trying to push a 64 bit integer data to a JSONNode using json.push_back call

    uint64_t myHigh = 0x10;          
    uint64_t myLow = 0x12;
    uint64_t myFinal = 0;


    myFinal = (myHigh << 32) | myLow ;

    std::cout << "val = 0x" << std::hex << myFinal << "\n";-----(1)
    JSONNode jvData;

    jvData.push_back(JSONNode("value",myFinal));
    std::cout<<jvData.write();--------------------------(2)

The cout (1) gives a value 0xa0000000c The cout (2) shows a value 12.

I expect the cout (2) value to be 42949672972 but doesnt seem to work as expected

Does Json support 64 bit int??

Matthieu Rouget
  • 3,289
  • 18
  • 23
payyans4u
  • 351
  • 2
  • 5
  • 16

1 Answers1

5

64 bits integers cannot be represented in JSON since JavaScript internally codes values as 64 bits floating point values (http://ecma262-5.com/ELS5_HTML.htm#Section_8.5).

Thus, you are limited to 53 bits of precision (2^53).

If you want to exchange 64 bits integers, you may use strings or split the 64 integer in two 32 bits integers and then recombine them (What is the accepted way to send 64-bit values over JSON?).

AJM
  • 1,317
  • 2
  • 15
  • 30
Matthieu Rouget
  • 3,289
  • 18
  • 23
  • But Python's `json` serializes and deserializes them as 64-bit ints just fine. While it would not technically be correct/valid/canonical JSON, I see no problem in having bignums as long ints as long as all JSON implementations in your use support them. – Mischa Arefiev Jan 29 '14 at 11:21
  • @MischaArefiev See https://developers.google.com/discovery/v1/type-format which states "For example, a 64-bit integer cannot be represented in JSON (since JavaScript and JSON support integers up to 2^53). Therefore, a 64-bit integer must be represented as a string in JSON requests/responses. So the type property will be set to "string", but the format property will be set to "int64" to indicate that it is a 64-bit integer." – AJM Nov 19 '20 at 10:12
  • Additional source: [RFC 7519](https://tools.ietf.org/html/rfc7159#section-6): "Note that when such software is used, numbers that are integers and are in the range [-(2**{53})+1, (2**{53})-1] are interoperable in the sense that implementations will agree exactly on their numeric values." – AJM Nov 19 '20 at 10:13