• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# JSON Extension Library
2
3The JSON extension library extends the native JSON functionality, providing additional error handling, circular reference detection, BigInt processing, and strict type checking for different input types. It relies on the native **JSON.parse** and **JSON.stringify** methods but adds custom logic and additional methods such as **has** and **remove**. For details, see [@arkts.json](../reference/apis-arkts/js-apis-json.md).
4
5## parse
6
7parse(text: string, reviver?: Transformer, options?: ParseOptions): Object | null
8
9Parses a JSON string and supports BigInt mode.
10
11**Differences from native**
12
13| Feature| Native parse | Current parse|
14| -------- | -------- | -------- |
15| BigInt support| Not supported (throws **TypeError**)|Supported (via **parseBigInt** extension)|
16| Parameter verification| Weak verification|Strong verification (throws **BusinessError**)|
17| Error message| Native error (such as **SyntaxError**)|	Custom **BusinessError**|
18| **reviver** parameter| Supported| Supported, with strict type checking|
19
20## stringify
21
22stringify(value: Object, replacer?: (number | string)[] | null, space?: string | number): string
23
24Converts an object to a JSON string and supports BigInt mode.
25
26**Differences from native**
27
28| Feature| Native stringify| Current stringify|
29| -------- | -------- | -------- |
30| BigInt support| Not supported (throws **TypeError**)| Supported (via **stringifyBigInt** extension)|
31| Circular reference detection| Throws **TypeError**| Detects and throws **BusinessError**|
32| Parameter verification| Weak verification| Strong verification (**replacer** must be a function or an array)|
33| Error message| Native error| Custom **BusinessError**|
34
35## has
36
37has(obj: object, property: string): boolean
38
39Checks whether an object contains a specified property, ensuring the input is an object and the property key is a valid string.
40
41**Differences from native**
42
43| Feature| Native (obj.hasOwnProperty)| Current has|
44| -------- | -------- | -------- |
45| Parameter verification| No verification (potential misuse)| Enforces **obj** as a regular object and **property** as a non-empty string|
46| Error handling| May fail silently| Throws **BusinessError**|
47
48## remove
49
50remove(obj: object, property: string): void
51
52Removes a specified property from an object.
53
54| Feature| Native (delete obj.key)| Current remove|
55| -------- | -------- | -------- |
56| Parameter verification| No verification (potential accidental deletion)| Enforces **obj** as a regular object and **property** as a non-empty string|
57| Error handling| May fail silently| Throws **BusinessError**|
58
59**Summary**
60
61| Feature| Native JSON| This Library|
62| ----- | ----- | -----|
63| Strict parameter verification| Not supported|	Supported|
64| Circular reference detection| Not supported|	Supported|
65| BigInt processing| Not supported|	Supported|
66| Enhanced error handling (**BusinessError**)| Not supported| Supported|
67| Additional methods (**has** or **remove**)| Not supported| Supported|
68
69**Use Cases**
70
71- JSON parsing or serialization that involves BigInt handling.
72
73- Situations requiring stricter parameter verification and error handling.
74
75- Scenarios where circular references need to be detected.
76
77- Safe object operations (for example, **has** or **remove**).
78
79This library is suitable for scenarios where enhanced JSON functionality is needed, especially when dealing with BigInt and strict parameter verification.
80