mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
refactor: Simplifying JSON converters
This commit is contained in:
@@ -3,6 +3,7 @@ using System.IO;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using ICD.Common.Utils.Extensions;
|
using ICD.Common.Utils.Extensions;
|
||||||
|
using ICD.Common.Utils.IO;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
@@ -17,6 +18,36 @@ namespace ICD.Common.Utils.Tests.Extensions
|
|||||||
Assert.Inconclusive();
|
Assert.Inconclusive();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ReadObjectTest()
|
||||||
|
{
|
||||||
|
const string json =
|
||||||
|
"{\"name\":\"Test\",\"help\":\"Test test.\",\"type\":\"System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e\",\"value\":\"Test\"}";
|
||||||
|
|
||||||
|
Dictionary<string, string> expected = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{"name", "Test"},
|
||||||
|
{"help", "Test test."},
|
||||||
|
{"type", "System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},
|
||||||
|
{"value", "Test"}
|
||||||
|
};
|
||||||
|
|
||||||
|
Dictionary<string, string> deserialized = new Dictionary<string, string>();
|
||||||
|
|
||||||
|
using (IcdStringReader textReader = new IcdStringReader(json))
|
||||||
|
{
|
||||||
|
using (JsonReader reader = new JsonTextReader(textReader.WrappedTextReader))
|
||||||
|
{
|
||||||
|
JsonSerializer serializer = new JsonSerializer();
|
||||||
|
|
||||||
|
reader.Read();
|
||||||
|
reader.ReadObject(serializer, (p, r, s) => deserialized.Add(p, (string)r.Value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.IsTrue(deserialized.DictionaryEqual(expected));
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void GetValueAsIntTest()
|
public void GetValueAsIntTest()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -69,6 +69,47 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
return serializer.Deserialize<T>(extends);
|
return serializer.Deserialize<T>(extends);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Reads through the current object token and calls the callback for each property value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <param name="serializer"></param>
|
||||||
|
/// <param name="readPropertyValue"></param>
|
||||||
|
public static void ReadObject(this JsonReader extends, JsonSerializer serializer,
|
||||||
|
Action<string, JsonReader, JsonSerializer> readPropertyValue)
|
||||||
|
{
|
||||||
|
if (extends == null)
|
||||||
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
|
if (serializer == null)
|
||||||
|
throw new ArgumentNullException("serializer");
|
||||||
|
|
||||||
|
if (readPropertyValue == null)
|
||||||
|
throw new ArgumentNullException("readPropertyValue");
|
||||||
|
|
||||||
|
if (extends.TokenType == JsonToken.Null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (extends.TokenType != JsonToken.StartObject)
|
||||||
|
throw new FormatException(string.Format("Expected {0} got {1}", JsonToken.StartObject, extends.TokenType));
|
||||||
|
|
||||||
|
while (extends.Read())
|
||||||
|
{
|
||||||
|
if (extends.TokenType == JsonToken.EndObject)
|
||||||
|
break;
|
||||||
|
|
||||||
|
// Get the property
|
||||||
|
if (extends.TokenType != JsonToken.PropertyName)
|
||||||
|
continue;
|
||||||
|
string property = (string)extends.Value;
|
||||||
|
|
||||||
|
// Read into the value
|
||||||
|
extends.Read();
|
||||||
|
|
||||||
|
readPropertyValue(property, extends, serializer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes the type value.
|
/// Writes the type value.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using ICD.Common.Properties;
|
using ICD.Common.Properties;
|
||||||
|
using ICD.Common.Utils.Extensions;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace ICD.Common.Utils.Json
|
namespace ICD.Common.Utils.Json
|
||||||
@@ -110,30 +111,15 @@ namespace ICD.Common.Utils.Json
|
|||||||
if (serializer == null)
|
if (serializer == null)
|
||||||
throw new ArgumentNullException("serializer");
|
throw new ArgumentNullException("serializer");
|
||||||
|
|
||||||
T output = default(T);
|
if (reader.TokenType == JsonToken.Null)
|
||||||
bool instantiated = false;
|
return default(T);
|
||||||
|
|
||||||
while (reader.Read())
|
if (reader.TokenType != JsonToken.StartObject)
|
||||||
{
|
throw new FormatException(string.Format("Expected {0} got {1}", JsonToken.StartObject, reader.TokenType));
|
||||||
if (reader.TokenType == JsonToken.Null || reader.TokenType == JsonToken.EndObject)
|
|
||||||
break;
|
|
||||||
|
|
||||||
if (!instantiated)
|
T output = Instantiate();
|
||||||
{
|
|
||||||
instantiated = true;
|
|
||||||
output = Instantiate();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the property
|
reader.ReadObject(serializer, (p, r, s) => ReadProperty(p, r, output, s));
|
||||||
if (reader.TokenType != JsonToken.PropertyName)
|
|
||||||
continue;
|
|
||||||
string property = (string)reader.Value;
|
|
||||||
|
|
||||||
// Read into the value
|
|
||||||
reader.Read();
|
|
||||||
|
|
||||||
ReadProperty(property, reader, output, serializer);
|
|
||||||
}
|
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user