50 lines
1.4 KiB
JavaScript
50 lines
1.4 KiB
JavaScript
|
/**
|
||
|
* @since 2.10.0
|
||
|
*/
|
||
|
import { tryCatch } from './Either';
|
||
|
import { identity } from './function';
|
||
|
/**
|
||
|
* Converts a JavaScript Object Notation (JSON) string into a `Json` type.
|
||
|
*
|
||
|
* @example
|
||
|
* import * as J from 'fp-ts/Json'
|
||
|
* import * as E from 'fp-ts/Either'
|
||
|
* import { pipe } from 'fp-ts/function'
|
||
|
*
|
||
|
* assert.deepStrictEqual(pipe('{"a":1}', J.parse), E.right({ a: 1 }))
|
||
|
* assert.deepStrictEqual(pipe('{"a":}', J.parse), E.left(new SyntaxError('Unexpected token } in JSON at position 5')))
|
||
|
*
|
||
|
* @since 2.10.0
|
||
|
*/
|
||
|
export var parse = function (s) { return tryCatch(function () { return JSON.parse(s); }, identity); };
|
||
|
/**
|
||
|
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
|
||
|
*
|
||
|
* @example
|
||
|
* import * as E from 'fp-ts/Either'
|
||
|
* import * as J from 'fp-ts/Json'
|
||
|
* import { pipe } from 'fp-ts/function'
|
||
|
*
|
||
|
* assert.deepStrictEqual(J.stringify({ a: 1 }), E.right('{"a":1}'))
|
||
|
* const circular: any = { ref: null }
|
||
|
* circular.ref = circular
|
||
|
* assert.deepStrictEqual(
|
||
|
* pipe(
|
||
|
* J.stringify(circular),
|
||
|
* E.mapLeft(e => e instanceof Error && e.message.includes('Converting circular structure to JSON'))
|
||
|
* ),
|
||
|
* E.left(true)
|
||
|
* )
|
||
|
*
|
||
|
* @since 2.10.0
|
||
|
*/
|
||
|
export var stringify = function (a) {
|
||
|
return tryCatch(function () {
|
||
|
var s = JSON.stringify(a);
|
||
|
if (typeof s !== 'string') {
|
||
|
throw new Error('Converting unsupported structure to JSON');
|
||
|
}
|
||
|
return s;
|
||
|
}, identity);
|
||
|
};
|