diff --git a/ICD.Common.Utils.Tests/Extensions/JsonExtensionsTest.cs b/ICD.Common.Utils.Tests/Extensions/JsonExtensionsTest.cs index 7097a60..d174160 100644 --- a/ICD.Common.Utils.Tests/Extensions/JsonExtensionsTest.cs +++ b/ICD.Common.Utils.Tests/Extensions/JsonExtensionsTest.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using System.Text; using ICD.Common.Utils.Extensions; +using ICD.Common.Utils.IO; using Newtonsoft.Json; using NUnit.Framework; @@ -17,6 +18,36 @@ namespace ICD.Common.Utils.Tests.Extensions 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 expected = new Dictionary + { + {"name", "Test"}, + {"help", "Test test."}, + {"type", "System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"}, + {"value", "Test"} + }; + + Dictionary deserialized = new Dictionary(); + + 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] public void GetValueAsIntTest() { diff --git a/ICD.Common.Utils/Extensions/JsonExtensions.cs b/ICD.Common.Utils/Extensions/JsonExtensions.cs index 405bd90..1d1cb09 100644 --- a/ICD.Common.Utils/Extensions/JsonExtensions.cs +++ b/ICD.Common.Utils/Extensions/JsonExtensions.cs @@ -69,6 +69,47 @@ namespace ICD.Common.Utils.Extensions return serializer.Deserialize(extends); } + /// + /// Reads through the current object token and calls the callback for each property value. + /// + /// + /// + /// + public static void ReadObject(this JsonReader extends, JsonSerializer serializer, + Action 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); + } + } + /// /// Writes the type value. /// diff --git a/ICD.Common.Utils/Json/AbstractGenericJsonConverter.cs b/ICD.Common.Utils/Json/AbstractGenericJsonConverter.cs index 4ad9714..e1bc3c1 100644 --- a/ICD.Common.Utils/Json/AbstractGenericJsonConverter.cs +++ b/ICD.Common.Utils/Json/AbstractGenericJsonConverter.cs @@ -1,5 +1,6 @@ using System; using ICD.Common.Properties; +using ICD.Common.Utils.Extensions; using Newtonsoft.Json; namespace ICD.Common.Utils.Json @@ -110,30 +111,15 @@ namespace ICD.Common.Utils.Json if (serializer == null) throw new ArgumentNullException("serializer"); - T output = default(T); - bool instantiated = false; + if (reader.TokenType == JsonToken.Null) + return default(T); - while (reader.Read()) - { - if (reader.TokenType == JsonToken.Null || reader.TokenType == JsonToken.EndObject) - break; + if (reader.TokenType != JsonToken.StartObject) + throw new FormatException(string.Format("Expected {0} got {1}", JsonToken.StartObject, reader.TokenType)); - if (!instantiated) - { - instantiated = true; - output = Instantiate(); - } + T output = Instantiate(); - // Get the property - if (reader.TokenType != JsonToken.PropertyName) - continue; - string property = (string)reader.Value; - - // Read into the value - reader.Read(); - - ReadProperty(property, reader, output, serializer); - } + reader.ReadObject(serializer, (p, r, s) => ReadProperty(p, r, output, s)); return output; }