json-fns
v1.0.0
Published
JSON parser/stringifier that supports functions and regexp
Downloads
3,123
Maintainers
Readme
json-fns
About
json-fns is a JSON parser/stringifier that supports functions and regexp.
json-fns uses the native JSON parse
and stringify
methods and extends them to support functions and regular expressions.
Install
npm i json-fns
Usage
Node
Stringify
const JSONfns = require('json-fns')
const obj = {
someFn: function (x) { console.log(x) },
someRegexp: /foobar/
}
JSONfns.stringify(obj);
Note: objects serialized by JSONfns.stringify
can only be restored back completely with JSONfns.parse
. Using JSON.parse
, although it works, will result in incomplete/incorrect objects.
Parse
const JSONfns = require('json-fns')
const serializedObj = '{ "someFn": "function (x) { console.log(x) }" }'
JSONfns.parse(serializedObj)
Browser
Just include it in your html:
<script type="text/javascript" src="https://unpkg.com/json-fns/json-fns.js">
The JSONfns
object will be available in your global scope.
AMD is also supported.
API
JSONfns.serialize(obj, space)
obj
: the object to serialize in JSON formatspace
(optional) a String or Number object that's used to insert white space into the output JSON string for readability purposes. This is the exact same parameter passed toJSON.stringify
(more info)
JSONfns.parse(str)
str
: the serialized object in string format
Credits
json-fns is an improved fork of json-fn that seems to be abandoned for some time.
LICENSE
Please check the LICENSE file.