What is the accepted way to send 64-bit values over JSON?

后端 未结 6 728
死守一世寂寞
死守一世寂寞 2020-12-08 20:05

Some of my data are 64-bit integers. I would like to send these to a JavaScript program running on a page.

However, as far as I can tell, integers in most JavaScript

6条回答
  •  借酒劲吻你
    2020-12-08 21:03

    This seems to be less a problem with JSON and more a problem with Javascript itself. What are you planning to do with these numbers? If it's just a magic token that you need to pass back to the website later on, by all means simply use a string containing the value. If you actually have to do arithmetic on the value, you could possibly write your own Javascript routines for 64-bit arithmetic.

    One way that you could represent values in Javascript (and hence JSON) would be by splitting the numbers into two 32-bit values, eg.

      [ 12345678, 12345678 ]
    

    To split a 64-bit value into two 32-bit values, do something like this:

      output_values[0] = (input_value >> 32) & 0xffffffff;
      output_values[1] = input_value & 0xffffffff;
    

    Then to recombine two 32-bit values to a 64-bit value:

      input_value = ((int64_t) output_values[0]) << 32) | output_values[1];
    

提交回复
热议问题