# safejson

> Safely parse and stringify JSON using node style callbacks.

Latest version **1.0.1** (published 2015-01-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install safejson
pnpm add safejson
yarn add safejson
bun add safejson
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2015-01-09 |
| First published | 2014-02-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Evan Shortiss |
| Maintainers | evanshortiss |
| Keywords | safe, json, async, parse, stringify |

## Links

- npm: https://www.npmjs.com/package/safejson
- Repository: git@github.com:evanshortiss/safejson
- Homepage: https://github.com/evanshortiss/safejson
- Issues: https://github.com/evanshortiss/safejson/issues
- npm.io page: https://npm.io/package/safejson

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.1 (latest) — 2015-01-09
- 1.0.0 — 2014-05-29
- 0.0.4 — 2014-05-28
- 0.0.3 — 2014-03-23
- 0.0.1 — 2014-02-10
- 0.0.0 — 2014-02-08

## README

safejson
===

Concise library to parse and stringify JSON without the need for try catch. 
Simply use the standard Node.js pattern of providing parameters and a 
callback that takes an error as the first parameter and result as the second.

This library is nice for chaining operations using async as demonstrated below.

## Browser Support
Pretty much any browser with JSON support.
[![browser support](https://ci.testling.com/evanshortiss/safejson.png)
](https://ci.testling.com/evanshortiss/safejson)

## Install

```
npm install safejson --save
```

```
bower install safejson --save
```

## Example with Async
```javascript

var safejson = require('safejson')
  , async = require('async')
  , fs = require('fs');

exports.updateFile = function (callback) {
  async.waterfall([
    readFile,
    safejson.parse,
    doUpdate,
    safejson.stringify,
    writeFile
  ], callback); 
}

```

## Example (Parse)

```javascript
// Valid JSON object that will stringify
var VALID_OBJECT = {
  name: 'evan',
  age: 23
};

var VALID_JSON_STRING = JSON.stringify(VALID_OBJECT);


safejson.parse(VALID_JSON_STRING, function(err, json) {
  // err is null as no error would have occured due to valid input
  // json is a valid JSON object
});

```

## Example (Stringify)
```javascript
// Valid JSON object that will stringify
var VALID_OBJECT = {
  name: 'evan',
  age: 23
};

// Invalid JSON object, has a circular reference added below
var CIRCULAR_OBJECT = {
  name: 'evan',
  age: 23
};
CIRCULAR_OBJECT.cref = CIRCULAR_OBJECT;

safejson.stringify(VALID_OBJECT, function(err, json) {
  // err would be null as the object is valid json
  // json is a valid json string
});

safejson.stringify(CIRCULAR_OBJECT, function(err, str) {
  // err would be defined as the object contained a circular reference
  // str would equal null
});

```


## Configs
#### safejson.defer = {Boolean}
If true the parsing of JSON will be briefly deffered. This uses 
process.nextTick in Node.js and the appropriate browser shim 
(setTimeout for example) where necessary.

## Methods
#### safejson.stringify(value[, replacer [, space]], callback)
Does the job of JSON.stringify but handles exceptions for you. Supports all the 
usual JSON.stringify parameters, including the optional *replacer* and 
*spaces*. The last parameter must always be a callback function and is not 
optional.

#### safejson.parse(str[, reviver], callback)
Does the job of JSON.parse but handles exceptions for you. Supports all the 
usual JSON.parse parameters. The last parameter must always be a callback 
function and is not optional.

---
_Source: https://npm.io/package/safejson · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
