Skip to content

convert all the big integers to string in JSON before JSON.parse

License

Notifications You must be signed in to change notification settings

whuter/json-parse-bigint

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 

Repository files navigation

json-parse-bigint

convert all the big integers (≥ 16 digits) to string in JSON before JSON.parse

Features

Simple, No dependencies, Less than 1KB, based on native JSON.parse

Install

Browser:

<script src='path/to/safe-json.js'></script>

Node.js:

npm install json-parse-bigint
const JsonParseBigInt = require('json-parse-bigint');

ES6 module:

import JsonParseBigInt from 'json-parse-bigint'

Example

var json = '{ "id": 298295614833079897, "name": "test", "is_liked": 1 }';
console.log(JSON.parse(json)); // {id: 298295614833079900, name: "test", is_liked: 1}
console.log(JsonParseBigInt(json)); // {id: "298295614833079897", name: "test", is_liked: 1}
// Use Globally
JSON.parse = JsonParseBigInt
console.log(JSON.parse(json)); // {id: "298295614833079897", name: "test", is_liked: 1}

// Someone try to confuse
var json = '{ "id": 298295614833079897, "name": "\\"test\\": 298295614833079897, ", "is_liked": 1 }';
// No Problem :)
console.log(JsonParseBigInt(json)); // {id: "298295614833079897", name: ""test": 298295614833079897, ", is_liked: 1}

Licence

The MIT Licence.

About

convert all the big integers to string in JSON before JSON.parse

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published