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

@@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- TableBuilder supports multi-line content - TableBuilder supports multi-line content
- Added eIcdFileMode for IO platform agnosticism - Added eIcdFileMode for IO platform agnosticism
### Changed
- Rewrote JsonItemWrapper serialization for JsonConvert friendliness
## [11.0.0] - 2020-03-20 ## [11.0.0] - 2020-03-20
### Added ### Added
- Added Not null tag for ICDUriBuilder Constructor that takes a URI as an argument. - Added Not null tag for ICDUriBuilder Constructor that takes a URI as an argument.

View File

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

View File

@@ -1,97 +1,97 @@
using System; using System;
using System.Text.RegularExpressions; using ICD.Common.Properties;
using ICD.Common.Utils.Extensions; using ICD.Common.Utils.Extensions;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ICD.Common.Utils.Json namespace ICD.Common.Utils.Json
{ {
/// <summary> /// <summary>
/// Simple wrapper for serialization of an object and its type. /// Simple wrapper for serialization of an object and its type.
/// </summary> /// </summary>
[JsonConverter(typeof(JsonItemWrapperConverter))]
public sealed class JsonItemWrapper public sealed class JsonItemWrapper
{ {
private const string TYPE_TOKEN = "t";
private const string ITEM_TOKEN = "i";
private readonly object m_Item;
/// <summary> /// <summary>
/// Gets the Type of the item. Returns null if the item is null. /// Gets the Type of the item. Returns null if the item is null.
/// </summary> /// </summary>
public Type ItemType { get { return m_Item == null ? null : m_Item.GetType(); } } [CanBeNull]
public Type Type { get; set; }
/// <summary> /// <summary>
/// Gets the wrapped item. /// Gets the wrapped item.
/// </summary> /// </summary>
public object Item { get { return m_Item; } } [CanBeNull]
public object Item { get; set; }
/// <summary>
/// Constructor.
/// </summary>
public JsonItemWrapper()
: this(null)
{
}
/// <summary> /// <summary>
/// Constructor. /// Constructor.
/// </summary> /// </summary>
/// <param name="item"></param> /// <param name="item"></param>
public JsonItemWrapper(object item) public JsonItemWrapper([CanBeNull] object item)
{ {
m_Item = item; Item = item;
Type = item == null ? null : item.GetType();
} }
}
public sealed class JsonItemWrapperConverter : AbstractGenericJsonConverter<JsonItemWrapper>
{
private const string TYPE_TOKEN = "t";
private const string ITEM_TOKEN = "i";
/// <summary> /// <summary>
/// Writes the JsonItemWrapper as a JObject. /// Override to write properties to the writer.
/// </summary> /// </summary>
/// <param name="writer"></param> /// <param name="writer"></param>
public void Write(JsonWriter writer) /// <param name="value"></param>
/// <param name="serializer"></param>
protected override void WriteProperties(JsonWriter writer, JsonItemWrapper value, JsonSerializer serializer)
{ {
if (writer == null) base.WriteProperties(writer, value, serializer);
throw new ArgumentNullException("writer");
writer.WriteStartObject(); if (value.Type != null)
writer.WriteProperty(TYPE_TOKEN, value.Type.GetMinimalName());
if (value.Item != null)
{ {
writer.WritePropertyName(TYPE_TOKEN);
writer.WriteType(ItemType);
writer.WritePropertyName(ITEM_TOKEN); writer.WritePropertyName(ITEM_TOKEN);
writer.WriteValue(JsonConvert.SerializeObject(m_Item)); serializer.Serialize(writer, value.Item);
} }
writer.WriteEndObject();
} }
/// <summary> /// <summary>
/// Reads the JToken back to the wrapped object. /// Override to handle the current property value with the given name.
/// </summary> /// </summary>
/// <param name="token"></param> /// <param name="property"></param>
/// <returns></returns> /// <param name="reader"></param>
public static object ReadToObject(JToken token) /// <param name="instance"></param>
/// <param name="serializer"></param>
protected override void ReadProperty(string property, JsonReader reader, JsonItemWrapper instance, JsonSerializer serializer)
{ {
if (token == null) switch (property)
throw new ArgumentNullException("token");
string typeString = (string)token.SelectToken(TYPE_TOKEN);
if (string.IsNullOrEmpty(typeString))
return null;
string itemString = (string)token.SelectToken(ITEM_TOKEN);
Type type = Type.GetType(typeString);
if (type == null)
{ {
typeString = typeString.Replace("_SimplSharp", "").Replace("_NetStandard", ""); case TYPE_TOKEN:
type = Type.GetType(typeString); instance.Type = reader.TokenType == JsonToken.Null ? null : reader.GetValueAsType();
break;
case ITEM_TOKEN:
if (instance.Type == null && reader.TokenType != JsonToken.Null)
throw new FormatException("No Type for associated Item");
instance.Item = serializer.Deserialize(reader, instance.Type);
break;
default:
base.ReadProperty(property, reader, instance, serializer);
break;
} }
if (type == null)
{
typeString = AddSimplSharpSuffix(typeString);
type = Type.GetType(typeString);
}
return JsonConvert.DeserializeObject(itemString, type);
}
private static string AddSimplSharpSuffix(string typeString)
{
return Regex.Replace(typeString,
"(?'prefix'[^,]+, )(?'assembly'[^,]*)(?'suffix', .*)",
"${prefix}${assembly}_SimplSharp${suffix}");
} }
} }
} }