|
| 1 | +[@kit-p/json-kit - v2.0.0](../README.md) / [Exports](../modules.md) / [JsonKit](JsonKit.md) / Parse |
| 2 | + |
| 3 | +# Namespace: Parse |
| 4 | + |
| 5 | +[JsonKit](JsonKit.md).Parse |
| 6 | + |
| 7 | +## Table of contents |
| 8 | + |
| 9 | +### Type Aliases |
| 10 | + |
| 11 | +- [ParseOptions](JsonKit.Parse.md#parseoptions) |
| 12 | +- [ParseReviverFunction](JsonKit.Parse.md#parsereviverfunction) |
| 13 | + |
| 14 | +### Functions |
| 15 | + |
| 16 | +- [decompressString](JsonKit.Parse.md#decompressstring) |
| 17 | +- [parse](JsonKit.Parse.md#parse) |
| 18 | + |
| 19 | +## Type Aliases |
| 20 | + |
| 21 | +### ParseOptions |
| 22 | + |
| 23 | +Ƭ **ParseOptions**: `Object` |
| 24 | + |
| 25 | +Type for the `options` parameter of [parse](JsonKit.Parse.md#parse). |
| 26 | + |
| 27 | +#### Type declaration |
| 28 | + |
| 29 | +| Name | Type | Description | |
| 30 | +| :------ | :------ | :------ | |
| 31 | +| `decompress?` | `boolean` \| { `enable`: `boolean` } | Specifies if the input string is compressed with [`lz4js.compress()`](https://github.com/Benzinga/lz4js/blob/73728a9c3c6a417ab9ce622fa112dc1cf04b00fd/lz4.js#L557). **`Remarks`** Will automatically disable if the decompression fails. **`Default Value`** `true` | |
| 32 | +| `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()`](https://github.com/mongodb/js-bson#ejsonparsetext-options) can be supplied with the long form (only effective if `enable` is `true`. **`Default Value`** `false` | |
| 33 | +| `unminify?` | `boolean` \| { `enable`: `boolean` ; `keyMap?`: `Record`<`string`, `string`\> } | Specifies if the input string is created with the `minify` option enabled in [stringify](JsonKit.Stringify.md#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` | |
| 34 | + |
| 35 | +#### Defined in |
| 36 | + |
| 37 | +[parse.ts:22](https://github.com/Kit-p/json-kit/blob/af9e51f/src/parse.ts#L22) |
| 38 | + |
| 39 | +___ |
| 40 | + |
| 41 | +### ParseReviverFunction |
| 42 | + |
| 43 | +Ƭ **ParseReviverFunction**: (`this`: `any`, `key`: `string`, `value`: `any`) => `any` |
| 44 | + |
| 45 | +#### Type declaration |
| 46 | + |
| 47 | +▸ (`this`, `key`, `value`): `any` |
| 48 | + |
| 49 | +Type for the `reviver` parameter of [parse](JsonKit.Parse.md#parse). |
| 50 | +Refer to [the MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#the_reviver_parameter) for more details. |
| 51 | + |
| 52 | +##### Parameters |
| 53 | + |
| 54 | +| Name | Type | |
| 55 | +| :------ | :------ | |
| 56 | +| `this` | `any` | |
| 57 | +| `key` | `string` | |
| 58 | +| `value` | `any` | |
| 59 | + |
| 60 | +##### Returns |
| 61 | + |
| 62 | +`any` |
| 63 | + |
| 64 | +#### Defined in |
| 65 | + |
| 66 | +[parse.ts:12](https://github.com/Kit-p/json-kit/blob/af9e51f/src/parse.ts#L12) |
| 67 | + |
| 68 | +## Functions |
| 69 | + |
| 70 | +### decompressString |
| 71 | + |
| 72 | +▸ **decompressString**(`str`): `string` |
| 73 | + |
| 74 | +Decompresses a string with [`lz4js.decompress()`](https://github.com/Benzinga/lz4js/blob/73728a9c3c6a417ab9ce622fa112dc1cf04b00fd/lz4.js#L538). |
| 75 | + |
| 76 | +#### Parameters |
| 77 | + |
| 78 | +| Name | Type | Description | |
| 79 | +| :------ | :------ | :------ | |
| 80 | +| `str` | `string` | The input string | |
| 81 | + |
| 82 | +#### Returns |
| 83 | + |
| 84 | +`string` |
| 85 | + |
| 86 | +str - The decompressed string |
| 87 | + |
| 88 | +#### Defined in |
| 89 | + |
| 90 | +[parse.ts:352](https://github.com/Kit-p/json-kit/blob/af9e51f/src/parse.ts#L352) |
| 91 | + |
| 92 | +___ |
| 93 | + |
| 94 | +### parse |
| 95 | + |
| 96 | +▸ **parse**<`T`\>(`text`, `reviver?`, `options?`, `typeGuard?`): `T` |
| 97 | + |
| 98 | +Turns the input string into an object. |
| 99 | + |
| 100 | +**`Remarks`** |
| 101 | + |
| 102 | +With the custom options, the input string can be either |
| 103 | + - a JSON string (identical to [`JSON.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse)) |
| 104 | + - an EJSON string (identical to [`bson.EJSON.parse()`](https://github.com/mongodb/js-bson#ejsonparsetext-options)) |
| 105 | + - a minified version of either of the above, where some or specified keys are replaced with a shorter identifier |
| 106 | + - a compressed version (by [`lz4js.compress()`](https://github.com/Benzinga/lz4js/blob/73728a9c3c6a417ab9ce622fa112dc1cf04b00fd/lz4.js#L557)) of either of the above |
| 107 | + |
| 108 | +**`Example`** |
| 109 | + |
| 110 | +```ts |
| 111 | +type Foo { |
| 112 | + long_key: string |
| 113 | +} |
| 114 | + |
| 115 | +parse<Foo>( |
| 116 | + "{\"lk\":\"A Large Object\"}", |
| 117 | + (key, val) => { |
| 118 | + if (key === "lk") { |
| 119 | + return "A Modified Large Object" |
| 120 | + } |
| 121 | + return val |
| 122 | + }, |
| 123 | + { |
| 124 | + extended: false, |
| 125 | + unminify: { enable: true, keyMap: { "lk": "long_key" } }, |
| 126 | + decompress: false |
| 127 | + }, |
| 128 | + (obj: any): obj is Foo => { |
| 129 | + const _obj: Partial<Foo> | null | undefined = obj |
| 130 | + return typeof _obj?.long_key === "string" |
| 131 | + } |
| 132 | +) |
| 133 | +``` |
| 134 | + |
| 135 | +#### Type parameters |
| 136 | + |
| 137 | +| Name | Description | |
| 138 | +| :------ | :------ | |
| 139 | +| `T` | Type of the output object | |
| 140 | + |
| 141 | +#### Parameters |
| 142 | + |
| 143 | +| Name | Type | Description | |
| 144 | +| :------ | :------ | :------ | |
| 145 | +| `text` | `string` | The input string | |
| 146 | +| `reviver?` | ``null`` \| [`ParseReviverFunction`](JsonKit.Parse.md#parsereviverfunction) | The `reviver` parameter of [`JSON.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#the_reviver_parameter) | |
| 147 | +| `options?` | ``null`` \| [`ParseOptions`](JsonKit.Parse.md#parseoptions) | The custom options, refer to [ParseOptions](JsonKit.Parse.md#parseoptions) for details | |
| 148 | +| `typeGuard?` | [`TypeGuardFunction`](JsonKit.Types.md#typeguardfunction)<`T`\> | The type guard function, ensures that the output object is of the expected type, refer to [TypeGuardFunction](JsonKit.Types.md#typeguardfunction) for an example. | |
| 149 | + |
| 150 | +#### Returns |
| 151 | + |
| 152 | +`T` |
| 153 | + |
| 154 | +The output object |
| 155 | + |
| 156 | +#### Defined in |
| 157 | + |
| 158 | +[parse.ts:185](https://github.com/Kit-p/json-kit/blob/af9e51f/src/parse.ts#L185) |
| 159 | + |
| 160 | +▸ **parse**<`T`\>(`text`, `options?`, `typeGuard?`): `T` |
| 161 | + |
| 162 | +Turns the input string into an object. |
| 163 | + |
| 164 | +**`Remarks`** |
| 165 | + |
| 166 | +With the custom options, the input string can be either |
| 167 | + - a JSON string (identical to [`JSON.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse)) |
| 168 | + - an EJSON string (identical to [`bson.EJSON.parse()`](https://github.com/mongodb/js-bson#ejsonparsetext-options)) |
| 169 | + - a minified version of either of the above, where some or specified keys are replaced with a shorter identifier |
| 170 | + - a compressed version (by [`lz4js.compress()`](https://github.com/Benzinga/lz4js/blob/73728a9c3c6a417ab9ce622fa112dc1cf04b00fd/lz4.js#L557)) of either of the above |
| 171 | + |
| 172 | +**`Example`** |
| 173 | + |
| 174 | +```ts |
| 175 | +type Foo { |
| 176 | + long_key: string |
| 177 | +} |
| 178 | + |
| 179 | +parse<Foo>( |
| 180 | + "{\"lk\":\"A Large Object\"}", |
| 181 | + { |
| 182 | + extended: false, |
| 183 | + unminify: { enable: true, keyMap: { "lk": "long_key" } }, |
| 184 | + decompress: false |
| 185 | + }, |
| 186 | + (obj: any): obj is Foo => { |
| 187 | + const _obj: Partial<Foo> | null | undefined = obj |
| 188 | + return typeof _obj?.long_key === "string" |
| 189 | + } |
| 190 | +) |
| 191 | +``` |
| 192 | + |
| 193 | +#### Type parameters |
| 194 | + |
| 195 | +| Name | Description | |
| 196 | +| :------ | :------ | |
| 197 | +| `T` | Type of the output object | |
| 198 | + |
| 199 | +#### Parameters |
| 200 | + |
| 201 | +| Name | Type | Description | |
| 202 | +| :------ | :------ | :------ | |
| 203 | +| `text` | `string` | The input string | |
| 204 | +| `options?` | ``null`` \| [`ParseOptions`](JsonKit.Parse.md#parseoptions) | The custom options, refer to [ParseOptions](JsonKit.Parse.md#parseoptions) for details | |
| 205 | +| `typeGuard?` | [`TypeGuardFunction`](JsonKit.Types.md#typeguardfunction)<`T`\> | The type guard function, ensures that the output object is of the expected type, refer to [TypeGuardFunction](JsonKit.Types.md#typeguardfunction) for an example. | |
| 206 | + |
| 207 | +#### Returns |
| 208 | + |
| 209 | +`T` |
| 210 | + |
| 211 | +The output object |
| 212 | + |
| 213 | +#### Defined in |
| 214 | + |
| 215 | +[parse.ts:230](https://github.com/Kit-p/json-kit/blob/af9e51f/src/parse.ts#L230) |
0 commit comments