From 470c66a61256e94049425fc0c6749b82d6eb53d0 Mon Sep 17 00:00:00 2001 From: Cheliangzhao Date: Thu, 19 Sep 2024 10:57:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20ChatData=20=E5=BA=8F=E5=88=97=E5=8C=96?= =?UTF-8?q?=20json=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../support_database/PreferenceStore.cj | 17 ++++++++ entry/src/main/cangjie/src/types/ChatData.cj | 42 +++++++++++++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 entry/src/main/cangjie/src/support/support_database/PreferenceStore.cj diff --git a/entry/src/main/cangjie/src/support/support_database/PreferenceStore.cj b/entry/src/main/cangjie/src/support/support_database/PreferenceStore.cj new file mode 100644 index 0000000..626566a --- /dev/null +++ b/entry/src/main/cangjie/src/support/support_database/PreferenceStore.cj @@ -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 + + } +} diff --git a/entry/src/main/cangjie/src/types/ChatData.cj b/entry/src/main/cangjie/src/types/ChatData.cj index 722f530..f0b6d0f 100644 --- a/entry/src/main/cangjie/src/types/ChatData.cj +++ b/entry/src/main/cangjie/src/types/ChatData.cj @@ -3,12 +3,48 @@ */ package ohos_app_cangjie_entry.types -public class ChatData { +import serialization.serialization.* +import std.math.* +import encoding.json.* + +public class ChatData <: Serializable { public ChatData(public let id!: String, public let message!: String, public let source!: String) {} public operator func ==(right: ChatData) { - return (this.id == right.id + return ( + this.id == right.id && this.message == right.message - && this.source == right.source) + && this.source == right.source + ) + } + + public func serialize(): DataModel { + return ( + DataModelStruct() + .add(field('id', id)) + .add(field('message', message)) + .add(field('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)) } }