refactor: Rewrote JsonItemWrapper serialization for JsonConvert friendliness

This commit is contained in:
Chris Cameron
2020-04-09 12:13:27 -04:00
parent 5067f5fdb3
commit 96946d6323
3 changed files with 66 additions and 61 deletions

View File

@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using ICD.Common.Utils.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using NUnit.Framework;
namespace ICD.Common.Utils.Tests.Json
@@ -14,7 +14,7 @@ namespace ICD.Common.Utils.Tests.Json
[TestCase(1, typeof(int))]
public void ItemTypeTest(object item, Type expected)
{
Assert.AreEqual(expected, new JsonItemWrapper(item).ItemType);
Assert.AreEqual(expected, new JsonItemWrapper(item).Type);
}
[TestCase("")]
@@ -27,19 +27,21 @@ namespace ICD.Common.Utils.Tests.Json
[Test]
public void WriteTest()
{
JsonItemWrapper wrapper = new JsonItemWrapper(new List<int> {1, 2, 3});
string json = JsonUtils.Serialize(wrapper.Write);
const string expected = @"{""t"":""System.Collections.Generic.List`1[[System.Int32]]"",""i"":[1,2,3]}";
Assert.Inconclusive();
JsonItemWrapper wrapper = new JsonItemWrapper(new List<int> {1, 2, 3});
string json = JsonConvert.SerializeObject(wrapper);
Assert.AreEqual(expected, json);
}
[Test]
public void ReadTest()
{
const string json = "{\"t\":\"System.Collections.Generic.List`1[[System.Int32, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]\",\"i\":\"[1,2,3]\"}";
const string json = @"{""t"":""System.Collections.Generic.List`1[[System.Int32]]"",""i"":[1,2,3]}";
JObject jObject = JObject.Parse(json);
List<int> wrappedObject = JsonItemWrapper.ReadToObject(jObject) as List<int>;
JsonItemWrapper wrapper = JsonConvert.DeserializeObject<JsonItemWrapper>(json);
List<int> wrappedObject = wrapper.Item as List<int>;
Assert.NotNull(wrappedObject);
Assert.AreEqual(3, wrappedObject.Count);