admin管理员组文章数量:1023273
I have a simple request but It seems to be harder than expected. I have to parse a bigint
from a JSON stream. The value is 990000000069396215
. In my code, this value is declared in TypeScript like this: id_address: bigint
. But this is not working, the value is truncated, and return nothing like 9900000000693962100
How can I simply manage this bigint
in my code?
I have a simple request but It seems to be harder than expected. I have to parse a bigint
from a JSON stream. The value is 990000000069396215
. In my code, this value is declared in TypeScript like this: id_address: bigint
. But this is not working, the value is truncated, and return nothing like 9900000000693962100
How can I simply manage this bigint
in my code?
- 1 JSON notation does not support "bigint" values. Just old-school JavaScript numbers. You can represent it as a string and then turn it back into a bigint with your own code. – Pointy Commented Jun 23, 2020 at 16:19
-
4
The JSON spec actually does not specify anything about precision of numbers, so you can technically have a bigint. However, built-in functions like
JSON.parse
will have a tough time with this since I don't see any way to change how numbers are parsed. – Jacob Commented Jun 23, 2020 at 16:28 - @Jacob yes I suppose that's true, but as far as I know all modern browsers parse JSON number strings as plain numbers. – Pointy Commented Jun 23, 2020 at 16:46
2 Answers
Reset to default 3If you want to make it reliable and clean then always stringify/parse bigint values as objects:
function replacer( key: string, value: any ): any {
if ( typeof value === 'bigint' ) {
return { '__bigintval__': value.toString() };
}
return value;
}
function reviver( key: string, value: any ): any {
if ( value != null && typeof value === 'object' && '__bigintval__' in value ) {
return BigInt( value[ '__bigintval__' ] );
}
return value;
}
JSON.stringify( obj, replacer );
JSON.parse( str, reviver );
I guess you need to do something like this,
export interface Address {
id_address: string;
}
Then somewhere in your code where you implement this interface you need to do,
const value = BigInt(id_address); // I am guessing that inside your class you have spread your props and you can access id_address. So inside value you will get your Big integer value.
Reference for BigInt.
I have a simple request but It seems to be harder than expected. I have to parse a bigint
from a JSON stream. The value is 990000000069396215
. In my code, this value is declared in TypeScript like this: id_address: bigint
. But this is not working, the value is truncated, and return nothing like 9900000000693962100
How can I simply manage this bigint
in my code?
I have a simple request but It seems to be harder than expected. I have to parse a bigint
from a JSON stream. The value is 990000000069396215
. In my code, this value is declared in TypeScript like this: id_address: bigint
. But this is not working, the value is truncated, and return nothing like 9900000000693962100
How can I simply manage this bigint
in my code?
- 1 JSON notation does not support "bigint" values. Just old-school JavaScript numbers. You can represent it as a string and then turn it back into a bigint with your own code. – Pointy Commented Jun 23, 2020 at 16:19
-
4
The JSON spec actually does not specify anything about precision of numbers, so you can technically have a bigint. However, built-in functions like
JSON.parse
will have a tough time with this since I don't see any way to change how numbers are parsed. – Jacob Commented Jun 23, 2020 at 16:28 - @Jacob yes I suppose that's true, but as far as I know all modern browsers parse JSON number strings as plain numbers. – Pointy Commented Jun 23, 2020 at 16:46
2 Answers
Reset to default 3If you want to make it reliable and clean then always stringify/parse bigint values as objects:
function replacer( key: string, value: any ): any {
if ( typeof value === 'bigint' ) {
return { '__bigintval__': value.toString() };
}
return value;
}
function reviver( key: string, value: any ): any {
if ( value != null && typeof value === 'object' && '__bigintval__' in value ) {
return BigInt( value[ '__bigintval__' ] );
}
return value;
}
JSON.stringify( obj, replacer );
JSON.parse( str, reviver );
I guess you need to do something like this,
export interface Address {
id_address: string;
}
Then somewhere in your code where you implement this interface you need to do,
const value = BigInt(id_address); // I am guessing that inside your class you have spread your props and you can access id_address. So inside value you will get your Big integer value.
Reference for BigInt.
本文标签: javascriptHow to parse a JSON data (typeBigInt) in TypeScriptStack Overflow
版权声明:本文标题:javascript - How to parse a JSON data (type : BigInt) in TypeScript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745598726a2158328.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论