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
- Added eIcdFileMode for IO platform agnosticism
### Changed
- Rewrote JsonItemWrapper serialization for JsonConvert friendliness
## [11.0.0] - 2020-03-20
### Added
- Added Not null tag for ICDUriBuilder Constructor that takes a URI as an argument.

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);

View File

@@ -1,97 +1,97 @@
using System;
using System.Text.RegularExpressions;
using ICD.Common.Properties;
using ICD.Common.Utils.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ICD.Common.Utils.Json
{
/// <summary>
/// Simple wrapper for serialization of an object and its type.
/// </summary>
[JsonConverter(typeof(JsonItemWrapperConverter))]
public sealed class JsonItemWrapper
{
private const string TYPE_TOKEN = "t";
private const string ITEM_TOKEN = "i";
private readonly object m_Item;
/// <summary>
/// Gets the Type of the item. Returns null if the item is null.
/// </summary>
public Type ItemType { get { return m_Item == null ? null : m_Item.GetType(); } }
[CanBeNull]
public Type Type { get; set; }
/// <summary>
/// Gets the wrapped item.
/// </summary>
public object Item { get { return m_Item; } }
[CanBeNull]
public object Item { get; set; }
/// <summary>
/// Constructor.
/// </summary>
public JsonItemWrapper()
: this(null)
{
}
/// <summary>
/// Constructor.
/// </summary>
/// <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>
/// Writes the JsonItemWrapper as a JObject.
/// Override to write properties to the writer.
/// </summary>
/// <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)
throw new ArgumentNullException("writer");
base.WriteProperties(writer, value, serializer);
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.WriteValue(JsonConvert.SerializeObject(m_Item));
serializer.Serialize(writer, value.Item);
}
writer.WriteEndObject();
}
/// <summary>
/// Reads the JToken back to the wrapped object.
/// Override to handle the current property value with the given name.
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
public static object ReadToObject(JToken token)
/// <param name="property"></param>
/// <param name="reader"></param>
/// <param name="instance"></param>
/// <param name="serializer"></param>
protected override void ReadProperty(string property, JsonReader reader, JsonItemWrapper instance, JsonSerializer serializer)
{
if (token == null)
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)
switch (property)
{
typeString = typeString.Replace("_SimplSharp", "").Replace("_NetStandard", "");
type = Type.GetType(typeString);
case TYPE_TOKEN:
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}");
}
}
}