mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
Begin implementing shorthand methods for serializing/deserializing json
This commit is contained in:
50
ICD.Common.Utils.Tests/Json/JsonUtilsTest.cs
Normal file
50
ICD.Common.Utils.Tests/Json/JsonUtilsTest.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ using System.Linq;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using ICD.Common.Properties;
|
using ICD.Common.Properties;
|
||||||
using ICD.Common.Utils.Extensions;
|
using ICD.Common.Utils.Extensions;
|
||||||
|
using ICD.Common.Utils.IO;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
@@ -18,6 +19,9 @@ namespace ICD.Common.Utils.Json
|
|||||||
// 2016-02-26T19:24:59
|
// 2016-02-26T19:24:59
|
||||||
private const string DATE_FORMAT = @"yyyy-MM-dd\THH:mm:ss";
|
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>
|
/// <summary>
|
||||||
/// Forces Newtonsoft to cache the given type for faster subsequent usage.
|
/// Forces Newtonsoft to cache the given type for faster subsequent usage.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -136,5 +140,71 @@ namespace ICD.Common.Utils.Json
|
|||||||
|
|
||||||
IcdConsole.PrintLine(sb.ToString());
|
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();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user