blog icon indicating copy to clipboard operation
blog copied to clipboard

Node.js 处理 thrift 的 int64

Open huruji opened this issue 4 years ago • 0 comments

在 thrift idl 定义会使用到 int64 这种数据类型,但是 javascript 只支持 IEEE 754 double-precision floats,因此无法支持 int64,再以下这段代码中 int64 被输出成了 buffer

image

idl

image

image

node.js 中通过 node-int64 处理,同时由于 JSON.stringify 支持一个 replacer 参数,可以再这个参数中指定处理 int64:

const Int64 = require('node-int64');
function customStringifier(key, value) {
  if (value instanceof Int64) {
    return value.toNumber();
  } else {
    return value;
  }
}

console.log(JSON.stringify(response, customStringifier, 2))

这个时候就能正常处理 int64 了:

image

参考: https://httgp.com/deserializing-thrift-data-in-node-js/

huruji avatar Feb 27 '21 07:02 huruji