harmony 鸿蒙ASON Parsing and Generation

  • 2025-06-06
  • 浏览 (4)

ASON Parsing and Generation

The ASON utility is similar to the JSON utility provided by JavaScript. While JSON is used to serialize (via stringify) and deserialize (via parse) JavaScript objects, ASON provides serialization and deserialization capabilities specifically for Sendable objects. You can convert Sendable objects to strings using the ASON.stringify method and convert strings back to Sendable objects using the ASON.parse method. This enables high-performance pass-by-reference of these objects between concurrent tasks.

The ASON.stringify method can also convert Map and Set objects into strings. These include Map, Set, collections.Map, collections.Set, HashMap, and HashSet.

NOTE

By default, objects generated by ASON.parse are immutable Sendable objects that do not support adding or deleting properties. If you need to return objects with mutable layouts, you can specify the return type as MAP. In this case, all objects will be returned as collections.Map objects, which support adding and deleting properties.

Usage Example

Use ASON interfaces to serialize and deserialize Sendable objects.

import { ArkTSUtils, collections } from '@kit.ArkTS';

ArkTSUtils.ASON.parse("{}")
ArkTSUtils.ASON.stringify(new collections.Array(1, 2, 3))

let options2: ArkTSUtils.ASON.ParseOptions = {
    bigIntMode: ArkTSUtils.ASON.BigIntMode.PARSE_AS_BIGINT,
    parseReturnType: ArkTSUtils.ASON.ParseReturnType.MAP,
}
let jsonText = '{"largeNumber":112233445566778899}';
let map = ArkTSUtils.ASON.parse(jsonText, undefined, options2);
ArkTSUtils.ASON.stringify(map);

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkTS

harmony 鸿蒙Configuring arkOptions in build-profile.json5

harmony 鸿蒙Asynchronous Lock

harmony 鸿蒙Ark Bytecode File Format

harmony 鸿蒙Naming Conventions for Ark Bytecode Functions

harmony 鸿蒙Ark Bytecode Fundamentals

harmony 鸿蒙Overview of Ark Bytecode

harmony 鸿蒙Shared Container

harmony 鸿蒙Asynchronous Waiting

harmony 鸿蒙ArkTS Cross-Language Interaction

0  赞