feat: ChatData 序列化 json化
This commit is contained in:
parent
32dfd20454
commit
470c66a612
|
@ -0,0 +1,17 @@
|
||||||
|
/**
|
||||||
|
* Created on 2024/9/19
|
||||||
|
*/
|
||||||
|
package ohos_app_cangjie_entry.support.support_database
|
||||||
|
|
||||||
|
import ohos.preferences.*
|
||||||
|
internal import ohos.ability.*
|
||||||
|
internal import ohos.window.*
|
||||||
|
import ohos.base.*
|
||||||
|
|
||||||
|
public class PreferencesStore {
|
||||||
|
private let context: AbilityContext
|
||||||
|
public init(context: AbilityContext) {
|
||||||
|
this.context = context
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,12 +3,48 @@
|
||||||
*/
|
*/
|
||||||
package ohos_app_cangjie_entry.types
|
package ohos_app_cangjie_entry.types
|
||||||
|
|
||||||
public class ChatData {
|
import serialization.serialization.*
|
||||||
|
import std.math.*
|
||||||
|
import encoding.json.*
|
||||||
|
|
||||||
|
public class ChatData <: Serializable<ChatData> {
|
||||||
public ChatData(public let id!: String, public let message!: String, public let source!: String) {}
|
public ChatData(public let id!: String, public let message!: String, public let source!: String) {}
|
||||||
|
|
||||||
public operator func ==(right: ChatData) {
|
public operator func ==(right: ChatData) {
|
||||||
return (this.id == right.id
|
return (
|
||||||
|
this.id == right.id
|
||||||
&& this.message == right.message
|
&& this.message == right.message
|
||||||
&& this.source == right.source)
|
&& this.source == right.source
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
public func serialize(): DataModel {
|
||||||
|
return (
|
||||||
|
DataModelStruct()
|
||||||
|
.add(field<String>('id', id))
|
||||||
|
.add(field<String>('message', message))
|
||||||
|
.add(field<String>('source', source))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func deserialize(dm: DataModel): ChatData {
|
||||||
|
let dms = match (dm) {
|
||||||
|
case data: DataModelStruct => data
|
||||||
|
case _ => throw Exception("this data is not DataModelStruct")
|
||||||
|
}
|
||||||
|
ChatData(
|
||||||
|
id: String.deserialize(dms.get('id')),
|
||||||
|
message: String.deserialize(dms.get('message')),
|
||||||
|
source: String.deserialize(dms.get('source')),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
public func toString(): String {
|
||||||
|
this.serialize().toJson().toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func fromString(s: String): ChatData {
|
||||||
|
let jsonObj = JsonValue.fromStr(s)
|
||||||
|
ChatData.deserialize(DataModel.fromJson(jsonObj))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user