-
Notifications
You must be signed in to change notification settings - Fork 0
JsonKit.Parse
github-actions[bot] edited this page Jul 30, 2023
·
1 revision
JsonKit.Parse
Ƭ ParseOptions: Object
Type for the options
parameter of parse.
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
|
Ƭ ParseReviverFunction: (this
: any
, key
: string
, value
: any
) => any
▸ (this
, key
, value
): any
Type for the reviver
parameter of parse.
Refer to the MDN documentation for more details.
Name | Type |
---|---|
this |
any |
key |
string |
value |
any |
any
▸ decompressString(str
): string
Decompresses a string with lz4js.decompress()
.
Name | Type | Description |
---|---|---|
str |
string |
The input string |
string
str - The decompressed string
▸ 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"
}
)
Name | Description |
---|---|
T |
Type of the output object |
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. |
T
The output object
▸ 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"
}
)
Name | Description |
---|---|
T |
Type of the output object |
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. |
T
The output object