Begin implementing shorthand methods for serializing/deserializing json

This commit is contained in:
Chris Cameron
2017-12-01 16:58:13 -05:00
parent e7116fdf4c
commit 87fb9e5e0d
2 changed files with 120 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
using NUnit.Framework;
namespace ICD.Common.Utils.Tests.Json
{
[TestFixture]
public sealed class JsonUtilsTest
{
[Test]
public void CacheTypeTest()
{
Assert.Inconclusive();
}
[Test]
public void ParseDateTimeTest()
{
Assert.Inconclusive();
}
[Test]
public void TryParseDateTimeTest()
{
Assert.Inconclusive();
}
[Test]
public void PrintTest()
{
Assert.Inconclusive();
}
[Test]
public void SerializeTest()
{
Assert.Inconclusive();
}
[Test]
public void DeserializeTest()
{
Assert.Inconclusive();
}
[Test]
public void SerializeMessageTest()
{
Assert.Inconclusive();
}
}
}

View File

@@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using ICD.Common.Properties;
using ICD.Common.Utils.Extensions;
using ICD.Common.Utils.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@@ -18,6 +19,9 @@ namespace ICD.Common.Utils.Json
// 2016-02-26T19:24:59
private const string DATE_FORMAT = @"yyyy-MM-dd\THH:mm:ss";
private const string MESSAGE_NAME_PROPERTY = "m";
private const string MESSAGE_DATA_PROPERTY = "d";
/// <summary>
/// Forces Newtonsoft to cache the given type for faster subsequent usage.
/// </summary>
@@ -136,5 +140,71 @@ namespace ICD.Common.Utils.Json
IcdConsole.PrintLine(sb.ToString());
}
/// <summary>
/// Shorthand for serializing an instance to a json string.
/// </summary>
/// <param name="serializeMethod"></param>
/// <returns></returns>
[PublicAPI]
public static string Serialize(Action<JsonWriter> serializeMethod)
{
if (serializeMethod == null)
throw new ArgumentNullException("serializeMethod");
StringBuilder builder = new StringBuilder();
using (JsonTextWriter writer = new JsonTextWriter(new IcdStringWriter(builder).WrappedStringWriter))
serializeMethod(writer);
return builder.ToString();
}
/// <summary>
/// Shorthand for deserializing a json string to the given type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="deserializeMethod"></param>
/// <param name="json"></param>
/// <returns></returns>
[PublicAPI]
public static T Deserialize<T>(Func<JsonReader, T> deserializeMethod, string json)
{
if (deserializeMethod == null)
throw new ArgumentNullException("deserializeMethod");
using (JsonTextReader reader = new JsonTextReader(new IcdStringReader(json).WrappedStringReader))
return deserializeMethod(reader);
}
/// <summary>
/// Serializes to json, wrapping the object with a message property to differentiate between messages.
/// E.g.
/// { a = 1 }
/// Becomes
/// { m = "Test", d = { a = 1 } }
/// </summary>
/// <param name="serializeMethod"></param>
/// <param name="messageName"></param>
/// <returns></returns>
[PublicAPI]
public static string SerializeMessage(Action<JsonWriter> serializeMethod, string messageName)
{
if (serializeMethod == null)
throw new ArgumentNullException("serializeMethod");
return Serialize(w =>
{
w.WriteStartObject();
{
w.WritePropertyName(MESSAGE_NAME_PROPERTY);
w.WriteValue(messageName);
w.WritePropertyName(MESSAGE_DATA_PROPERTY);
serializeMethod(w);
}
w.WriteEndObject();
});
}
}
}