From 87fb9e5e0d55a18bd24c9f3db52caa91393ac7d7 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Fri, 1 Dec 2017 16:58:13 -0500 Subject: [PATCH] Begin implementing shorthand methods for serializing/deserializing json --- ICD.Common.Utils.Tests/Json/JsonUtilsTest.cs | 50 ++++++++++++++ ICD.Common.Utils/Json/JsonUtils.cs | 70 ++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 ICD.Common.Utils.Tests/Json/JsonUtilsTest.cs diff --git a/ICD.Common.Utils.Tests/Json/JsonUtilsTest.cs b/ICD.Common.Utils.Tests/Json/JsonUtilsTest.cs new file mode 100644 index 0000000..cbcce53 --- /dev/null +++ b/ICD.Common.Utils.Tests/Json/JsonUtilsTest.cs @@ -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(); + } + } +} diff --git a/ICD.Common.Utils/Json/JsonUtils.cs b/ICD.Common.Utils/Json/JsonUtils.cs index 8700c6c..4a3e57c 100644 --- a/ICD.Common.Utils/Json/JsonUtils.cs +++ b/ICD.Common.Utils/Json/JsonUtils.cs @@ -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"; + /// /// Forces Newtonsoft to cache the given type for faster subsequent usage. /// @@ -136,5 +140,71 @@ namespace ICD.Common.Utils.Json IcdConsole.PrintLine(sb.ToString()); } + + /// + /// Shorthand for serializing an instance to a json string. + /// + /// + /// + [PublicAPI] + public static string Serialize(Action 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(); + } + + /// + /// Shorthand for deserializing a json string to the given type. + /// + /// + /// + /// + /// + [PublicAPI] + public static T Deserialize(Func deserializeMethod, string json) + { + if (deserializeMethod == null) + throw new ArgumentNullException("deserializeMethod"); + + using (JsonTextReader reader = new JsonTextReader(new IcdStringReader(json).WrappedStringReader)) + return deserializeMethod(reader); + } + + /// + /// Serializes to json, wrapping the object with a message property to differentiate between messages. + /// E.g. + /// { a = 1 } + /// Becomes + /// { m = "Test", d = { a = 1 } } + /// + /// + /// + /// + [PublicAPI] + public static string SerializeMessage(Action 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(); + }); + } } }