1# Uniform Data Structs
2
3
4## When to Use
5
6Uniform data structs are provided to define data of common [uniform data types](../reference/apis-arkdata/js-apis-data-uniformTypeDescriptor.md#uniformdatatype). For example, the data struct for the system-defined home screen icon (the uniform data type is **openharmony.app-item**) is provided to help you easily define the data.
7
8Applications can directly use the uniform data structs in certain scenarios. For example, in the drag-and-drop operation across applications, you can write the data (encapsulated in a uniform struct) to be dragged to [DragEvent](../reference/apis-arkui/arkui-ts/ts-universal-events-drag-drop.md#dragevent). The target application (application requesting the data) reads the data from **DragEvent** and parses the data in the uniform data struct. Using uniform data structs for data interaction between applications effectively reduces the development workload in your app experience.
9
10## Uniform Data Structs
11
12The following table lists the uniform data structs provided by the UDMF.
13
14| Data Struct               |       Data Type       | Description      |
15| ----------------------- | :-------------------: | ---------- |
16| [PlainText](../reference/apis-arkdata/js-apis-data-uniformDataStruct.md#plaintext)    |      'general.plain-text'        | Plain text    |
17| [Hyperlink](../reference/apis-arkdata/js-apis-data-uniformDataStruct.md#hyperlink)     |       'general.hyperlink'       | Hyperlink    |
18| [HTML](../reference/apis-arkdata/js-apis-data-uniformDataStruct.md#html)          |         'general.html'          | HyperText Markup Language (HTML)    |
19| [OpenHarmonyAppItem](../reference/apis-arkdata/js-apis-data-uniformDataStruct.md#openharmonyappitem)  | 'openharmony.app-item'    | Icon      |
20
21## How to Develop
22
23The following describes how to use the uniform data structs to define a hyperlink and a plaint text.
24
25  ```ts
26  // 1. Import the unifiedDataChannel and uniformTypeDescriptor modules.
27  import { uniformDataStruct, uniformTypeDescriptor, unifiedDataChannel } from '@kit.ArkData';
28
29  // 2. Create a data record for a hyperlink.
30  let hyperlinkDetails : Record<string, string> = {
31    'attr1': 'value1',
32    'attr2': 'value2',
33  }
34  let hyperlink : uniformDataStruct.Hyperlink = {
35    uniformDataType:'general.hyperlink',
36    url : 'www.XXX.com',
37    description : 'This is the description of this hyperlink',
38    details : hyperlinkDetails,
39  }
40
41  hyperlink.description = '...'; // Set description of the hyperlink.
42
43  console.info(`hyperlink url = ${hyperlink.url}`); // Access object attributes.
44
45  // 3. Create a data record for a plain text and add it to the UnifiedData instance created.
46  let plainTextDetails : Record<string, string> = {
47    'attr1': 'value1',
48    'attr2': 'value2',
49  }
50  let plainText : uniformDataStruct.PlainText = {
51    uniformDataType: 'general.plain-text',
52    textContent : 'This is plainText textContent example',
53    abstract : 'this is abstract',
54    details : plainTextDetails,
55  }
56  // 4. Create a UnifiedData instance.
57  let unifiedData = new unifiedDataChannel.UnifiedData();
58  let hyperlinkRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.HYPERLINK, hyperlink);
59  let plainTextRecord = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainText);
60
61  // 5. Add a plainText data record.
62  unifiedData.addRecord(hyperlinkRecord);
63  unifiedData.addRecord(plainTextRecord);
64
65  // 6. Obtain all data records in this UnifiedData instance.
66  let records = unifiedData.getRecords();
67
68  // 7. Traverse all records, determine the data type of the record, and convert the record into a child class object to obtain the original data record.
69  for (let i = 0; i < records.length; i ++) {
70    let unifiedDataRecord = records[i] as unifiedDataChannel.UnifiedRecord;
71    let record = unifiedDataRecord.getValue() as object;
72    if (record != undefined) {
73      // Obtain the type of each data record.
74      let type : string = record["uniformDataType"];
75      switch (type) {
76        case uniformTypeDescriptor.UniformDataType.HYPERLINK:
77          Object.keys(record).forEach(key => {
78            console.info('show records: ' + key + ', value:' + record[key]);
79          });
80          break;
81        case uniformTypeDescriptor.UniformDataType.PLAIN_TEXT:
82          Object.keys(record).forEach(key => {
83            console.info('show records: ' + key + ', value:' + record[key]);
84          });
85          break;
86        default:
87          break;
88      }
89    }
90  }
91  ```