https://github.com/rogeriopvl/json-fns
🔗 JSON parser/stringifier that supports functions and regexp
https://github.com/rogeriopvl/json-fns
functions json parse regexp serialize stringify
Last synced: 5 months ago
JSON representation
🔗 JSON parser/stringifier that supports functions and regexp
- Host: GitHub
- URL: https://github.com/rogeriopvl/json-fns
- Owner: rogeriopvl
- License: mit
- Created: 2019-08-16T17:39:17.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2022-02-12T12:30:21.000Z (over 4 years ago)
- Last Synced: 2025-10-20T21:23:36.046Z (8 months ago)
- Topics: functions, json, parse, regexp, serialize, stringify
- Language: JavaScript
- Size: 109 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# json-fns
[](https://travis-ci.org/rogeriopvl/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
```javascript
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
```javascript
const JSONfns = require('json-fns')
const serializedObj = '{ "someFn": "function (x) { console.log(x) }" }'
JSONfns.parse(serializedObj)
```
### Browser
Just include it in your html:
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 format
- `space`(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 to `JSON.stringify` ([more info](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Parameters))
- `JSONfns.parse(str)`
- `str`: the serialized object in string format
## Credits
json-fns is an improved fork of [json-fn](https://github.com/vkiryukhin/jsonfn) that seems to be abandoned for some time.
## LICENSE
Please check the [LICENSE](https://github.com/rogeriopvl/json-fns/blob/master/LICENSE) file.