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