• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ASON解析与生成
2<!--Kit: ArkTS-->
3<!--Subsystem: CommonLibrary-->
4<!--Owner: @lijiamin2025-->
5<!--Designer: @weng-changcheng-->
6<!--Tester: @kirl75; @zsw_zhushiwei-->
7<!--Adviser: @ge-yafang-->
8
9[ASON工具](../reference/apis-arkts/arkts-apis-arkts-utils-ASON.md)与JS提供的JSON工具类似,JSON用于进行JS对象的序列化(stringify)、反序列化(parse)。ASON则提供了[Sendable对象](arkts-sendable.md)的序列化、反序列化能力。使用ASON.stringify方法可将对象转换为字符串,使用ASON.parse方法可将字符串转换为Sendable对象,从而实现对象在并发任务间的高性能引用传递。
10
11ASON.stringify方法还支持将Map和Set对象转换为字符串,可转换的Map和Set类型包括:Map、Set、[collections.Map](../reference/apis-arkts/arkts-apis-arkts-collections-Map.md)、[collections.Set](../reference/apis-arkts/arkts-apis-arkts-collections-Set.md)、[HashMap](../reference/apis-arkts/js-apis-hashmap.md#hashmap)、[HashSet](../reference/apis-arkts/js-apis-hashset.md#hashset)。
12
13> **说明:**
14>
15> ASON.parse默认生成的对象为Sendable对象,布局不可变,不支持增删属性。如果返回的对象需要支持增删属性,可以指定返回类型为[collections.Map](../reference/apis-arkts/arkts-apis-arkts-collections-Map.md)对象。
16
17## 使用示例
18
19使用ASON提供的接口,对[Sendable对象](arkts-sendable.md)进行序列化、反序列化。
20
21```ts
22import { ArkTSUtils, collections } from '@kit.ArkTS';
23
24ArkTSUtils.ASON.parse("{}")
25ArkTSUtils.ASON.stringify(new collections.Array(1, 2, 3))
26
27let options2: ArkTSUtils.ASON.ParseOptions = {
28    bigIntMode: ArkTSUtils.ASON.BigIntMode.PARSE_AS_BIGINT,
29    parseReturnType: ArkTSUtils.ASON.ParseReturnType.MAP,
30}
31let jsonText = '{"largeNumber":112233445566778899}';
32let map = ArkTSUtils.ASON.parse(jsonText, undefined, options2);
33// 执行结果为:{"largeNumber":112233445566778899}
34console.info(ArkTSUtils.ASON.stringify(map));
35```
36<!-- @[example_serialize](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/ArkTsConcurrent/ConcurrentThreadCommunication/InterThreadCommunicationObjects/SendableObject/SendableObjectRelated/entry/src/main/ets/managers/AsonParsingGeneration.ets) -->