xref: /interface/sdk-js/api/@ohos.util.json.d.ts (revision 61847f8e)
1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/**
17 * @file
18 * @kit ArkTS
19 */
20
21/**
22 * JSON is a syntax for serializing objects, arrays, numbers, strings, booleans, and null.
23 *
24 * @namespace json
25 * @syscap SystemCapability.Utils.Lang
26 * @crossplatform
27 * @atomicservice
28 * @since 12
29 */
30declare namespace json {
31  /**
32   * The type of conversion result function.
33   *
34   * @typedef { function } Transformer
35   * @param { Object } this - The object to which the parsed key value pair belongs.
36   * @param { string } key - Attribute name.
37   * @param { Object } value - The value of the parsed key value pair.
38   * @returns { Object | undefined | null } Return the modified object or undefined or null.
39   * @syscap SystemCapability.Utils.Lang
40   * @atomicservice
41   * @since 12
42   */
43  type Transformer = (this: Object, key: string, value: Object) => Object | undefined | null
44
45  /**
46   * Converts a JavaScript Object Notation (JSON) string into an Object or null.
47   *
48   * @param { string } text - A valid JSON string.
49   * @param { Transformer } [reviver] - A function that transforms the results.
50   * @param {ParseOptions} options - The config of parse.
51   * @returns { Object | null } Return an Object, array, string, number, boolean, or null value corresponding to JSON text.
52   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.
53   * @syscap SystemCapability.Utils.Lang
54   * @crossplatform
55   * @atomicservice
56   * @since 12
57   */
58  function parse(text: string, reviver?: Transformer, options?: ParseOptions): Object | null;
59
60  /**
61   * Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
62   *
63   * @param { Object } value - A JavaScript value, usually an Object or array.
64   * @param { (number | string)[] | null } [replacer] - An array of strings and numbers that acts as an approved list for selecting the object properties that will be stringify.
65   * @param { string | number } [space] - Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
66   * @returns { string } Return a JSON text.
67   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.
68   * @syscap SystemCapability.Utils.Lang
69   * @crossplatform
70   * @atomicservice
71   * @since 12
72   */
73  function stringify(value: Object, replacer?: (number | string)[] | null, space?: string | number): string
74
75  /**
76   * Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
77   *
78   * @param { Object } value - A JavaScript value, usually an Object or array.
79   * @param { Transformer } [replacer] - A function that transforms the results.
80   * @param { string | number } [space] - Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
81   * @returns { string } Return a JSON text.
82   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.
83   * @syscap SystemCapability.Utils.Lang
84   * @crossplatform
85   * @atomicservice
86   * @since 12
87   */
88  function stringify(value: Object, replacer?: Transformer, space?: string | number): string;
89
90  /**
91   * Checks whether the object parsed from a JSON string contains the property.
92   *
93   * @param { object } obj - The object parsed from a JSON string.
94   * @param { string } property - Determine whether the object contains the property.
95   * @returns { boolean } Return true if the key is in the object, otherwise return false.
96   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.
97   * @syscap SystemCapability.Utils.Lang
98   * @crossplatform
99   * @atomicservice
100   * @since 12
101   */
102  function has(obj: object, property: string): boolean;
103
104  /**
105   * Removes a property from the object parsed from a JSON string.
106   *
107   * @param { object } obj - The object parsed from a JSON string.
108   * @param { string } property - The property to be removed.
109   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.
110   * @syscap SystemCapability.Utils.Lang
111   * @crossplatform
112   * @atomicservice
113   * @since 12
114   */
115  function remove(obj: object, property: string): void;
116
117  /**
118   * Enum defining modes for handling bigint.
119   *
120   * @enum { number } BigIntMode
121   * @syscap SystemCapability.Utils.Lang
122   * @crossplatform
123   * @atomicservice
124   * @since 12
125   */
126  const enum BigIntMode {
127    /**
128     * BigInt is not supported.
129     *
130     * @syscap SystemCapability.Utils.Lang
131     * @crossplatform
132     * @atomicservice
133     * @since 12
134     */
135    DEFAULT = 0,
136    /**
137     * Parse as BigInt when number less than -(2^53 – 1) or greater than (2^53 – 1).
138     *
139     * @syscap SystemCapability.Utils.Lang
140     * @crossplatform
141     * @atomicservice
142     * @since 12
143     */
144    PARSE_AS_BIGINT = 1,
145    /**
146     * All numbers parse as BigInt.
147     *
148     * @syscap SystemCapability.Utils.Lang
149     * @crossplatform
150     * @atomicservice
151     * @since 12
152     */
153    ALWAYS_PARSE_AS_BIGINT = 2,
154  }
155
156  /**
157   * Parse's options
158   *
159   * @typedef ParseOptions
160   * @syscap SystemCapability.Utils.Lang
161   * @crossplatform
162   * @atomicservice
163   * @since 12
164   */
165  interface ParseOptions {
166    /**
167    * Enum defining modes for handling bigint.
168    * @type { BigIntMode }
169    * @syscap SystemCapability.Utils.Lang
170    * @crossplatform
171    * @atomicservice
172    * @since 12
173    */
174    bigIntMode: BigIntMode;
175  }
176}
177export default json;