From 20221678ba018aad58e8bd10cd756dc2eebb2852 Mon Sep 17 00:00:00 2001 From: Jack Kanarish Date: Thu, 8 Mar 2018 17:34:06 -0500 Subject: [PATCH] Improvements to JsonItemWrapper serialization --- .../Json/JsonItemWrapperTest.cs | 19 ++++++++----- ICD.Common.Utils/Json/JsonItemWrapper.cs | 27 +++++++------------ 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/ICD.Common.Utils.Tests/Json/JsonItemWrapperTest.cs b/ICD.Common.Utils.Tests/Json/JsonItemWrapperTest.cs index 0a98c87..b6c8866 100644 --- a/ICD.Common.Utils.Tests/Json/JsonItemWrapperTest.cs +++ b/ICD.Common.Utils.Tests/Json/JsonItemWrapperTest.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using ICD.Common.Utils.Json; +using Newtonsoft.Json.Linq; using NUnit.Framework; namespace ICD.Common.Utils.Tests.Json @@ -7,12 +9,6 @@ namespace ICD.Common.Utils.Tests.Json [TestFixture] public sealed class JsonItemWrapperTest { - [TestCase(null, null)] - public void ItemTypeStringTest(object item, string expected) - { - Assert.AreEqual(expected, new JsonItemWrapper(item).ItemTypeString); - } - [TestCase(null, null)] [TestCase("", typeof(string))] [TestCase(1, typeof(int))] @@ -31,13 +27,22 @@ namespace ICD.Common.Utils.Tests.Json [Test] public void WriteTest() { + JsonItemWrapper wrapper = new JsonItemWrapper(new List {1, 2, 3}); + string json = JsonUtils.Serialize(wrapper.Write); + Assert.Inconclusive(); } [Test] public void ReadTest() { - Assert.Inconclusive(); + 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]\"}"; + + JObject jObject = JObject.Parse(json); + List wrappedObject = JsonItemWrapper.ReadToObject(jObject) as List; + + Assert.NotNull(wrappedObject); + Assert.AreEqual(3, wrappedObject.Count); } } } diff --git a/ICD.Common.Utils/Json/JsonItemWrapper.cs b/ICD.Common.Utils/Json/JsonItemWrapper.cs index ec3136e..4ca434f 100644 --- a/ICD.Common.Utils/Json/JsonItemWrapper.cs +++ b/ICD.Common.Utils/Json/JsonItemWrapper.cs @@ -1,4 +1,5 @@ using System; +using ICD.Common.Utils.Extensions; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -12,26 +13,17 @@ namespace ICD.Common.Utils.Json private const string TYPE_TOKEN = "t"; private const string ITEM_TOKEN = "i"; - private readonly string m_ItemTypeString; private readonly object m_Item; - /// - /// Gets the string representation of the item type. Returns null if the item is null. - /// - public string ItemTypeString { get { return m_ItemTypeString; } } - /// /// Gets the Type of the item. Returns null if the item is null. /// - public Type ItemType - { - get { return string.IsNullOrEmpty(m_ItemTypeString) ? null : Type.GetType(m_ItemTypeString); } - } + public Type ItemType { get { return m_Item == null ? null : m_Item.GetType(); } } /// /// Gets the wrapped item. /// - public object Item { get { return string.IsNullOrEmpty(m_ItemTypeString) ? null : m_Item; } } + public object Item { get { return m_Item; } } /// /// Constructor. @@ -39,7 +31,6 @@ namespace ICD.Common.Utils.Json /// public JsonItemWrapper(object item) { - m_ItemTypeString = item == null ? null : item.GetType().FullName; m_Item = item; } @@ -53,13 +44,13 @@ namespace ICD.Common.Utils.Json throw new ArgumentNullException("writer"); writer.WriteStartObject(); + { + writer.WritePropertyName(TYPE_TOKEN); + writer.WriteType(ItemType); - writer.WritePropertyName(TYPE_TOKEN); - writer.WriteValue(m_ItemTypeString); - - writer.WritePropertyName(ITEM_TOKEN); - writer.WriteValue(JsonConvert.SerializeObject(m_Item)); - + writer.WritePropertyName(ITEM_TOKEN); + writer.WriteValue(JsonConvert.SerializeObject(m_Item)); + } writer.WriteEndObject(); }