sidorares / json-bigint

JSON.parse/stringify with bigints support
MIT License
814 stars 196 forks source link

Make it easy to get JSON that can be re-used by code that is not aware of json-bigint (aka stringify big numbers) #4

Open tkissing opened 10 years ago

tkissing commented 10 years ago

See the updated "test" why this is useful :)

sidorares commented 10 years ago

Not sure if you really need this in the package - if you need stringified ints instead of BigNumber's you can add reviver in your own code, it's just 3 lines function as you demonstrated.

My biggest concern is that you proposing to change JSON.parse api ( probably in backward compartible way, but still). Please convince me with real life examples :)

Also, if you really want to help with this project - could you make a proper test suite out of test.js ( preferably using mocha )

anyway, thanks for your suggestion and waiting for more discussion

KeKs0r commented 9 years ago

@sidorares I actually have a use case where I need to sent a BigInt to an API. And somehow I can't construct it without this PR.

thats my test that only passes with @tkissing changes:

    describe('BigINt', {only:true},function(){
        it('is the same', function(done){
            var JSONbig = require('json-bigint');
            var BigNumber = require('bignumber.js');
            var input = '{"id":40564371973213694,"custom_id":"2"}';
            var parsed = JSONbig.parse(input);
            var output = JSONbig.stringify(parsed);
            expect(input).equal(output);

            var ownJSON = {
                id: new BigNumber("40564371973213694"),
                custom_id: "2"
            };
            var outOwn = JSONbig.stringify(ownJSON);
            expect(outOwn).equal(input);
            done();
        });
    });