Skip to content

JsonKit.Parse

github-actions[bot] edited this page Jul 30, 2023 · 1 revision

Namespace: Parse

JsonKit.Parse

Table of contents

Type Aliases

Functions

Type Aliases

ParseOptions

Ƭ ParseOptions: Object

Type for the options parameter of parse.

Type declaration

Name Type Description
decompress? boolean | { enable: boolean } Specifies if the input string is compressed with lz4js.compress(). Remarks Will automatically disable if the decompression fails. Default Value true
extended? boolean | { enable: boolean ; relaxed?: boolean } Specifies if the input string is in JSON or EJSON format, additional options to be passed into bson.EJSON.parse() can be supplied with the long form (only effective if enable is true. Default Value false
unminify? boolean | { enable: boolean ; keyMap?: Record<string, string> } Specifies if the input string is created with the minify option enabled in stringify, a custom key map (shortened:original) can be supplied with the long form (only effective if enable is true. Remarks Will automatically disable if the unminifcation fails. Default Value true

Defined in

parse.ts:22


ParseReviverFunction

Ƭ ParseReviverFunction: (this: any, key: string, value: any) => any

Type declaration

▸ (this, key, value): any

Type for the reviver parameter of parse. Refer to the MDN documentation for more details.

Parameters
Name Type
this any
key string
value any
Returns

any

Defined in

parse.ts:12

Functions

decompressString

decompressString(str): string

Decompresses a string with lz4js.decompress().

Parameters

Name Type Description
str string The input string

Returns

string

str - The decompressed string

Defined in

parse.ts:352


parse

parse<T>(text, reviver?, options?, typeGuard?): T

Turns the input string into an object.

Remarks

With the custom options, the input string can be either

  • a JSON string (identical to JSON.parse())
  • an EJSON string (identical to bson.EJSON.parse())
  • a minified version of either of the above, where some or specified keys are replaced with a shorter identifier
  • a compressed version (by lz4js.compress()) of either of the above

Example

type Foo {
  long_key: string
}

parse<Foo>(
  "{\"lk\":\"A Large Object\"}",
  (key, val) => {
    if (key === "lk") {
      return "A Modified Large Object"
    }
    return val
  },
  {
    extended: false,
    unminify: { enable: true, keyMap: { "lk": "long_key" } },
    decompress: false
  },
  (obj: any): obj is Foo => {
    const _obj: Partial<Foo> | null | undefined = obj
    return typeof _obj?.long_key === "string"
  }
)

Type parameters

Name Description
T Type of the output object

Parameters

Name Type Description
text string The input string
reviver? null | ParseReviverFunction The reviver parameter of JSON.parse()
options? null | ParseOptions The custom options, refer to ParseOptions for details
typeGuard? TypeGuardFunction<T> The type guard function, ensures that the output object is of the expected type, refer to TypeGuardFunction for an example.

Returns

T

The output object

Defined in

parse.ts:185

parse<T>(text, options?, typeGuard?): T

Turns the input string into an object.

Remarks

With the custom options, the input string can be either

  • a JSON string (identical to JSON.parse())
  • an EJSON string (identical to bson.EJSON.parse())
  • a minified version of either of the above, where some or specified keys are replaced with a shorter identifier
  • a compressed version (by lz4js.compress()) of either of the above

Example

type Foo {
  long_key: string
}

parse<Foo>(
  "{\"lk\":\"A Large Object\"}",
  {
    extended: false,
    unminify: { enable: true, keyMap: { "lk": "long_key" } },
    decompress: false
  },
  (obj: any): obj is Foo => {
    const _obj: Partial<Foo> | null | undefined = obj
    return typeof _obj?.long_key === "string"
  }
)

Type parameters

Name Description
T Type of the output object

Parameters

Name Type Description
text string The input string
options? null | ParseOptions The custom options, refer to ParseOptions for details
typeGuard? TypeGuardFunction<T> The type guard function, ensures that the output object is of the expected type, refer to TypeGuardFunction for an example.

Returns

T

The output object

Defined in

parse.ts:230