Skip to content

JsonKit.Stringify

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

Namespace: Stringify

JsonKit.Stringify

Table of contents

Type Aliases

Functions

Type Aliases

StringifyOptions

Ƭ StringifyOptions: Object

Type for the options parameter of stringify.

Type declaration

Name Type Description
compress? boolean | { enable: boolean } Determines if the output string will be compressed with lz4js.compress(). Default Value false
extended? boolean | { enable: boolean ; relaxed?: boolean } Determines if the output string is in JSON or EJSON format, additional options to be passed into bson.EJSON.stringify() can be supplied with the long form (only effective if enable is true. Default Value false
minify? boolean | { enable: boolean ; keyMap?: Record<string, string> } Determines if the output string will have some of the keys replaced with a shorter identifier, a custom key map (original:shortened) can be supplied with the long form (only effective if enable is true. Default Value false

Defined in

stringify.ts:37


StringifyReplacerArray

Ƭ StringifyReplacerArray: (string | number)[]

Array type for the replacer parameter of stringify. Refer to the MDN documentation for more details.

Defined in

stringify.ts:16


StringifyReplacerFunction

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

Type declaration

▸ (this, key, value): any

Function type for the replacer parameter of stringify. Refer to the MDN documentation for more details.

Parameters
Name Type
this any
key string
value any
Returns

any

Defined in

stringify.ts:23

Functions

compressString

compressString(str): string

Compresses a string with lz4js.compress().

Parameters

Name Type Description
str string The input string

Returns

string

str - The compressed string

Defined in

stringify.ts:407


stringify

stringify(obj, replacer?, space?, options?): string

Turns the input object into a string.

Remarks

With the custom options, the output string can be either

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

Example

stringify(
  { long_key: "A Large Object" },
  (key, val) => {
    if (key === "long_key") {
      return "A Modified Large Object"
    }
    return val
  },
  2,
  {
    extended: false,
    minify: { enable: true, keyMap: { long_key: "lk" } },
    compress: false
  }
)

Parameters

Name Type Description
obj any The input object
replacer? null | StringifyReplacerArray | StringifyReplacerFunction The replacer parameter of JSON.stringify()
space? null | string | number The space parameter of JSON.stringify()
options? null | StringifyOptions The custom options, refer to StringifyOptions for details

Returns

string

The output string

Defined in

stringify.ts:186

stringify(obj, options?): string

Turns the input object into a string.

Remarks

With the custom options, the output string can be either

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

Example

stringify(
  { long_key: "A Large Object" },
  {
    extended: false,
    minify: { enable: true, keyMap: { long_key: "lk" } },
    compress: false
  }
)

Parameters

Name Type Description
obj any The input object
options? null | StringifyOptions The custom options, refer to StringifyOptions for details

Returns

string

The output string

Defined in

stringify.ts:221