[banshee] [Hyena.Json] Factor Dump methods into a serializer



commit 2164b5e9fc37dbdf02f3dfe4b5ac8139ead4e680
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Fri Feb 12 08:06:44 2010 -0800

    [Hyena.Json] Factor Dump methods into a serializer
    
    You can get the string representation of a JsonObject or JsonArray
    object via ToString or Dump (StringBuffer, int level).  Add unit tests
    for the serialization.

 src/Libraries/Hyena/Hyena.Json/IJsonCollection.cs  |    2 +
 src/Libraries/Hyena/Hyena.Json/JsonArray.cs        |   32 +++++++--
 src/Libraries/Hyena/Hyena.Json/JsonObject.cs       |   32 +++++++--
 .../Hyena/Hyena.Json/Tests/SerializerTests.cs      |   75 ++++++++++++++++++++
 src/Libraries/Hyena/Hyena.csproj                   |    1 +
 src/Libraries/Hyena/Makefile.am                    |    1 +
 6 files changed, 129 insertions(+), 14 deletions(-)
---
diff --git a/src/Libraries/Hyena/Hyena.Json/IJsonCollection.cs b/src/Libraries/Hyena/Hyena.Json/IJsonCollection.cs
index c9fc698..d995a5e 100644
--- a/src/Libraries/Hyena/Hyena.Json/IJsonCollection.cs
+++ b/src/Libraries/Hyena/Hyena.Json/IJsonCollection.cs
@@ -28,6 +28,7 @@
 
 using System;
 using System.Collections;
+using System.Text;
 
 namespace Hyena.Json
 {
@@ -35,5 +36,6 @@ namespace Hyena.Json
     {
         void Dump ();
         void Dump (int count);
+        void Dump (StringBuilder sb, int level);
     }
 }
diff --git a/src/Libraries/Hyena/Hyena.Json/JsonArray.cs b/src/Libraries/Hyena/Hyena.Json/JsonArray.cs
index 5d86826..23fff5f 100644
--- a/src/Libraries/Hyena/Hyena.Json/JsonArray.cs
+++ b/src/Libraries/Hyena/Hyena.Json/JsonArray.cs
@@ -28,6 +28,7 @@
 
 using System;
 using System.Collections.Generic;
+using System.Text;
 
 namespace Hyena.Json
 {
@@ -38,23 +39,40 @@ namespace Hyena.Json
             Dump (1);
         }
 
-        public void Dump (int levels)
+        public void Dump (int level)
+        {
+            Console.Write (ToString (level));
+        }
+
+        public override string ToString ()
+        {
+            return ToString (1);
+        }
+
+        public string ToString (int level)
+        {
+            var sb = new StringBuilder ();
+            Dump (sb, level);
+            return sb.ToString ();
+        }
+
+        public void Dump (StringBuilder sb, int level)
         {
             if (Count == 0) {
-                Console.WriteLine ("[ ]");
+                sb.AppendLine ("[ ]");
                 return;
             }
 
-            Console.WriteLine ("[");
+            sb.AppendLine ("[");
             foreach (object item in this) {
-                Console.Write (String.Empty.PadLeft (levels * 2, ' '));
+                sb.Append (String.Empty.PadLeft (level * 2, ' '));
                 if (item is IJsonCollection) {
-                    ((IJsonCollection)item).Dump (levels + 1);
+                    ((IJsonCollection)item).Dump (sb, level + 1);
                 } else {
-                    Console.WriteLine (item);
+                    sb.AppendLine (item.ToString ());
                 }
             }
-            Console.WriteLine ("{0}]", String.Empty.PadLeft ((levels - 1) * 2, ' '));
+            sb.AppendFormat ("{0}]\n", String.Empty.PadLeft ((level - 1) * 2, ' '));
         }
     }
 }
diff --git a/src/Libraries/Hyena/Hyena.Json/JsonObject.cs b/src/Libraries/Hyena/Hyena.Json/JsonObject.cs
index a9216b1..4ec2091 100644
--- a/src/Libraries/Hyena/Hyena.Json/JsonObject.cs
+++ b/src/Libraries/Hyena/Hyena.Json/JsonObject.cs
@@ -28,6 +28,7 @@
 
 using System;
 using System.Collections.Generic;
+using System.Text;
 
 namespace Hyena.Json
 {
@@ -38,23 +39,40 @@ namespace Hyena.Json
             Dump (1);
         }
 
