• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
53   * 1.Mandatory parameters are left unspecified;
54   * 2.Incorrect parameter types; 3.Parameter verification failed.
55   * @syscap SystemCapability.Utils.Lang
56   * @crossplatform
57   * @atomicservice
58   * @since 12
59   */
60  function parse(text: string, reviver?: Transformer, options?: ParseOptions): Object | null;
61
62  /**
63   * Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
64   *
65   * @param { Object } value - A JavaScript value, usually an Object or array.
66   * @param { (number | string)[] | null } [replacer] - An array of strings and numbers that acts as an approved list
67   * for selecting the object properties that will be stringify.
68   * @param { string | number } [space] - Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
69   * @returns { string } Return a JSON text.
70   * @throws { BusinessError } 401 - Parameter error. Possible causes:
71   * 1.Mandatory parameters are left unspecified;
72   * 2.Incorrect parameter types;
73   * 3.Parameter verification failed.
74   * @syscap SystemCapability.Utils.Lang
75   * @crossplatform
76   * @atomicservice
77   * @since 12
78   */
79  function stringify(value: Object, replacer?: (number | string)[] | null, space?: string | number): string;
80
81  /**
82   * Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
83   *
84   * @param { Object } value - A JavaScript value, usually an Object or array.
85   * @param { Transformer } [replacer] - A function that transforms the results.
86   * @param { string | number } [space] - Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
87   * @returns { string } Return a JSON text.
88   * @throws { BusinessError } 401 - Parameter error. Possible causes:
89   * 1.Mandatory parameters are left unspecified;
90   * 2.Incorrect parameter types;
91   * 3.Parameter verification failed.
92   * @syscap SystemCapability.Utils.Lang
93   * @crossplatform
94   * @atomicservice
95   * @since 12
96   */
97  function stringify(value: Object, replacer?: Transformer, space?: string | number): string;
98
99  /**
100   * Checks whether the object parsed from a JSON string contains the property.
101   *
102   * @param { object } obj - The object parsed from a JSON string.
103   * @param { string } property - Determine whether the object contains the property.
104   * @returns { boolean } Return true if the key is in the object, otherwise return false.
105   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.
106   * @syscap SystemCapability.Utils.Lang
107   * @crossplatform
108   * @atomicservice
109   * @since 12
110   */
111  function has(obj: object, property: string): boolean;
112
113  /**
114   * Removes a property from the object parsed from a JSON string.
115   *
116   * @param { object } obj - The object parsed from a JSON string.
117   * @param { string } property - The property to be removed.
118   * @throws { BusinessError } 401 - Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.
119   * @syscap SystemCapability.Utils.Lang
120   * @crossplatform
121   * @atomicservice
122   * @since 12
123   */
124  function remove(obj: object, property: string): void;
125
126  /**
127   * Enum defining modes for handling bigint.
128   *
129   * @enum { number } BigIntMode
130   * @syscap SystemCapability.Utils.Lang
131   * @crossplatform
132   * @atomicservice
133   * @since 12
134   */
135  const enum BigIntMode {
136    /**
137     * BigInt is not supported.
138     *
139     * @syscap SystemCapability.Utils.Lang
140     * @crossplatform
141     * @atomicservice
142     * @since 12
143     */
144    DEFAULT = 0,
145    /**
146     * Parse as BigInt when number less than -(2^53 – 1) or greater than (2^53 – 1).
147     *
148     * @syscap SystemCapability.Utils.Lang
149     * @crossplatform
150     * @atomicservice
151     * @since 12
152     */
153    PARSE_AS_BIGINT = 1,
154    /**
155     * All numbers parse as BigInt.
156     *
157     * @syscap SystemCapability.Utils.Lang
158     * @crossplatform
159     * @atomicservice
160     * @since 12
161     */
162    ALWAYS_PARSE_AS_BIGINT = 2,
163  }
164
165  /**
166   * Parse's options
167   *
168   * @typedef ParseOptions
169   * @syscap SystemCapability.Utils.Lang
170   * @crossplatform
171   * @atomicservice
172   * @since 12
173   */
174  interface ParseOptions {
175    /**
176    * Enum defining modes for handling bigint.
177    * @type { BigIntMode }
178    * @syscap SystemCapability.Utils.Lang
179    * @crossplatform
180    * @atomicservice
181    * @since 12
182    */
183    bigIntMode: BigIntMode;
184  }
185}
186export default json;