Skip to content

arminrosu/json-slim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON-Slim

Build Status Dependencies js-standard-style

Minify JSON better than JSON.stringify().

It achieves this by converting Numbers, number Strings and Booleans to a shorter form. Unicode characters are converted automatically by JSON.stringify(), which is used internally.

You should be aware of the potential type conversion json-slim performs, as type checks in your application might fail.

Installation

npm i json-slim

Example

var slim = require('json-slim');

var input = {
  boolean: true,
  number: '123000',
  unicode: '\u03B1 and \u03A9',
  customField: 'Tom Marvolo Riddle'
}

var json = slim(input, {
  // report how much space it saved
  report: true,
  // You can add your own minifiers too
  minifiers: [
    {
      // RegExp Pattern
      pattern: '"Tom Marvolo Riddle"',
      replace: function (string) {
        return '"I am Lord Voldemort"'
      }
    }
  ]
})

console.log(json);

Output

Report. Note: the bigger the original JSON, the bigger the savings are.

JSON-Slim: 94% of original.

Result

{"boolean":1,"number":123e3,"unicode":"α and Ω","customField":"I am Lord Voldemort"}

Even less?

Check out json-lean if your JSON requests have the same object structure.

About

Minify JSON better than JSON.stringify()

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published