[hyena] Nuke old serializer, improve new one
- From: Gabriel Burt <gburt src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [hyena] Nuke old serializer, improve new one
- Date: Wed, 26 May 2010 02:35:06 +0000 (UTC)
commit bd86709839f6a0c7db48d857b48421d74b0aeb68
Author: Gabriel Burt <gabriel burt gmail com>
Date: Thu Feb 18 17:04:57 2010 -0800
Nuke old serializer, improve new one
Sandy's new serializer can now handle IEnumerables and any
Dictinonary<s,o> types like my ExtensionMethods could, but the
serialization is much higher quality (actually deserializable)!
Change Hyena.Metrics to use the new serialization API, and test that the
JSON it generates can be deserialized.
src/Hyena/Hyena.Json/ExtensionMethods.cs | 138 ----------------------
src/Hyena/Hyena.Json/JsonArray.cs | 4 +-
src/Hyena/Hyena.Json/JsonObject.cs | 4 +-
src/Hyena/Hyena.Json/Serializer.cs | 83 +++++++------
src/Hyena/Hyena.Json/Tests/OldSerializerTests.cs | 101 ----------------
src/Hyena/Hyena.Metrics/HttpPoster.cs | 1 +
src/Hyena/Hyena.Metrics/MetricsCollection.cs | 2 +-
src/Hyena/Hyena.Metrics/Tests/MetricsTests.cs | 13 ++-
src/Hyena/Hyena.csproj | 2 +-
src/Hyena/Makefile.am | 1 -
10 files changed, 64 insertions(+), 285 deletions(-)
---
diff --git a/src/Hyena/Hyena.Json/JsonArray.cs b/src/Hyena/Hyena.Json/JsonArray.cs
index e1d5331..6f551d6 100644
--- a/src/Hyena/Hyena.Json/JsonArray.cs
+++ b/src/Hyena/Hyena.Json/JsonArray.cs
@@ -42,12 +42,12 @@ namespace Hyena.Json
public void Dump (int level)
{
- Console.Write (this.ToJsonString (level));
+ Console.Write (ToString ());
}
public override string ToString ()
{
- return this.ToJsonString ();
+ return new Serializer (this).Serialize ();
}
}
}
diff --git a/src/Hyena/Hyena.Json/JsonObject.cs b/src/Hyena/Hyena.Json/JsonObject.cs
index 22175ff..34f8328 100644
--- a/src/Hyena/Hyena.Json/JsonObject.cs
+++ b/src/Hyena/Hyena.Json/JsonObject.cs
@@ -42,12 +42,12 @@ namespace Hyena.Json
public void Dump (int level)
{
- Console.Write (this.ToJsonString (level));
+ Console.Write (ToString ());
}
public override string ToString ()
{
- return this.ToJsonString ();
+ return new Serializer (this).Serialize ();
}
}
}
diff --git a/src/Hyena/Hyena.Json/Serializer.cs b/src/Hyena/Hyena.Json/Serializer.cs
index 4eb781e..a298528 100644
--- a/src/Hyena/Hyena.Json/Serializer.cs
+++ b/src/Hyena/Hyena.Json/Serializer.cs
@@ -1,29 +1,34 @@
-// 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>
-//
+//
+// Serializer.cs
+//
+// Copyright (c) 2009 Novell, Inc. (http://www.novell.com)
+//
+// Authors:
+// Sandy Armstrong <sanfordarmstrong gmail com>
+//
+// 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.
+//
using System;
+using System.Collections;
+using System.Collections.Generic;
using System.IO;
using System.Text;
@@ -51,7 +56,7 @@ namespace Hyena.Json
{
return Serialize (input);
}
-
+
private string SerializeBool (bool val)
{
return val ? serializedTrue : serializedFalse;
@@ -83,19 +88,23 @@ namespace Hyena.Json
"\"";
}
- private string SerializeArray (JsonArray array)
+ private string SerializeEnumerable (IEnumerable 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 (",");
+ int i = 0;
+ foreach (var obj in array) {
+ builder.Append (Serialize (obj));
+ builder.Append (",");
+ i++;
}
+ // Get rid of trailing comma
+ if (i > 0)
+ builder.Remove (builder.Length - 1, 1);
builder.Append ("]");
return builder.ToString ();
}
- private string SerializeObject (JsonObject obj)
+ private string SerializeDictionary (Dictionary<string, object> obj)
{
StringBuilder builder = new StringBuilder ("{");
foreach (var pair in obj) {
@@ -115,11 +124,11 @@ namespace Hyena.Json
{
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);
@@ -132,13 +141,13 @@ namespace Hyena.Json
if (s != null)
return SerializeString (s);
- JsonObject o = unknownObj as JsonObject;
+ var o = unknownObj as Dictionary<string, object>;
if (o != null)
- return SerializeObject (o);
+ return SerializeDictionary (o);
- JsonArray a = unknownObj as JsonArray;
+ var a = unknownObj as IEnumerable;
if (a != null)
- return SerializeArray (a);
+ return SerializeEnumerable (a);
throw new ArgumentException ("Cannot serialize anything but doubles, integers, strings, JsonObjects, and JsonArrays");
}
diff --git a/src/Hyena/Hyena.Metrics/HttpPoster.cs b/src/Hyena/Hyena.Metrics/HttpPoster.cs
index 1a81480..774b2ba 100644
--- a/src/Hyena/Hyena.Metrics/HttpPoster.cs
+++ b/src/Hyena/Hyena.Metrics/HttpPoster.cs
@@ -53,6 +53,7 @@ namespace Hyena.Metrics
try {
using (var stream = request.GetRequestStream ()) {
using (var writer = new StreamWriter (stream)) {
+ // TODO gzip the data
writer.Write (metrics.ToJsonString ());
}
}
diff --git a/src/Hyena/Hyena.Metrics/MetricsCollection.cs b/src/Hyena/Hyena.Metrics/MetricsCollection.cs
index 1e35768..5b649cd 100644
--- a/src/Hyena/Hyena.Metrics/MetricsCollection.cs
+++ b/src/Hyena/Hyena.Metrics/MetricsCollection.cs
@@ -91,7 +91,7 @@ namespace Hyena.Metrics
}
report["Metrics"] = metrics;
- return report.ToJsonString ();
+ return new Serializer (report).Serialize ();
}
public void AddDefaults ()
diff --git a/src/Hyena/Hyena.Metrics/Tests/MetricsTests.cs b/src/Hyena/Hyena.Metrics/Tests/MetricsTests.cs
index df7fe6f..c620a5c 100644
--- a/src/Hyena/Hyena.Metrics/Tests/MetricsTests.cs
+++ b/src/Hyena/Hyena.Metrics/Tests/MetricsTests.cs
@@ -34,6 +34,7 @@ using System.IO;
using NUnit.Framework;
using Hyena;
+using Hyena.Json;
using Hyena.Metrics;
namespace Hyena.Tests
@@ -44,14 +45,15 @@ namespace Hyena.Tests
[Test]
public void MetricsCollection ()
{
- var metrics = new MetricsCollection ("myuniqueid", new MemorySampleStore ());
+ string id = "myuniqueid";
+ var metrics = new MetricsCollection (id, new MemorySampleStore ());
Assert.AreEqual ("myuniqueid", metrics.AnonymousUserId);
metrics.AddDefaults ();
Assert.IsTrue (metrics.Count > 0);
string metrics_str = metrics.ToJsonString ();
- Assert.IsTrue (metrics_str.Contains ("\"ID\" : myuniqueid"));
+ Assert.IsTrue (metrics_str.Contains ("\"ID\":\"myuniqueid\""));
// tests/Makefile.am runs the tests with Locale=it_IT
Assert.IsTrue (metrics_str.Contains ("it-IT"));
@@ -60,6 +62,13 @@ namespace Hyena.Tests
var now = DateTime.Now;
var time_metric = metrics.Add ("Foo", now);
Assert.AreEqual (Hyena.DateTimeUtil.ToInvariantString (now), metrics.Store.GetFor (time_metric).First ().Value);
+
+ // Make sure we can read the JSON back in
+ var ds = new Json.Deserializer ();
+ ds.SetInput (metrics.ToJsonString ());
+ var json_obj = ds.Deserialize () as JsonObject;
+ Assert.AreEqual (id, json_obj["ID"]);
+ Assert.IsTrue (json_obj["Metrics"] is JsonObject);
}
}
}
diff --git a/src/Hyena/Hyena.csproj b/src/Hyena/Hyena.csproj
index 980eed8..6fc43a7 100644
--- a/src/Hyena/Hyena.csproj
+++ b/src/Hyena/Hyena.csproj
@@ -160,7 +160,7 @@
<Compile Include="Hyena.Metrics\Tests\MetricsTests.cs" />
<Compile Include="Hyena.Metrics\MemorySampleStore.cs" />
<Compile Include="Hyena.Json\Tests\SerializerTests.cs" />
- <Compile Include="Hyena.Json\ExtensionMethods.cs" />
+ <Compile Include="Hyena.Json\Serializer.cs" />
<Compile Include="Hyena\Tests\DateTimeUtilTests.cs" />
</ItemGroup>
<ItemGroup>
diff --git a/src/Hyena/Makefile.am b/src/Hyena/Makefile.am
index 0247803..05a3812 100644
--- a/src/Hyena/Makefile.am
+++ b/src/Hyena/Makefile.am
@@ -106,7 +106,6 @@ SOURCES = \
Hyena.Json/Tests/SerializerTests.cs \
Hyena.Json/IJsonCollection.cs \
Hyena.Json/Tests/DeserializerTests.cs \
- Hyena.Json/ExtensionMethods.cs \
Hyena/Delegates.cs \
Hyena.Collections/LruCache.cs \
Hyena.Query/ExactStringQueryValue.cs \
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]