From 27f5fd0fe7db18644448a1899212f4839699f8b0 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Thu, 5 Apr 2018 17:03:24 -0400 Subject: [PATCH] feat: Additional validation --- .../Json/AbstractGenericJsonConverter.cs | 12 +++++++++ ICD.Common.Utils/Json/JsonUtils.cs | 27 ++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/ICD.Common.Utils/Json/AbstractGenericJsonConverter.cs b/ICD.Common.Utils/Json/AbstractGenericJsonConverter.cs index 31a2a6e..151a7ca 100644 --- a/ICD.Common.Utils/Json/AbstractGenericJsonConverter.cs +++ b/ICD.Common.Utils/Json/AbstractGenericJsonConverter.cs @@ -14,6 +14,12 @@ namespace ICD.Common.Utils.Json /// The calling serializer. public sealed override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { + if (writer == null) + throw new ArgumentNullException("writer"); + + if (serializer == null) + throw new ArgumentNullException("serializer"); + if (value == null) { writer.WriteNull(); @@ -45,6 +51,12 @@ namespace ICD.Common.Utils.Json public sealed override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { + if (reader == null) + throw new ArgumentNullException("reader"); + + if (serializer == null) + throw new ArgumentNullException("serializer"); + return ReadJson(reader, (T)existingValue, serializer); } diff --git a/ICD.Common.Utils/Json/JsonUtils.cs b/ICD.Common.Utils/Json/JsonUtils.cs index d5eea96..8bb7a9c 100644 --- a/ICD.Common.Utils/Json/JsonUtils.cs +++ b/ICD.Common.Utils/Json/JsonUtils.cs @@ -37,6 +37,9 @@ namespace ICD.Common.Utils.Json /// public static void CacheType(Type type) { + if (type == null) + throw new ArgumentNullException("type"); + string serialized = JsonConvert.SerializeObject(ReflectionUtils.CreateInstance(type)); JsonConvert.DeserializeObject(serialized, type); } @@ -91,7 +94,11 @@ namespace ICD.Common.Utils.Json [PublicAPI] public static void Print(string json) { - IcdConsole.PrintLine(Format(json)); + if (json == null) + throw new ArgumentNullException("json"); + + string formatted = Format(json); + IcdConsole.PrintLine(formatted); } /// @@ -113,6 +120,9 @@ namespace ICD.Common.Utils.Json [PublicAPI] public static string Format(string json) { + if (json == null) + throw new ArgumentNullException("json"); + int indent = 0; bool quoted = false; StringBuilder sb = new StringBuilder(); @@ -298,6 +308,12 @@ namespace ICD.Common.Utils.Json /// public static object Deserialize(Type type, JToken token) { + if (type == null) + throw new ArgumentNullException("type"); + + if (token == null) + throw new ArgumentNullException("token"); + return Deserialize(type, token, new JsonSerializer()); } @@ -310,6 +326,15 @@ namespace ICD.Common.Utils.Json /// public static object Deserialize(Type type, JToken token, JsonSerializer serializer) { + if (type == null) + throw new ArgumentNullException("type"); + + if (token == null) + throw new ArgumentNullException("token"); + + if (serializer == null) + throw new ArgumentNullException("serializer"); + using (JTokenReader jsonReader = new JTokenReader(token)) return serializer.Deserialize(jsonReader, type); }