* ObjectTest.cs:

* LCObjectData.cs:

* LCObject.cs: chore: 支持 LCObject 序列化
oneRain 2020-05-06 14:07:35 +08:00
parent 2266887b9a
commit 385a261388
3 changed files with 65 additions and 5 deletions

View File

@ -180,5 +180,39 @@ namespace Storage.Test {
Assert.NotNull(obj["intList"]); 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<int> { 1, 1, 2, 3, 5, 8 };
obj["stringMap"] = new Dictionary<string, object> {
{ "k1", 111 },
{ "k2", true },
{ "k3", "haha" }
};
LCObject nestedObj = new LCObject("World");
nestedObj["content"] = "7788";
obj["objectValue"] = nestedObj;
obj["pointerList"] = new List<object> {
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");
}
} }
} }

View File

@ -55,11 +55,17 @@ namespace LeanCloud.Storage.Internal.Object {
return null; return null;
} }
Dictionary<string, object> dict = new Dictionary<string, object> { Dictionary<string, object> dict = new Dictionary<string, object> {
{ "className", objectData.ClassName }, { "className", objectData.ClassName }
{ "objectId", objectData.ObjectId },
{ "createdAt", objectData.CreatedAt },
{ "updatedAt", objectData.UpdatedAt },
}; };
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) { if (objectData.CustomPropertyDict != null) {
foreach (KeyValuePair<string, object> kv in objectData.CustomPropertyDict) { foreach (KeyValuePair<string, object> kv in objectData.CustomPropertyDict) {
string key = kv.Key; string key = kv.Key;

View File

@ -3,7 +3,7 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Collections.ObjectModel; using Newtonsoft.Json;
using LeanCloud.Storage.Internal.Object; using LeanCloud.Storage.Internal.Object;
using LeanCloud.Storage.Internal.Operation; using LeanCloud.Storage.Internal.Operation;
using LeanCloud.Storage.Internal.Codec; using LeanCloud.Storage.Internal.Codec;
@ -457,6 +457,26 @@ namespace LeanCloud.Storage {
subclassTypeDict[classType] = subclassInfo; subclassTypeDict[classType] = subclassInfo;
} }
/// <summary>
/// 序列化为 json 字符串
/// </summary>
/// <returns></returns>
public override string ToString() {
return JsonConvert.SerializeObject(LCObjectData.Encode(data));
}
/// <summary>
/// 反序列化为 LCObject 对象
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public static LCObject ParseObject(string json) {
LCObjectData objectData = LCObjectData.Decode(JsonConvert.DeserializeObject<Dictionary<string, object>>(json));
LCObject obj = Create(objectData.ClassName);
obj.Merge(objectData);
return obj;
}
void ApplyOperation(string key, ILCOperation op) { void ApplyOperation(string key, ILCOperation op) {
if (op is LCDeleteOperation) { if (op is LCDeleteOperation) {
estimatedData.Remove(key); estimatedData.Remove(key);