[hyena] Add support for serializing IGroupings



commit 6c4b97db752dbb96533ea2ec68f9994e9f10a013
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Fri Feb 12 09:32:25 2010 -0800

    Add support for serializing IGroupings

 src/Hyena/Hyena.Json/ExtensionMethods.cs      |   38 +++++++++++++++++++++---
 src/Hyena/Hyena.Json/Tests/SerializerTests.cs |   10 ++++++-
 2 files changed, 42 insertions(+), 6 deletions(-)
---
diff --git a/src/Hyena/Hyena.Json/ExtensionMethods.cs b/src/Hyena/Hyena.Json/ExtensionMethods.cs
index 0b1de90..83fb9d2 100644
--- a/src/Hyena/Hyena.Json/ExtensionMethods.cs
+++ b/src/Hyena/Hyena.Json/ExtensionMethods.cs
@@ -25,6 +25,7 @@
 // THE SOFTWARE.
 
 using System;
+using System.Linq;
 using System.Collections;
 using System.Collections.Generic;
 using System.Text;
@@ -61,6 +62,35 @@ namespace Hyena.Json
             sb.AppendFormat ("{0}}}\n", String.Empty.PadLeft ((level - 1) * 2, ' '));
         }
 
+        // System.Linq.IGrouping serializer
+        public static string ToJsonString<K, V> (this IEnumerable<IGrouping<K, V>> obj)
+        {
+            return obj.ToJsonString (1);
+        }
+
+        public static string ToJsonString<K, V> (this IEnumerable<IGrouping<K, V>> obj, int level)
+        {
+            var sb = new StringBuilder ();
+            obj.ToJsonString (sb, level);
+            return sb.ToString ();
+        }
+
+        public static void ToJsonString<K, V> (this IEnumerable<IGrouping<K, V>> obj, StringBuilder sb, int level)
+        {
+            bool first = true;
+
+            sb.Append ("{");
+            foreach (var item in obj) {
+                if (first) {
+                    first = false;
+                    sb.AppendLine ();
+                }
+                sb.AppendFormat ("{0}\"{1}\" : ", String.Empty.PadLeft (level * 2, ' '), item.Key);
+                item.ToJsonString (sb, level + 1);
+            }
+            sb.AppendFormat ("{0}}}\n", first ? " " : String.Empty.PadLeft ((level - 1) * 2, ' '));
+        }
+
         // JsonArray serializer
         public static string ToJsonString (this IEnumerable list)
         {
@@ -88,11 +118,7 @@ namespace Hyena.Json
                 item.ToJsonString (sb, level + 1);
             }
 
-            if (first) {
-                sb.Append (" ");
-            }
-
-            sb.AppendFormat ("{0}]\n", first ? "" : String.Empty.PadLeft ((level - 1) * 2, ' '));
+            sb.AppendFormat ("{0}]\n", first ? " " : String.Empty.PadLeft ((level - 1) * 2, ' '));
         }
 
         // Utility method
@@ -100,6 +126,8 @@ namespace Hyena.Json
         {
             if (item is Dictionary<string, object>) {
                 ((Dictionary<string, object>)item).ToJsonString (sb, level);
+            //} else if (item is IGrouping) {
+                //((IGrouping)item).ToJsonString (sb, level);
             } else if (item is IEnumerable && !(item is string)) {
                 ((IEnumerable)item).ToJsonString (sb, level);
             } else {
diff --git a/src/Hyena/Hyena.Json/Tests/SerializerTests.cs b/src/Hyena/Hyena.Json/Tests/SerializerTests.cs
index 1e3e46a..b427b5b 100644
--- a/src/Hyena/Hyena.Json/Tests/SerializerTests.cs
+++ b/src/Hyena/Hyena.Json/Tests/SerializerTests.cs
@@ -32,6 +32,7 @@ using System.Reflection;
 using NUnit.Framework;
 
 using Hyena.Json;
+using System.Collections;
 
 namespace Hyena.Json.Tests
 {
@@ -83,9 +84,16 @@ namespace Hyena.Json.Tests
             );
 
             Assert.AreEqual (
-                "[\n  [\n    0\n    2\n  ]\n  [\n    1\n    3\n  ]\n]\n",
+                "{\n  \"True\" : [\n    0\n    2\n  ]\n  \"False\" : [\n    1\n    3\n  ]\n}\n",
                  Enumerable.Range (0, 4).GroupBy<int, bool> (i => i % 2 == 0).ToJsonString ()
             );
+
+            /*var a = new ArrayList ();
+            a.Add (Enumerable.Range (0, 4).GroupBy<int, bool> (i => i % 2 == 0));
+            Assert.AreEqual (
+                "",
+                a.ToJsonString ()
+            );*/
         }
     }
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]