Options
All
  • Public
  • Public/Protected
  • All
Menu

Index

Type aliases

Boolean

Boolean: boolean

CaughtError

CaughtError: Error & object

Type extends built-in Error with optinal coughtError field. The reason is in JS you can throw and catch anything including numbers etc... But to have somewhat typed API we're normalizing non Error exceptions by creating a new Error and storing actually caught one under coughtError field.

Match

Match: function

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 declaration

    • Parameters

      Returns a

Null

Null: null

Number

Number: number

ParseResult

ParseResult: Result<SyntaxError, Value>

Type represents parse result.

Replacer

Replacer: function

Type declaration

    • Parameters

      • key: string

        key of the member to be replaced

      • value: Value

        value of the member to be replaced.

      Returns void | Value

      If void is returned member will be ommited otherwise it will be substituted with a returned member.

Reviver

Reviver: function

Type declaration

    • Parameters

      • key: string

        name of the member being transformed

      • value: Value

        value of the member being transformed

      Returns void | Value

      transformed value or void to be omitted.

SerializationResult

SerializationResult: Result<CaughtError, string>

Type represents serialization result returned by calling stringify.

String

String: string

Value

Type representing arbitrary JSON object.

WhiteList

WhiteList: (string | number)[]

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.

Variables

parseJSON

parseJSON: parse

stringifyJSON

stringifyJSON: stringify

Functions

isArray

  • isArray(value: Value): boolean
  • 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']
    }
    

    Parameters

    Returns boolean

isBoolean

  • isBoolean(value: any): boolean
  • 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
    }
    

    Parameters

    • value: any

    Returns boolean

isNull

  • isNull(value: any): boolean
  • 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
    }
    

    Parameters

    • value: any

    Returns boolean

isNumber

  • isNumber(value: any): boolean
  • 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'
    }
    

    Parameters

    • value: any

    Returns boolean

isObject

  • isObject(value: Value): boolean
  • 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}}
    }
    

    Parameters

    Returns boolean

isString

  • isString(value: any): boolean
  • 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'
    }
    

    Parameters

    • value: any

    Returns boolean

match

  • match<a>(Null: function, Boolean: function, Number: function, String: function, Array: function, Object: function): Match<a>
  • 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.

    Known limitation

    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',
    

    Type parameters

    • a

    Parameters

    • Null: function
        • Parameters

          Returns a

    • Boolean: function
    • Number: function
    • String: function
    • Array: function
        • Parameters

          Returns a

    • Object: function

    Returns Match<a>

parse

  • Parse a input string as JSON, optionally transform the produced value and its properties, and return the value.

    Difference from built-in 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.

    Parameters

    • input: string

      The string to parse as JSON.

    • Optional reviver: Reviver

      If argument is passed, this prescribes how the value originally produced by parsing is transformed, before being returned.

    Returns ParseResult

    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.

stringify

  • Function serializes a JSON.Value to a string.

    Difference from built-in 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.

    Parameters

    • value: Value

      JSON value to be serialized.

    Returns SerializationResult

  • Function serializes a JSON.Value to a string.

    Difference from built-in 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.

    Parameters

    • value: Value

      JSON value to be serialized.

    • whiteList: WhiteList

      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.

    Returns SerializationResult

  • Function serializes a JSON.Value to a string.

    Difference from built-in 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.

    Parameters

    • value: Value

      JSON value to be serialized.

    • replacer: Replacer

      Replacing values of members by results of calling given replacer function with member name and value.

    Returns SerializationResult

  • Function serializes a JSON.Value to a string.

    Difference from built-in 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.

    Parameters

    • value: Value

      JSON value to be serialized.

    • replacerOrWhiteList: null | void | WhiteList | Replacer

      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.

    • space: 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.

    Returns SerializationResult

  • Function serializes a JSON.Value to a string.

    Difference from built-in 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.

    Parameters

    • value: Value

      JSON value to be serialized.

    • replacerOrWhiteList: void | null | WhiteList | Replacer

      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.

    • spaceSize: number

      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.

    Returns SerializationResult

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc