Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,13 @@ var json_parse = (function () {
// Return the json_parse function. It will have access to all of the above
// functions and variables.

function bigNumberToStringReviver(key, value) {
if (value && value instanceof BigNumber) {
return value.toString();
}
return value;
}

return function (source, reviver) {
var result;

Expand All @@ -323,6 +330,10 @@ var json_parse = (function () {
error("Syntax error");
}

if (reviver === true) {
reviver = bigNumberToStringReviver;
}

// If there is a reviver function, we recursively walk the new structure,
// passing each name/value pair to the reviver function for possible
// transformation, starting with a temporary root object that holds the result
Expand Down
22 changes: 15 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
var JSONbig = require('./index.js');

var json = '{ "value" : 9223372036854775807, "v2": 123 }';

console.log('Input:', json);
console.log('');

console.log('node.js bult-in JSON:')
var r = JSON.parse(json);
console.log('JSON.parse(input).value : ', r.value.toString());
var r1 = JSONbig.parse(json);
var r2 = JSONbig.parse(json, true);

console.log('\nnode.js bult-in JSON:');
console.log('JSON.parse(input).value.toString(): ', r.value);
console.log('JSON.stringify(JSON.parse(input)):', JSON.stringify(r));

console.log('\n\nbig number JSON:');
var r1 = JSONbig.parse(json);
console.log('JSON.parse(input).value : ', r1.value.toString());
console.log('JSON.stringify(JSON.parse(input)):', JSONbig.stringify(r1));
console.log('\nbig number JSON:');
console.log('JSONbig.parse(input).value.toString(): ', r1.value.toString());
console.log('JSONbig.stringify(JSONbig.parse(input)):', JSONbig.stringify(r1));
console.log('JSON.stringify(JSONbig.parse(input)):', JSON.stringify(r1));

console.log('\nbig number JSON with default reviver:');
console.log('JSONbig.parse(input, true).value.toString(): ', r2.value.toString());
console.log('JSONbig.stringify(JSONbig.parse(input, true)):', JSONbig.stringify(r2));
console.log('JSON.stringify(JSONbig.parse(input, true)):', JSON.stringify(r2));