[tomboy] Add Hyena.Json.Serializer, associated tests, and a README for contributors.
- From: Sanford Armstrong <sharm src gnome org>
- To: svn-commits-list gnome org
- Subject: [tomboy] Add Hyena.Json.Serializer, associated tests, and a README for contributors.
- Date: Mon, 18 May 2009 10:23:07 -0400 (EDT)
commit 864993c7b12b130eaa0fdc6ca9c83e85611a8fb5
Author: Sandy Armstrong <sanfordarmstrong gmail com>
Date: Sun May 17 07:29:51 2009 -0700
Add Hyena.Json.Serializer, associated tests, and a README for contributors.
---
Tomboy/Addins/WebSyncService/Hyena.Json/README | 16 ++
.../Addins/WebSyncService/Hyena.Json/Serializer.cs | 146 +++++++++++++++++
.../Hyena.Json/Tests/SerializerTests.cs | 166 ++++++++++++++++++++
3 files changed, 328 insertions(+), 0 deletions(-)
diff --git a/Tomboy/Addins/WebSyncService/Hyena.Json/README b/Tomboy/Addins/WebSyncService/Hyena.Json/README
new file mode 100644
index 0000000..5f40d19
--- /dev/null
+++ b/Tomboy/Addins/WebSyncService/Hyena.Json/README
@@ -0,0 +1,16 @@
+About Hyena.Json
+----------------
+Hyena.Json is written by Aaron Bockover, copyright Novell, Inc,
+and distributed under the MIT/X11 license. It is borrowed from
+the source of the Banshee media player.
+
+
+About Hyena.Json in Tomboy
+--------------------------
+Banshee uses a different C# style guideline than Tomboy does.
+Please follow the Banshee guidelines when working in Hyena.Json,
+to make upstream collaboration more efficient.
+
+So far, the only additions Tomboy has made to Hyena.Json are
+Hyena.Json.Serializer and its associated unit tests, and specific
+support for integers in the Tokenizer and Deserializer.
\ No newline at end of file
diff --git a/Tomboy/Addins/WebSyncService/Hyena.Json/Serializer.cs b/Tomboy/Addins/WebSyncService/Hyena.Json/Serializer.cs
new file mode 100644
index 0000000..24eb064
--- /dev/null
+++ b/Tomboy/Addins/WebSyncService/Hyena.Json/Serializer.cs
@@ -0,0 +1,146 @@
+// 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.
+//
+// Copyright (c) 2008 Novell, Inc. (http://www.novell.com)
+//
+// Authors:
+// Sandy Armstrong <sanfordarmstrong gmail com>
+//
+
+using System;
+using System.IO;
+using System.Text;
+
+namespace Hyena.Json
+{
+ public class Serializer
+ {
+ private const string serializedNull = "null";
+ private const string serializedTrue = "true";
+ private const string serializedFalse = "false";
+
+ private object input;
+
+ public Serializer () { }
+ public Serializer (object input) { SetInput (input); }
+
+ public void SetInput (object input)
+ {
+ this.input = input;
+ }
+
+ // TODO: Support serialize to stream?
+
+ public string Serialize ()
+ {
+ return Serialize (input);
+ }
+
+ private string SerializeBool (bool val)
+ {
+ return val ? serializedTrue : serializedFalse;
+ }
+
+ private string SerializeInt (int val)
+ {
+ return val.ToString ();
+ }
+
+ private string SerializeDouble (double val)
+ {
+ return val.ToString ();
+ }
+
+ // TODO: exponent stuff
+
+ private string SerializeString (string val)
+ {
+ // TODO: More work, escaping, etc
+ return "\"" +
+ val.Replace ("\\", "\\\\").
+ Replace ("\"", "\\\"").
+ Replace ("\b", "\\b").
+ Replace ("\f", "\\f").
+ Replace ("\n", "\\n").
+ Replace ("\r", "\\r").
+ Replace ("\t", "\\t") +
+ "\"";
+ }
+
+ private string SerializeArray (JsonArray array)
+ {
+ StringBuilder builder = new StringBuilder ("[");
+ for (int i = 0; i < array.Count; i++) {
+ builder.Append (Serialize (array [i]));
+ if (i != (array.Count -1))
+ builder.Append (",");
+ }
+ builder.Append ("]");
+ return builder.ToString ();
+ }
+
+ private string SerializeObject (JsonObject obj)
+ {
+ StringBuilder builder = new StringBuilder ("{");
+ foreach (var pair in obj) {
+ builder.Append (SerializeString (pair.Key));
+ builder.Append (":");
+ builder.Append (Serialize (pair.Value));
+ builder.Append (",");
+ }
+ // Get rid of trailing comma
+ if (obj.Count > 0)
+ builder.Remove (builder.Length - 1, 1);
+ builder.Append ("}");
+ return builder.ToString ();
+ }
+
+ private string Serialize (object unknownObj)
+ {
+ if (unknownObj == null)
+ return serializedNull;
+
+ bool? b = unknownObj as bool?;
+ if (b.HasValue)
+ return SerializeBool (b.Value);
+
+ int? i = unknownObj as int?;
+ if (i.HasValue)
+ return SerializeInt (i.Value);
+
+ double? d = unknownObj as double?;
+ if (d.HasValue)
+ return SerializeDouble (d.Value);
+
+ string s = unknownObj as string;
+ if (s != null)
+ return SerializeString (s);
+
+ JsonObject o = unknownObj as JsonObject;
+ if (o != null)
+ return SerializeObject (o);
+
+ JsonArray a = unknownObj as JsonArray;
+ if (a != null)
+ return SerializeArray (a);
+
+ throw new ArgumentException ("Cannot serialize anything but doubles, integers, strings, JsonObjects, and JsonArrays");
+ }
+ }
+}
diff --git a/Tomboy/Addins/WebSyncService/Hyena.Json/Tests/SerializerTests.cs b/Tomboy/Addins/WebSyncService/Hyena.Json/Tests/SerializerTests.cs
new file mode 100644
index 0000000..723e17e
--- /dev/null
+++ b/Tomboy/Addins/WebSyncService/Hyena.Json/Tests/SerializerTests.cs
@@ -0,0 +1,166 @@
+// 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.
+//
+// Copyright (c) 2008 Novell, Inc. (http://www.novell.com)
+//
+// Authors:
+// Sandy Armstrong <sanfordarmstrong gmail com>
+//
+
+#if ENABLE_TESTS
+
+using System;
+
+using Hyena.Json;
+
+using NUnit.Framework;
+
+namespace Hyena.Json.Tests
+{
+ [TestFixture]
+ public class SerializerTests
+ {
+ [Test]
+ public void SerializeBoolTest ()
+ {
+ Serializer ser = new Serializer (true);
+ Assert.AreEqual ("true", ser.Serialize ());
+ ser.SetInput (false);
+ Assert.AreEqual ("false", ser.Serialize ());
+ }
+
+ [Test]
+ public void SerializeIntTest ()
+ {
+ Serializer ser = new Serializer (12);
+ Assert.AreEqual ("12", ser.Serialize ());
+ ser.SetInput (-658);
+ Assert.AreEqual ("-658", ser.Serialize ());
+ ser.SetInput (0);
+ Assert.AreEqual ("0", ser.Serialize ());
+ }
+
+ [Test]
+ public void SerializeDoubleTest ()
+ {
+ Serializer ser = new Serializer (12.5);
+ Assert.AreEqual ("12.5", ser.Serialize ());
+ ser.SetInput (-658.1);
+ Assert.AreEqual ("-658.1", ser.Serialize ());
+ ser.SetInput (0.0);
+ Assert.AreEqual ("0", ser.Serialize ());
+ ser.SetInput (0.1);
+ Assert.AreEqual ("0.1", ser.Serialize ());
+ }
+
+ [Test]
+ public void SerializeStringTest ()
+ {
+ VerifyString ("The cat\njumped \"over\" the rat! We escape with \\\"",
+ @"""The cat\njumped \""over\"" the rat! We escape with \\\""""");
+ }
+
+ [Test]
+ public void EscapedCharactersTest ()
+ {
+ VerifyString ("\\", @"""\\""");
+ VerifyString ("\b", @"""\b""");
+ VerifyString ("\f", @"""\f""");
+ VerifyString ("\n", @"""\n""");
+ VerifyString ("\r", @"""\r""");
+ VerifyString ("\t", @"""\t""");
+ VerifyString ("\u2022", "\"\u2022\"");
+ }
+
+ private void VerifyString (string original, string expectedSerialized)
+ {
+ Serializer ser = new Serializer (original);
+ string output = ser.Serialize ();
+ Assert.AreEqual (expectedSerialized, output, "Serialized Output");
+ Assert.AreEqual (original, new Deserializer (output).Deserialize (),
+ "Input should be identical after serialized then deserialized back to string");
+ }
+
+ [Test]
+ public void SerializeNullTest ()
+ {
+ Serializer ser = new Serializer (null);
+ Assert.AreEqual ("null", ser.Serialize ());
+ }
+
+ [Test]
+ public void SerializeArrayTest ()
+ {
+ JsonArray simpleArray = new JsonArray ();
+ simpleArray.Add (1);
+ simpleArray.Add ("text");
+ simpleArray.Add (0.1);
+ simpleArray.Add ("5");
+ simpleArray.Add (false);
+ simpleArray.Add (null);
+
+ Serializer ser = new Serializer (simpleArray);
+ Assert.AreEqual ("[1,\"text\",0.1,\"5\",false,null]",
+ ser.Serialize ());
+
+ JsonArray emptyArray = new JsonArray ();
+ ser.SetInput (emptyArray);
+ Assert.AreEqual ("[]", ser.Serialize ());
+ }
+
+ // TODO: Test arrays/objects in each other, various levels deep
+
+ [Test]
+ public void SerializeObjectTest ()
+ {
+ JsonObject obj1 = new JsonObject ();
+ obj1 ["intfield"] = 1;
+ obj1 ["text field"] = "text";
+ obj1 ["double-field"] = 0.1;
+ obj1 ["Boolean, Field"] = true;
+ obj1 ["\"Null\"\nfield"] = null;
+
+ Serializer ser = new Serializer (obj1);
+ // Test object equality, since order not guaranteed
+ string output = ser.Serialize ();
+ JsonObject reconstitutedObj1 = (JsonObject) new Deserializer (output).Deserialize ();
+ AssertJsonObjectsEqual (obj1, reconstitutedObj1);
+
+ JsonObject obj2 = new JsonObject ();
+ obj2 ["double-field"] = 0.1;
+ obj2 ["\"Null\"\nfield"] = null;
+ ser.SetInput (obj2);
+ output = ser.Serialize ();
+ Assert.IsTrue ((output == "{\"double-field\":0.1,\"\\\"Null\\\"\\nfield\":null}") ||
+ (output == "{\"\\\"Null\\\"\\nfield\":null,\"double-field\":0.1}"),
+ "Serialized output format");
+ }
+
+ private void AssertJsonObjectsEqual (JsonObject expectedObj, JsonObject actualObj)
+ {
+ Assert.AreEqual (expectedObj.Count, actualObj.Count, "Field count");
+ foreach (var expectedPair in expectedObj)
+ Assert.AreEqual (expectedPair.Value,
+ actualObj [expectedPair.Key],
+ expectedPair.Key + " field");
+ }
+ }
+}
+
+#endif
\ No newline at end of file
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]