Skip to content

Commit

Permalink
Merge pull request #11 from ecaron/setAsString
Browse files Browse the repository at this point in the history
Introducing an option to convert BigInts to strings
  • Loading branch information
sidorares committed Mar 8, 2016
2 parents 919bdb9 + a603a7f commit 9fdf758
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,32 @@ Succesfully catched expected exception on duplicate keys: {"name":"SyntaxError",
```

#### options.storeAsString, boolean, default false
Specifies if BigInts should be stored in the object as a string, rather than the default BigNumber.

Note that this is a dangerous behavior as it breaks the default functionality of being able to convert back-and-forth without data type changes (as this will convert all BigInts to be-and-stay strings).

example:
```js
var JSONbig = require('json-bigint');
var JSONbigString = require('json-bigint')({"storeAsString": true});
var key = '{ "key": 1234567890123456789 }';
console.log('\n\nStoring the BigInt as a string, instead of a BigNumber');
console.log('Input:', key);
var withInt = JSONbig.parse(key);
var withString = JSONbigString.parse(key);
console.log('Default type: %s, With option type: %s', typeof withInt.key, typeof withString.key);

```

Output
```
Storing the BigInt as a string, instead of a BigNumber
Input: { "key": 1234567890123456789 }
Default type: object, With option type: string
```


### Links:
- [RFC4627: The application/json Media Type for JavaScript Object Notation (JSON)](http://www.ietf.org/rfc/rfc4627.txt)
Expand Down
8 changes: 6 additions & 2 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ var json_parse = function (options) {

// Default options one can override by passing options to the parse()
var _options = {
"strict": false // not being strict means do not generate syntax errors for "duplicate key"
"strict": false, // not being strict means do not generate syntax errors for "duplicate key"
"storeAsString": false // toggles whether the values should be stored as BigNumber (default) or a string
};


Expand All @@ -84,6 +85,9 @@ var json_parse = function (options) {
if (options.strict === true) {
_options.strict = true;
}
if (options.storeAsString === true) {
_options.storeAsString = true;
}
}


Expand Down Expand Up @@ -170,7 +174,7 @@ var json_parse = function (options) {
//if (number > 9007199254740992 || number < -9007199254740992)
// Bignumber has stricter check: everything with length > 15 digits disallowed
if (string.length > 15)
return new BigNumber(string);
return (_options.storeAsString === true) ? string : new BigNumber(string);
return number;
}
},
Expand Down
21 changes: 21 additions & 0 deletions test/string-option-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var mocha = require('mocha')
, assert = require('chai').assert
, expect = require('chai').expect
;

describe("Testing 'storeAsString' option", function(){
var key = '{ "key": 12345678901234567 }';
it("Should show that the key is of type object", function(done){
var JSONbig = require('../index');
var result = JSONbig.parse(key);
expect(typeof result.key).to.equal("object");
done();
});

it("Should show that key is of type string, when storeAsString option is true", function(done){
var JSONstring = require('../index')({"storeAsString": true});
var result = JSONstring.parse(key);
expect(typeof result.key).to.equal("string");
done();
});
});

0 comments on commit 9fdf758

Please sign in to comment.