From 46a1ea09b6b2bb81c03316021b8be6153e317cbe Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Mon, 4 Feb 2019 12:03:15 -0500 Subject: [PATCH] refactor: Moving JSON type name util into TypeExtensions --- .../Extensions/JsonWriterExtensions.cs | 84 +------------------ ICD.Common.Utils/Extensions/TypeExtensions.cs | 73 ++++++++++++++++ 2 files changed, 74 insertions(+), 83 deletions(-) diff --git a/ICD.Common.Utils/Extensions/JsonWriterExtensions.cs b/ICD.Common.Utils/Extensions/JsonWriterExtensions.cs index acfb591..dd82f32 100644 --- a/ICD.Common.Utils/Extensions/JsonWriterExtensions.cs +++ b/ICD.Common.Utils/Extensions/JsonWriterExtensions.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Text; using ICD.Common.Properties; using Newtonsoft.Json; @@ -8,16 +7,6 @@ namespace ICD.Common.Utils.Extensions { public static class JsonWriterExtensions { - private static readonly Dictionary s_TypeToName; - - /// - /// Static constructor. - /// - static JsonWriterExtensions() - { - s_TypeToName = new Dictionary(); - } - /// /// Writes the type value. /// @@ -35,7 +24,7 @@ namespace ICD.Common.Utils.Extensions return; } - string name = GetTypeName(type); + string name = type.GetNameWithoutAssemblyDetails(); extends.WriteValue(name); } @@ -90,76 +79,5 @@ namespace ICD.Common.Utils.Extensions } writer.WriteEndArray(); } - - /// - /// Gets the string representation foe the given type. - /// - /// - /// - private static string GetTypeName(Type type) - { - if (type == null) - throw new ArgumentNullException("type"); - - string name; - if (!s_TypeToName.TryGetValue(type, out name)) - { - name = RemoveAssemblyDetails(type.AssemblyQualifiedName); - s_TypeToName.Add(type, name); - } - - return name; - } - - /// - /// Taken from Newtonsoft.Json.Utilities.ReflectionUtils - /// Removes the assembly details from a type assembly qualified name. - /// - /// - /// - private static string RemoveAssemblyDetails(string fullyQualifiedTypeName) - { - StringBuilder builder = new StringBuilder(); - - // loop through the type name and filter out qualified assembly details from nested type names - bool writingAssemblyName = false; - bool skippingAssemblyDetails = false; - for (int i = 0; i < fullyQualifiedTypeName.Length; i++) - { - char current = fullyQualifiedTypeName[i]; - switch (current) - { - case '[': - writingAssemblyName = false; - skippingAssemblyDetails = false; - builder.Append(current); - break; - case ']': - writingAssemblyName = false; - skippingAssemblyDetails = false; - builder.Append(current); - break; - case ',': - if (!writingAssemblyName) - { - writingAssemblyName = true; - builder.Append(current); - } - else - { - skippingAssemblyDetails = true; - } - break; - default: - if (!skippingAssemblyDetails) - { - builder.Append(current); - } - break; - } - } - - return builder.ToString(); - } } } diff --git a/ICD.Common.Utils/Extensions/TypeExtensions.cs b/ICD.Common.Utils/Extensions/TypeExtensions.cs index 73f9336..dc5f471 100644 --- a/ICD.Common.Utils/Extensions/TypeExtensions.cs +++ b/ICD.Common.Utils/Extensions/TypeExtensions.cs @@ -62,6 +62,7 @@ namespace ICD.Common.Utils.Extensions private static readonly Dictionary s_TypeBaseTypes; private static readonly Dictionary s_TypeImmediateInterfaces; private static readonly Dictionary s_TypeMinimalInterfaces; + private static readonly Dictionary s_TypeToNameWithoutAssemblyDetails; /// /// Static constructor. @@ -72,6 +73,7 @@ namespace ICD.Common.Utils.Extensions s_TypeBaseTypes = new Dictionary(); s_TypeImmediateInterfaces = new Dictionary(); s_TypeMinimalInterfaces = new Dictionary(); + s_TypeToNameWithoutAssemblyDetails = new Dictionary(); } /// @@ -307,6 +309,77 @@ namespace ICD.Common.Utils.Extensions return index == -1 ? name : name.Substring(0, index); } + /// + /// Gets the string representation for the type. + /// + /// + /// + public static string GetNameWithoutAssemblyDetails(this Type extends) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + string name; + if (!s_TypeToNameWithoutAssemblyDetails.TryGetValue(extends, out name)) + { + name = RemoveAssemblyDetails(extends.AssemblyQualifiedName); + s_TypeToNameWithoutAssemblyDetails.Add(extends, name); + } + + return name; + } + + /// + /// Taken from Newtonsoft.Json.Utilities.ReflectionUtils + /// Removes the assembly details from a type assembly qualified name. + /// + /// + /// + private static string RemoveAssemblyDetails(string fullyQualifiedTypeName) + { + StringBuilder builder = new StringBuilder(); + + // loop through the type name and filter out qualified assembly details from nested type names + bool writingAssemblyName = false; + bool skippingAssemblyDetails = false; + for (int i = 0; i < fullyQualifiedTypeName.Length; i++) + { + char current = fullyQualifiedTypeName[i]; + switch (current) + { + case '[': + writingAssemblyName = false; + skippingAssemblyDetails = false; + builder.Append(current); + break; + case ']': + writingAssemblyName = false; + skippingAssemblyDetails = false; + builder.Append(current); + break; + case ',': + if (!writingAssemblyName) + { + writingAssemblyName = true; + builder.Append(current); + } + else + { + skippingAssemblyDetails = true; + } + break; + default: + if (!skippingAssemblyDetails) + { + builder.Append(current); + } + break; + } + } + + return builder.ToString(); + } + /// /// Gets the type name as it would appear in code. ///