Skip to main content

Json

Utilities

Utilities

Json

Signature

export type Json = boolean | number | string | null | JsonArray | JsonRecord

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

JsonArray

Signature

export interface JsonArray extends ReadonlyArray<Json> {}

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

JsonRecord

Signature

export interface JsonRecord {
readonly [key: string]: Json
}

Details

  • Added in 0.1.0

License

  • MIT – Copyright (c) 2017-present Giulio Canti

parse

Converts a JavaScript Object Notation (JSON) string into a Json type.

Signature

export declare const parse: (s: string) => Either<unknown, Json>

Details

  • Added in 0.1.0

Example

import * as J from '@fp-tx/core/Json'
import * as E from '@fp-tx/core/Either'
import { pipe } from '@fp-tx/core/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')),
)

License

  • MIT – Copyright (c) 2017-present Giulio Canti

stringify

Converts a JavaScript value to a JavaScript Object Notation (JSON) string.

Signature

export declare const stringify: <A>(a: A) => Either<unknown, string>

Details

  • Added in 0.1.0

Example

import * as E from '@fp-tx/core/Either'
import * as J from '@fp-tx/core/Json'
import { pipe } from '@fp-tx/core/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),
)

License

  • MIT – Copyright (c) 2017-present Giulio Canti