Type represents "composite mapper" that is returned by match
function. It
takes Value
, refines it to concrete type and maps with a corresponding
"mapper" function.
Type represents parse result.
Type represents serialization result returned by calling stringify
.
Type representing arbitrary JSON object.
An array of strings and numbers that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string.
Type predicate to refine given value
type to JSON.Array
var data = JSON.parse('[1, 2, 3]')
// Line below won't type check
data.map(String) // => [ts]: Property 'map' does not exist on type 'Value'
// Line below works due to type refinement with type predicate
if (JSON.isArray(data)) {
data.map(String) // => <Array<string>>['1','2','3']
}
Type predicate to refine given value
type to boolean
var data = JSON.parse('true')
// Line below won't type check
var x:boolean = data // => [ts]: Type 'Value' is not assignable to type 'boolean'
// Line below works due to type refinement with type predicate
if (JSON.isBoolean(data)) {
var x:boolean = data
}
Type predicate to refines given value
type to null
var data = JSON.parse('null')
// Line below won't type check
var x:null = data // => [ts]: Type 'Value' is not assignable to type 'null'
// Line below works due to type refinement with type predicate
if (JSON.isNull(data)) {
var x:null = data
}
Type predicate to refine given value
type to number
var data = JSON.parse('5.4')
// Line below won't type check
data.toFixed() // => [ts]: Property 'toFixed' does not exist on type 'Value'
// Line below works due to type refinement with type predicate
if (JSON.isNumber(data)) {
data.toFixed() // => <string>'5'
}
Type predicate to refine given value
type to JSON.Object
var data = JSON.parse('{a:{b:{c:3}}}')
// Line below won't type check
data.a // => [ts]: Property 'a' does not exist on type 'Value'
// Line below works due to type refinement with type predicate
if (JSON.isObject(data)) {
data.a // => <JSON.Value>{b:{c:3}}
}
Type predicate to refine given value
type to string
var data = JSON.parse('"Hi"')
// Line below won't type check
data.toUpperCase() // => [ts]: Property 'toUpperCase' does not exist on type 'Value'
// Line below works due to type refinement with type predicate
if (JSON.isString(data)) {
data.toUpperCase() // => <string>'HI'
}
Function provides a means to pattern match over the JSON.Value
with very
little overhead. It takes a "mapper" function for each JSON.Value
type and returns a "composite mapper" function that takes JSON.Value
,
refines it to a concrete type and then maps it with corresponding "mapper".
const show = match
( _ => 'null'
boolean => boolean.toString(),
number => number.toFixed(),
string => string.toUpperCase(),
array => array.forEach(String),
object => JSON.stringify(object))
show(JSON.parse('null')) // => null
show(JSON.parse('true')) // => true
show(JSON.parse('"Hi"')) // => "HI"
show(JSON.parse('{name: "Jack"}')) // => {name: "Jack"}
Please note that this implies that all "mapper" functions return value of
the same type. In practice this means that often return type of the
"composite mapper" function will be a type union or rather a
discriminated union that you'd have to either match over or refine
further. If that is too cumbersome for your use case consider using
isArray
, isObject
and other type predicates provided instead.
As of writing Typescript (2.3.3) has trouble infering return type of the
"composite mapper" function created by match
if it is recursive - If any
of the "mapper" functions refer to "composite mapper" directly or indirectly.
In order to overcome this limitation you'd have to type manually anotate
either "mapper" functions passed to match
or anotate resulting "composite
mapper" instead & that's where exported Match
type alias comes handy:
const toTypedString = match
( _ => '<null>null',
Parse a input
string as JSON
, optionally transform the produced value
and its properties, and return the value.
JSON.parse
Unlike built-in this returns Result<SyntaxError, Value>
where SyntaxError
is error that built-in throws on invalid JSON input and Value
is typed
JSON instead of any
.
If optional reviever
is used and it omits everything built-in version
returns undefined
which is not valid JSON. This implementation returns
Result<SyntaxError, null>
instead.
The string to parse as JSON.
If argument is passed, this prescribes how the value
originally produced by parsing is transformed, before being returned.
Result that is either ok
containing JSON value corresponding to
the given JSON text or error
containing SyntaxError
describing why
input is not a valid JSON.
Function serializes a JSON.Value to a string.
JSON.stringify
Unlike built-in version this function returns SerializationResult
which is
either ok
containing serialized string or an error
containing an Error
that built-in version would have thrown.
Unlike built-in version if this function succeeds (returns ok) it's result
value is guaranteed to parse. There is a catch though built-in returns
undefined
if replacer
omits everything. In such case this function
returns string "null"
which would parse to null
which seems to better
capture the fact that JSON.Value
got serailzed to nothing.
JSON value to be serialized.
Function serializes a JSON.Value to a string.
JSON.stringify
Unlike built-in version this function returns SerializationResult
which is
either ok
containing serialized string or an error
containing an Error
that built-in version would have thrown.
Unlike built-in version if this function succeeds (returns ok) it's result
value is guaranteed to parse. There is a catch though built-in returns
undefined
if replacer
omits everything. In such case this function
returns string "null"
which would parse to null
which seems to better
capture the fact that JSON.Value
got serailzed to nothing.
JSON value to be serialized.
An array of strings and numbers that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string.
Function serializes a JSON.Value to a string.
JSON.stringify
Unlike built-in version this function returns SerializationResult
which is
either ok
containing serialized string or an error
containing an Error
that built-in version would have thrown.
Unlike built-in version if this function succeeds (returns ok) it's result
value is guaranteed to parse. There is a catch though built-in returns
undefined
if replacer
omits everything. In such case this function
returns string "null"
which would parse to null
which seems to better
capture the fact that JSON.Value
got serailzed to nothing.
JSON value to be serialized.
Replacing values of members by results of calling given
replacer
function with member name and value.
Function serializes a JSON.Value to a string.
JSON.stringify
Unlike built-in version this function returns SerializationResult
which is
either ok
containing serialized string or an error
containing an Error
that built-in version would have thrown.
Unlike built-in version if this function succeeds (returns ok) it's result
value is guaranteed to parse. There is a catch though built-in returns
undefined
if replacer
omits everything. In such case this function
returns string "null"
which would parse to null
which seems to better
capture the fact that JSON.Value
got serailzed to nothing.
JSON value to be serialized.
A function that alters the behavior of the stringification process, or an array of string and number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string.
A string
used to insert white space into the output JSON
string for readability purposes. Geven string (or the first 10 characters
of the string, if it's longer than that) is used as white space.
Function serializes a JSON.Value to a string.
JSON.stringify
Unlike built-in version this function returns SerializationResult
which is
either ok
containing serialized string or an error
containing an Error
that built-in version would have thrown.
Unlike built-in version if this function succeeds (returns ok) it's result
value is guaranteed to parse. There is a catch though built-in returns
undefined
if replacer
omits everything. In such case this function
returns string "null"
which would parse to null
which seems to better
capture the fact that JSON.Value
got serailzed to nothing.
JSON value to be serialized.
A function that alters the behavior of the stringification process, or an array of string and number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string.
A number
that's used to insert white space into the output
JSON string for readability purposes. Number indicates the number of space
characters to use as white space; this number is capped at 10 (if it is
greater, the value is just 10). Values less than 1 indicate that no space
should be used.
Generated using TypeDoc
Type extends built-in
Error
with optinalcoughtError
field. The reason is in JS you can throw and catch anything including numbers etc... But to have somewhat typed API we're normalizing nonError
exceptions by creating a newError
and storing actually caught one undercoughtError
field.