GithubHelp home page GithubHelp logo

isabella232 / json-bignum Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rollbar/json-bignum

0.0 0.0 0.0 22 KB

Node.js JSON replacement which handles 64-bit integers and arbitrary-precision decimals.

License: MIT License

JavaScript 100.00%

json-bignum's Introduction

json-bignum

Node.js JSON replacement which handles 64-bit integers and arbitrary-precision decimals. It is a modified version of Douglas Crockford's JSON library. Although it can handle 64-bit integers and arbitrary-precision decimals, it is slower than the built-in JSON functions.

Install

$ npm install json-bignum

Usage

parse()

var bignumJSON = require('json-bignum');

var obj = bignumJSON.parse('{ "decimal": -9223372036854775807.4237482374983253298159 }');

stringify()

var bignumJSON = require('json-bignum');

var obj = {
    bigint: new bignumJSON.BigNumber('92233720368547758074237482374983253298159'),
    decimal: new bignumJSON.BigNumber('-9223372036854775807.4237482374983253298159'),
};

console.log(bignumJSON.stringify(obj));

BigNumber

The BigNumber class simply stores the number as a string. It does not support arithmetic, but if you need that here are some excellent libraries.

  • BigDecimal.js: a literal port of Java's BigInteger and BigDecimal classes.
  • bigint: Big integer arithmetic using GMP.
  • bignum: Big integer arithmetic using OpenSSL.
// example using BigDecimal.js

var bignumJSON = require('json-bignum');
var bigdecimal = require('bigdecimal');

var jsonStr = '{"normal":-922337203.234,"big":-9223372036854775807.4237482374983253298159}';
var jsonObj = bignumJSON.parse(jsonStr);

var a = new bigdecimal.BigDecimal(jsonObj.normal.toString());
var b = new bigdecimal.BigDecimal(jsonObj.big.toString());
var sum = a.add(b);

jsonObj.sum = new bignumJSON.BigNumber(sum.toString());

console.log(bignumJSON.stringify(jsonObj));

Caveats

It is not recommended to mix calls to JSON and bignumJSON. For example, JSON.stringify() does not know how to parse BigNumber.

Benchmark

Below shows the result of the benchmark on my machine.

$ node benchmark.js
10000 calls of JSON.parse():                                   26.746847 ms
10000 calls of JSON.stringify():                               20.824071 ms
10000 calls of bignumJSON.parse() with bignums in JSON:        221.945307 ms
10000 calls of bignumJSON.parse() without bignums in JSON:     150.626292 ms
10000 calls of bignumJSON.stringify() with bignums in JSON:    64.166056 ms
10000 calls of bignumJSON.stringify() without bignums in JSON: 61.860016 ms

json-bignum's People

Contributors

brianr avatar coryvirok avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.