!8976 Add JSON logic code under the public basic library util.

Merge pull request !8976 from 王景悟/master
This commit is contained in:
openharmony_ci 2024-02-28 01:30:26 +00:00 committed by Gitee
commit 5d632e2b9d
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 106 additions and 1 deletions

104
api/@ohos.util.json.d.ts vendored Normal file
View File

@ -0,0 +1,104 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* @kit ArkTS
*/
/**
* JSON is a syntax for serializing objects, arrays, numbers, strings, booleans, and null.
*
* @namespace json
* @syscap SystemCapability.Utils.Lang
* @crossplatform
* @since 12
*/
declare namespace json {
/**
* The type of conversion result function.
*
* @syscap SystemCapability.Utils.Lang
* @since 12
*/
type Transformer = (this: Object, key: string, value: Object) => Object | undefined | null
/**
* Converts a JavaScript Object Notation (JSON) string into an Object or null.
*
* @param { string } text - A valid JSON string.
* @param { Transformer } [reviver] - A function that transforms the results.
* @returns { Object | null } Return an Object, array, string, number, boolean, or null value corresponding to JSON text.
* @throws { BusinessError } 401 - if the input parameters are invalid.
* @syscap SystemCapability.Utils.Lang
* @crossplatform
* @since 12
*/
function parse(text: string, reviver?: Transformer): Object | null;
/**
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
*
* @param { Object } value - A JavaScript value, usually an Object or array.
* @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 stringified.
* @param { string | number } [space] - Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
* @returns { string } Return a JSON text.
* @throws { BusinessError } 401 - if the input parameters are invalid.
* @syscap SystemCapability.Utils.Lang
* @crossplatform
* @since 12
*/
function stringify(value: Object, replacer?: (number | string)[] | null, space?: string | number): string
/**
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
*
* @param { Object } value - A JavaScript value, usually an Object or array.
* @param { Transformer } [replacer] - A function that transforms the results.
* @param { string | number } [space] - Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
* @returns { string } Return a JSON text.
* @throws { BusinessError } 401 - if the input parameters are invalid.
* @syscap SystemCapability.Utils.Lang
* @crossplatform
* @since 12
*/
function stringify(value: Object, replacer?: Transformer, space?: string | number): string;
/**
* Checks whether the object parsed from a JSON string contains the property.
*
* @param { object } obj - The object parsed from a JSON string.
* @param { string } property - Determine whether the object contains the property.
* @returns { boolean } Return true if the key is in the object, otherwise return false.
* @throws { BusinessError } 401 - if the input parameters are invalid.
* @syscap SystemCapability.Utils.Lang
* @crossplatform
* @since 12
*/
function has(obj: object, property: string): boolean;
/**
* Removes a property from the object parsed from a JSON string.
*
* @param { object } obj - The object parsed from a JSON string.
* @param { string } property - The property to be removed.
* @throws { BusinessError } 401 - if the input parameters are invalid.
* @syscap SystemCapability.Utils.Lang
* @crossplatform
* @since 12
*/
function remove(obj: object, property: string): void;
}
export default json;

View File

@ -45,11 +45,12 @@ import worker, {
WorkerEventListener, WorkerEventTarget, WorkerOptions
} from '@ohos.worker';
import xml from '@ohos.xml';
import JSON from '@ohos.util.json';
export {
ArrayList, convertxml, DedicatedWorkerGlobalScope, Deque, ErrorEvent, Event, EventListener,
EventTarget, HashMap, HashSet, LightWeightMap, LightWeightSet, LinkedList, List, MessageEvent,
MessageEvents, PlainArray, PostMessageOptions, Queue, Stack, ThreadWorkerGlobalScope, TreeMap,
TreeSet, Vector, WorkerEventListener, WorkerEventTarget, WorkerOptions, buffer, process, taskpool,
uri, url, util, worker, xml
uri, url, util, worker, xml, JSON
};