 [](https://opensource.org/license
npm install babel-plugin-object-to-json-parse!release


This repository is inspired by this article
> As long as the JSON string is only evaluated once, the JSON.parse approach is much faster compared to the JavaScript object literal, especially for cold loads.
This plugin converts from object literal to JSON.parse
``js
// before
const data = { foo: 42, bar: 1337 };
// after
const data = JSON.parse('{"foo":42,"bar":1337}');
`
`sh`
$ npm install babel-plugin-object-to-json-parse -D
or
$ yarn add babel-plugin-object-to-json-parse -D
`json`
{
"plugins": ["object-to-json-parse"]
}
(number, defaults to 1024)The
minJSONStringSize option will prevent the plugin from replacing an expression if the length of the JSON string given to JSON.parse is smaller than minJSONStringSize. For example, the following ensures all replacements have a string size of at least 1kb.`json
{
"plugins": [
["object-to-json-parse", {
"minJSONStringSize": 1024
}]
]
}
`Caution!!
$3
I just made this plugin for my understanding about AST and babel plugin.$3
I decided not to support partially JSON expression like below.
> Partially JSON expressions such as [notValid, {isValid:true}] ensuring {isValid:true} is transformed.
`
const data = { bar: invalid_object, foo: 'foo' }
ā
const data = { bar: invalid_object, JSON.parse('{"foo": "foo"}')}
`This is because I think most large objects are not partially JSON expressions. JSON.parse() is much faster in the case that object is 10 kB or larger. Converting small object to JSON.parse expression is not meaningful.
$3
I don't care about some backwards compatibilities like this issue.
Development
Any contributions are welcomed from everyone!!
$3
`sh
$ git clone git@github.com:nissy-dev/babel-plugin-object-to-json-parse.git
$ cd babel-plugin-object-to-json-parse
$ yarn install
`$3
`sh
// build
$ yarn build// test
$ yarn test
``