From 385a2613889adfd65d71d9401c3c5b675e0d2266 Mon Sep 17 00:00:00 2001 From: oneRain Date: Wed, 6 May 2020 14:07:35 +0800 Subject: [PATCH] * ObjectTest.cs: * LCObjectData.cs: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * LCObject.cs: chore: 支持 LCObject 序列化 --- Storage/Storage.Test/ObjectTest.cs | 34 +++++++++++++++++++ .../Storage/Internal/Object/LCObjectData.cs | 14 +++++--- Storage/Storage/LCObject.cs | 22 +++++++++++- 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/Storage/Storage.Test/ObjectTest.cs b/Storage/Storage.Test/ObjectTest.cs index 4eacaad..22c1fec 100644 --- a/Storage/Storage.Test/ObjectTest.cs +++ b/Storage/Storage.Test/ObjectTest.cs @@ -180,5 +180,39 @@ namespace Storage.Test { Assert.NotNull(obj["intList"]); } } + + [Test] + public async Task Serialization() { + LCObject obj = new LCObject("Hello"); + obj["intValue"] = 123; + obj["boolValue"] = true; + obj["stringValue"] = "hello, world"; + obj["time"] = DateTime.Now; + obj["intList"] = new List { 1, 1, 2, 3, 5, 8 }; + obj["stringMap"] = new Dictionary { + { "k1", 111 }, + { "k2", true }, + { "k3", "haha" } + }; + LCObject nestedObj = new LCObject("World"); + nestedObj["content"] = "7788"; + obj["objectValue"] = nestedObj; + obj["pointerList"] = new List { + new LCObject("World"), + nestedObj + }; + await obj.Save(); + + string json = obj.ToString(); + WriteLine(json); + LCObject newObj = LCObject.ParseObject(json); + Assert.NotNull(newObj.ObjectId); + Assert.NotNull(newObj.ClassName); + Assert.NotNull(newObj.CreatedAt); + Assert.NotNull(newObj.UpdatedAt); + Assert.AreEqual(newObj["intValue"], 123); + Assert.AreEqual(newObj["boolValue"], true); + Assert.AreEqual(newObj["stringValue"], "hello, world"); + } } } diff --git a/Storage/Storage/Internal/Object/LCObjectData.cs b/Storage/Storage/Internal/Object/LCObjectData.cs index 964758b..d941068 100644 --- a/Storage/Storage/Internal/Object/LCObjectData.cs +++ b/Storage/Storage/Internal/Object/LCObjectData.cs @@ -55,11 +55,17 @@ namespace LeanCloud.Storage.Internal.Object { return null; } Dictionary dict = new Dictionary { - { "className", objectData.ClassName }, - { "objectId", objectData.ObjectId }, - { "createdAt", objectData.CreatedAt }, - { "updatedAt", objectData.UpdatedAt }, + { "className", objectData.ClassName } }; + if (!string.IsNullOrEmpty(objectData.ObjectId)) { + dict["objectId"] = objectData.ObjectId; + } + if (objectData.CreatedAt != null) { + dict["createdAt"] = objectData.CreatedAt; + } + if (objectData.UpdatedAt != null) { + dict["updatedAt"] = objectData.UpdatedAt; + } if (objectData.CustomPropertyDict != null) { foreach (KeyValuePair kv in objectData.CustomPropertyDict) { string key = kv.Key; diff --git a/Storage/Storage/LCObject.cs b/Storage/Storage/LCObject.cs index 09ebb33..84ad22b 100644 --- a/Storage/Storage/LCObject.cs +++ b/Storage/Storage/LCObject.cs @@ -3,7 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using System.Collections.ObjectModel; +using Newtonsoft.Json; using LeanCloud.Storage.Internal.Object; using LeanCloud.Storage.Internal.Operation; using LeanCloud.Storage.Internal.Codec; @@ -457,6 +457,26 @@ namespace LeanCloud.Storage { subclassTypeDict[classType] = subclassInfo; } + /// + /// 序列化为 json 字符串 + /// + /// + public override string ToString() { + return JsonConvert.SerializeObject(LCObjectData.Encode(data)); + } + + /// + /// 反序列化为 LCObject 对象 + /// + /// + /// + public static LCObject ParseObject(string json) { + LCObjectData objectData = LCObjectData.Decode(JsonConvert.DeserializeObject>(json)); + LCObject obj = Create(objectData.ClassName); + obj.Merge(objectData); + return obj; + } + void ApplyOperation(string key, ILCOperation op) { if (op is LCDeleteOperation) { estimatedData.Remove(key);