-        public void Dump (int levels)
+        public void Dump (int level)
+        {
+            Console.Write (ToString (level));
+        }
+
+        public override string ToString ()
+        {
+            return ToString (1);
+        }
+
+        public string ToString (int level)
+        {
+            var sb = new StringBuilder ();
+            Dump (sb, level);
+            return sb.ToString ();
+        }
+
+        public void Dump (StringBuilder sb, int level)
         {
             if (Count == 0) {
-                Console.WriteLine ("{ }");
+                sb.AppendLine ("{ }");
                 return;
             }
 
-            Console.WriteLine ("{");
+            sb.AppendLine ("{");
             foreach (KeyValuePair<string, object> item in this) {
-                Console.Write ("{0}\"{1}\" : ", String.Empty.PadLeft (levels * 2, ' '), item.Key);
+                sb.AppendFormat ("{0}\"{1}\" : ", String.Empty.PadLeft (level * 2, ' '), item.Key);
                 if (item.Value is IJsonCollection) {
-                    ((IJsonCollection)item.Value).Dump (levels + 1);
+                    ((IJsonCollection)item.Value).Dump (sb, level + 1);
                 } else {
-                    Console.WriteLine (item.Value);
+                    sb.AppendLine (item.Value.ToString ());
                 }
             }
-            Console.WriteLine ("{0}}}", String.Empty.PadLeft ((levels - 1) * 2, ' '));
+            sb.AppendFormat ("{0}}}\n", String.Empty.PadLeft ((level - 1) * 2, ' '));
         }
     }
 }
diff --git a/src/Libraries/Hyena/Hyena.Json/Tests/SerializerTests.cs b/src/Libraries/Hyena/Hyena.Json/Tests/SerializerTests.cs
new file mode 100644
index 0000000..e9c70c9
--- /dev/null
+++ b/src/Libraries/Hyena/Hyena.Json/Tests/SerializerTests.cs
@@ -0,0 +1,75 @@
+//
+// SerializerTests.cs
+//
+// Author:
+//   Gabriel Burt <gabriel burt gmail com>
+//
+// Copyright (c) 2010 Gabriel Burt
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+#if ENABLE_TESTS
+
+using System;
+using System.Reflection;
+using NUnit.Framework;
+
+using Hyena.Json;
+
+namespace Hyena.Json.Tests
+{
+    [TestFixture]
+    public class SerializerTests : Hyena.Tests.TestBase
+    {
+        JsonObject obj;
+        const string obj_serialized = "{\n  \"foo\" : bar\n  \"baz\" : 12,2\n}\n";
+        const string array_serialized = "[\n  foo\n  {\n    \"foo\" : bar\n    \"baz\" : 12,2\n  }\n]\n";
+
+        [TestFixtureSetUp]
+        public void Setup ()
+        {
+            obj = new JsonObject ();
+            obj["foo"] = "bar";
+            obj["baz"] = 12.2;
+        }
+
+        [Test]
+        public void Literal ()
+        {
+            Assert.AreEqual (obj_serialized, obj.ToString ());
+        }
+
+        [Test]
+        public void Array ()
+        {
+            var a = new JsonArray ();
+            a.Add ("foo");
+            a.Add (obj);
+
+            Assert.AreEqual (array_serialized, a.ToString ());
+        }
+
+        [Test]
+        public void ExtensionMethods ()
+        {
+        }
+    }
+}
+
+#endif
diff --git a/src/Libraries/Hyena/Hyena.csproj b/src/Libraries/Hyena/Hyena.csproj
index 0e72bc8..71171a5 100644
--- a/src/Libraries/Hyena/Hyena.csproj
+++ b/src/Libraries/Hyena/Hyena.csproj
@@ -177,6 +177,7 @@
     <Compile Include="Hyena.Metrics\ISampleStore.cs" />
     <Compile Include="Hyena.Metrics\Tests\MetricsTests.cs" />
     <Compile Include="Hyena.Metrics\MemorySampleStore.cs" />
+    <Compile Include="Hyena.Json\Tests\SerializerTests.cs" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="Mono.Posix">
diff --git a/src/Libraries/Hyena/Makefile.am b/src/Libraries/Hyena/Makefile.am
index 814e47a..9711362 100644
--- a/src/Libraries/Hyena/Makefile.am
+++ b/src/Libraries/Hyena/Makefile.am
@@ -62,6 +62,7 @@ SOURCES =  \
 	Hyena.Json/JsonArray.cs \
 	Hyena.Json/JsonObject.cs \
 	Hyena.Json/Tests/DeserializerTests.cs \
+	Hyena.Json/Tests/SerializerTests.cs \
 	Hyena.Json/Tests/TokenizerTests.cs \
 	Hyena.Json/Token.cs \
 	Hyena.Json/Tokenizer.cs \



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