JSON overview

Various functions to aid in working with JSON.

Added in v0.1.0


Table of contents


0 Types

JSONString (type alias)

Newtype representing stringified JSON.

Signature

export type JSONString = Newtype<JSONStringSymbol, string>
type JSONString = Newtype JSONStringSymbol string

Example

import { JSONString, stringifyPrimitive } from 'fp-ts-std/JSON'

const safeToParse: JSONString = stringifyPrimitive('foo')

Added in v0.5.0

3 Functions

parse

Parse a string as JSON. The Json type on the right side comes from fp-ts and is a union of all possible parsed types.

Signature

export declare const parse: <E>(f: (e: SyntaxError) => E) => (x: string) => Either<E, Json>
parse :: (SyntaxError -> e) -> string -> Either e Json

Example

import { parse } from 'fp-ts-std/JSON'
import * as E from 'fp-ts/Either'
import { constant } from 'fp-ts/function'

const f = parse(constant('e'))

const valid = '"abc"'
const invalid = 'abc'

assert.deepStrictEqual(f(valid), E.right('abc'))
assert.deepStrictEqual(f(invalid), E.left('e'))

Added in v0.1.0

parseO

Parse a string as JSON, returning an Option.

Signature

export declare const parseO: (stringified: string) => Option<unknown>
parseO :: string -> Option unknown

Example

import { parseO } from 'fp-ts-std/JSON'
import * as O from 'fp-ts/Option'

const valid = '"abc"'
const invalid = 'abc'

assert.deepStrictEqual(parseO(valid), O.some('abc'))
assert.deepStrictEqual(parseO(invalid), O.none)

Added in v0.1.0

stringify

Stringify some arbitrary data.

Signature

export declare const stringify: <E>(f: (e: TypeError) => E) => (x: unknown) => Either<E, JSONString>
stringify :: (TypeError -> e) -> unknown -> Either e JSONString

Example

import { stringify } from 'fp-ts-std/JSON'
import * as E from 'fp-ts/Either'
import { constant } from 'fp-ts/function'

const f = stringify(constant('e'))

const valid = 'abc'
const invalid = () => {}

assert.deepStrictEqual(f(valid), E.right('"abc"'))
assert.deepStrictEqual(f(invalid), E.left('e'))

Added in v0.1.0

stringifyO

Stringify some arbitrary data, returning an Option.

Signature

export declare const stringifyO: (data: unknown) => Option<JSONString>
stringifyO :: unknown -> Option JSONString

Example

import { stringifyO } from 'fp-ts-std/JSON'
import * as O from 'fp-ts/Option'

const valid = 'abc'
const invalid = () => {}

assert.deepStrictEqual(stringifyO(valid), O.some('"abc"'))
assert.deepStrictEqual(stringifyO(invalid), O.none)

Added in v0.1.0

stringifyPrimitive

Stringify a primitive value with no possibility of failure.

Signature

export declare const stringifyPrimitive: (x: string | number | boolean | null) => JSONString
stringifyPrimitive :: string | number | boolean | null -> JSONString

Example

import { stringifyPrimitive } from 'fp-ts-std/JSON'

assert.strictEqual(stringifyPrimitive('abc'), '"abc"')

Added in v0.1.0

unJSONString

Unwrap a JSONString newtype back to its underlying string representation.

Signature

export declare const unJSONString: (x: JSONString) => string
unJSONString :: JSONString -> string

Added in v0.6.0

unstringify

Parse a string as JSON. This is safe provided there have been no shenanigans with the JSONString newtype.

Signature

export declare const unstringify: (x: JSONString) => unknown
unstringify :: JSONString -> unknown

Example

import { unstringify, stringifyPrimitive } from 'fp-ts-std/JSON'
import { flow } from 'fp-ts/function'

const f = flow(stringifyPrimitive, unstringify)

assert.strictEqual(f('abc'), 'abc')

Added in v0.5.0