blog
blog copied to clipboard
Node.js 处理 thrift 的 int64
在 thrift idl 定义会使用到 int64 这种数据类型,但是 javascript 只支持 IEEE 754 double-precision floats,因此无法支持 int64,再以下这段代码中 int64 被输出成了 buffer
idl
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 了: