仓颉应用开发之——序列化和反序列化(16)

一 概述

  • 包含基本类型的数据类序列化和反序列化
  • 包含序列化类的数据类序列化和反序列化
  • 序列化类的调用

二 包含基本类型的数据类序列化和反序列化

2.1 说明

  • 手动导包import serialization.serialization.*
  • 只包含:整数(Int)、字符(Rune)、布尔(Boolean)、字符串(String),不包含类(class)、结构体等
  • 此类实现 Serializable 接口,并实现serialize序列化和deserialize反序列化方法
  • serialize序列化方法:调用DataModelStruct将类对象转换位DataModel
  • deserialize反序列化方法:接受一个DataModel,并将DataModel对象解析为类对象

2.2 示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Location <: Serializable<Location> {
var time: Int64 = 666
var heheh: Rune = 'T'

/* 实现 Serializable 接口的序列化方法 */
public func serialize(): DataModel {
return DataModelStruct().add(field<Int64>("time", time)).add(field<Rune>("heheh", heheh))
}

/* 实现反序列化方法 */
public static func deserialize(dm: DataModel): Location {
let dms = match (dm) {
case data: DataModelStruct => data
case _ => throw Exception("this data is not DataModelStruct")
}
let result = Location()
result.time = Int64.deserialize(dms.get("time"))
result.heheh = Rune.deserialize(dms.get("heheh"))
return result
}
}

三 包含序列化类的数据类序列化和反序列化

3.1 说明

  • 手动导包import serialization.serialization.*
  • 除基本类型,包含序列化类型(使用Option装饰)
  • 同理,序列化和反序列化方法增加对Option的支持

3.2 示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Abc <: Serializable<Abc> {
var name: String = "Abcde"
var age: Int64 = 555
var loc: Option<Location> = Option<Location>.None

/* 实现 Serializable 接口的序列化方法 */
public func serialize(): DataModel {
return DataModelStruct().add(field<String>("name", name)).add(field<Int64>("age", age)).add(field<Option<Location>>("loc", loc))
}

/* 实现反序列化方法 */
public static func deserialize(dm: DataModel): Abc {
let dms = match (dm) {
case data: DataModelStruct => data
case _ => throw Exception("this data is not DataModelStruct")
}
let result = Abc()
result.name = String.deserialize(dms.get("name"))
result.age = Int64.deserialize(dms.get("age"))
result.loc = Option<Location>.deserialize(dms.get("loc"))
return result
}
}

四 序列化类的调用

4.1 onPageShow中调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 protected override func onPageShow() {

let dd = Abc()
let aa: JsonValue = dd.serialize().toJson()
let bb: JsonObject = (aa as JsonObject).getOrThrow()
let v1 = (bb.get("name").getOrThrow() as JsonString).getOrThrow()
let v2 = (bb.get("age").getOrThrow() as JsonInt).getOrThrow()
let v3 = bb.get("loc").getOrThrow()
Hilog.printInfo("printInfo",v1.getValue())
Hilog.printInfo("printInfo",v2.getValue().toString())
Hilog.printInfo("printInfo",v3.toString())
Hilog.printInfo("printInfo","===========")
let aaa = ##"{"age": 123, "loc": { "heheh": "H", "time": 45 }, "name": "zhangsan"}"##
let bbb = JsonValue.fromStr(aaa)
let ccc = (bbb as JsonObject).getOrThrow()
let v4 = (ccc.get("name").getOrThrow() as JsonString).getOrThrow()
let v5 = (ccc.get("age").getOrThrow() as JsonInt).getOrThrow()
let v6 = (ccc.get("loc").getOrThrow() as JsonObject).getOrThrow()
let v7 = (v6.get("time").getOrThrow() as JsonInt).getOrThrow()
let v8 = (v6.get("heheh").getOrThrow() as JsonString).getOrThrow()
Hilog.printInfo("printInfo",v4.getValue())
Hilog.printInfo("printInfo",v5.getValue().toString())
Hilog.printInfo("printInfo",v7.getValue().toString())
Hilog.printInfo("printInfo",v8.getValue().toString())

}
//Hilog扩展
extend Hilog{
static func printInfo(tag: String, format: String){
Hilog.info(1, tag,format)
}
}

说明(类型):

  • 序列化类.serialize().toJson() ==> JsonValue
  • (JsonValue as JsonObject).getOrThrow() ==> JsonObject(bb)
  • bb.get("name") ==>Option<T>
  • bb.get("name").getOrThrow() ==><T>
  • (bb.get("name").getOrThrow() as JsonString) 强制转换为JsonString类型
  • (bb.get("name").getOrThrow() as JsonString) .getOrThrow()==>JsonString
  • (bb.get("name").getOrThrow() as JsonString) .getOrThrow().getValue()==>String
  • aaa = ##"{"age": 123, "loc": { "heheh": "H", "time": 45 }, "name": "zhangsan"}"##
  • JsonValue.fromStr(aaa)==>JsonValue

4.2 打印结果

1
2
3
4
5
6
7
8
Abcde
555
null
===========
zhangsan
123
45
H

五 参考

  • HarmonyOS NEXT Developer Beta5仓颉—class序列化和反序列化