Bläddra i källkod

Added support for custom key formatters

Brad Robinson 2 år sedan
förälder
incheckning
6dfd5f5cd3
2 ändrade filer med 47 tillägg och 2 borttagningar
  1. 28 0
      Topten.JsonKit/JsonKit.cs
  2. 19 2
      Topten.JsonKit/JsonWriter.cs

+ 28 - 0
Topten.JsonKit/JsonKit.cs

@@ -443,6 +443,34 @@ namespace Topten.JsonKit
             JsonWriter._formatters.Set(type, formatter);
         }
 
+        /// <summary>
+        /// Register a callback that to format the key values of dictionaries
+        /// </summary>
+        /// <remarks>
+        /// These formatters are only used when writing .NET dictionary 
+        /// key instances - not when writing properties names.
+        /// </remarks>
+        /// <param name="type">The type of object to be formatted</param>
+        /// <param name="formatter">The formatter callback</param>
+        public static void RegisterKeyFormatter(Type type, Func<object, string> formatter)
+        {
+            JsonWriter._keyFormatters.Set(type, formatter);
+        }
+
+        /// <summary>
+        /// Register a callback that to format the key values of dictionaries
+        /// </summary>
+        /// <remarks>
+        /// These formatters are only used when writing .NET dictionary 
+        /// key instances - not when writing properties names.
+        /// </remarks>
+        /// <typeparam name="T">The type of object to be formatted</typeparam>
+        /// <param name="formatter">The formatter callback</param>
+        public static void RegisterKeyFormatter<T>(Func<T, string> formatter)
+        {
+            JsonWriter._keyFormatters.Set(typeof(T), (o) => formatter((T)o));
+        }
+
         /// <summary>
         /// Register a callback that can format a value of a particular type into json 
         /// </summary>

+ 19 - 2
Topten.JsonKit/JsonWriter.cs

@@ -54,6 +54,7 @@ namespace Topten.JsonKit
 
         public static Func<Type, Action<IJsonWriter, object>> _formatterResolver;
         public static ThreadSafeCache<Type, Action<IJsonWriter, object>> _formatters = new ThreadSafeCache<Type, Action<IJsonWriter, object>>();
+        public static ThreadSafeCache<Type, Func<object, string>> _keyFormatters = new ThreadSafeCache<Type, Func<object, string>>();
 
         static Action<IJsonWriter, object> ResolveFormatter(Type type)
         {
@@ -130,6 +131,22 @@ namespace Topten.JsonKit
             NextElement();
         }
 
+        // Writes a dictionary key using custom formatter if available
+        void WriteDictionaryKey(object value)
+        {
+            string str;
+            Func<object, string> formatter;
+            if (_keyFormatters.TryGetValue(value.GetType(), out formatter))
+            {
+                str = formatter(value);
+            }
+            else
+            {
+                str = value.ToString();
+            }
+            WriteKey(str);
+        }
+
         // Write next dictionary key
         public void WriteKey(string key)
         {
@@ -298,7 +315,7 @@ namespace Topten.JsonKit
                 {
                     foreach (var key in d.Keys)
                     {
-                        WriteKey(key.ToString());
+                        WriteDictionaryKey(key);
                         WriteValue(d[key]);
                     }
                 });
@@ -313,7 +330,7 @@ namespace Topten.JsonKit
                 {
                     foreach (var key in dso.Keys)
                     {
-                        WriteKey(key.ToString());
+                        WriteDictionaryKey(key);
                         WriteValue(dso[key]);
                     }
                 });