最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to parse a JSON data (type : BigInt) in TypeScript - Stack Overflow

matteradmin6PV0评论

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?

Share Improve this question edited Jun 23, 2020 at 16:20 Heretic Monkey 12.1k7 gold badges61 silver badges131 bronze badges asked Jun 23, 2020 at 16:15 BenjaminBenjamin 1031 silver badge9 bronze badges 3
  • 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
Add a ment  | 

2 Answers 2

Reset to default 3

If 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.

Post a comment

comment list (0)

  1. No comments so far