Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / json-bigint / README.md
1 json-bigint
2 ===========
3
4 [![Build Status](https://secure.travis-ci.org/sidorares/json-bigint.png)](http://travis-ci.org/sidorares/json-bigint)
5 [![NPM](https://nodei.co/npm/json-bigint.png?downloads=true&stars=true)](https://nodei.co/npm/json-bigint/)
6
7 JSON.parse/stringify with bigints support. Based on Douglas Crockford [JSON.js](https://github.com/douglascrockford/JSON-js) package and [bignumber.js](https://github.com/MikeMcl/bignumber.js) library.
8
9 While most JSON parsers assume numeric values have same precision restrictions as IEEE 754 double, JSON specification _does not_ say anything about number precision. Any floating point number in decimal (optionally scientific) notation is valid JSON value. It's a good idea to serialize values which might fall out of IEEE 754 integer precision as strings in your JSON api, but `{ "value" : 9223372036854775807}`, for example, is still a valid RFC4627 JSON string, and in most JS runtimes the result of `JSON.parse` is this object: `{ value: 9223372036854776000 }`
10
11 ==========
12
13 example:
14
15 ```js
16 var JSONbig = require('json-bigint');
17
18 var json = '{ "value" : 9223372036854775807, "v2": 123 }';
19 console.log('Input:', json);
20 console.log('');
21
22 console.log('node.js bult-in JSON:')
23 var r = JSON.parse(json);
24 console.log('JSON.parse(input).value : ', r.value.toString());
25 console.log('JSON.stringify(JSON.parse(input)):', JSON.stringify(r));
26
27 console.log('\n\nbig number JSON:');
28 var r1 = JSONbig.parse(json);
29 console.log('JSONbig.parse(input).value : ', r1.value.toString());
30 console.log('JSONbig.stringify(JSONbig.parse(input)):', JSONbig.stringify(r1));
31 ```
32
33 Output:
34
35 ```
36 Input: { "value" : 9223372036854775807, "v2": 123 }
37
38 node.js bult-in JSON:
39 JSON.parse(input).value :  9223372036854776000
40 JSON.stringify(JSON.parse(input)): {"value":9223372036854776000,"v2":123}
41
42
43 big number JSON:
44 JSONbig.parse(input).value :  9223372036854775807
45 JSONbig.stringify(JSONbig.parse(input)): {"value":9223372036854775807,"v2":123}
46 ```
47 ### Options
48 The behaviour of the parser is somewhat configurable through 'options'
49
50 #### options.strict, boolean, default false
51 Specifies the parsing should be "strict" towards reporting duplicate-keys in the parsed string.
52 The default follows what is allowed in standard json and resembles the behavior of JSON.parse, but overwrites any previous values with the last one assigned to the duplicate-key.
53
54 Setting options.strict = true will fail-fast on such duplicate-key occurances and thus warn you upfront of possible lost information.
55
56 example:
57 ```js
58 var JSONbig = require('json-bigint');
59 var JSONstrict = require('json-bigint')({"strict": true});
60
61 var dupkeys = '{ "dupkey": "value 1", "dupkey": "value 2"}';
62 console.log('\n\nDuplicate Key test with both lenient and strict JSON parsing');
63 console.log('Input:', dupkeys);
64 var works = JSONbig.parse(dupkeys);
65 console.log('JSON.parse(dupkeys).dupkey: %s', works.dupkey);
66 var fails = "will stay like this";
67 try {
68     fails = JSONstrict.parse(dupkeys);
69     console.log('ERROR!! Should never get here');
70 } catch (e) {
71     console.log('Succesfully catched expected exception on duplicate keys: %j', e);
72 }
73 ```
74
75 Output
76 ```
77 Duplicate Key test with big number JSON
78 Input: { "dupkey": "value 1", "dupkey": "value 2"}
79 JSON.parse(dupkeys).dupkey: value 2
80 Succesfully catched expected exception on duplicate keys: {"name":"SyntaxError","message":"Duplicate key \"dupkey\"","at":33,"text":"{ \"dupkey\": \"value 1\", \"dupkey\": \"value 2\"}"}
81
82 ```
83
84 #### options.storeAsString, boolean, default false
85 Specifies if BigInts should be stored in the object as a string, rather than the default BigNumber.
86
87 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).
88
89 example:
90 ```js
91 var JSONbig = require('json-bigint');
92 var JSONbigString = require('json-bigint')({"storeAsString": true});
93 var key = '{ "key": 1234567890123456789 }';
94 console.log('\n\nStoring the BigInt as a string, instead of a BigNumber');
95 console.log('Input:', key);
96 var withInt = JSONbig.parse(key);
97 var withString = JSONbigString.parse(key);
98 console.log('Default type: %s, With option type: %s', typeof withInt.key, typeof withString.key);
99
100 ```
101
102 Output
103 ```
104 Storing the BigInt as a string, instead of a BigNumber
105 Input: { "key": 1234567890123456789 }
106 Default type: object, With option type: string
107
108 ```
109
110
111 ### Links:
112 - [RFC4627: The application/json Media Type for JavaScript Object Notation (JSON)](http://www.ietf.org/rfc/rfc4627.txt)
113 - [Re: \[Json\] Limitations on number size?](http://www.ietf.org/mail-archive/web/json/current/msg00297.html)
114 - [Is there any proper way to parse JSON with large numbers? (long, bigint, int64)](http://stackoverflow.com/questions/18755125/node-js-is-there-any-proper-way-to-parse-json-with-large-numbers-long-bigint)
115 - [What is JavaScript's Max Int? What's the highest Integer value a Number can go to without losing precision?](http://stackoverflow.com/questions/307179/what-is-javascripts-max-int-whats-the-highest-integer-value-a-number-can-go-t)
116 - [Large numbers erroneously rounded in Javascript](http://stackoverflow.com/questions/1379934/large-numbers-erroneously-rounded-in-javascript)
117