Skip to content

Instantly share code, notes, and snippets.

@chrisnicola
Forked from grumpydev/JsonTest.cs
Created September 30, 2011 17:54
Show Gist options
  • Save chrisnicola/1254492 to your computer and use it in GitHub Desktop.
Save chrisnicola/1254492 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
namespace JSONTestingAgain
{
class Program
{
static void Main(string[] args)
{
var test = new TestClass { TestGuid = Guid.NewGuid(), TestString = "blah" };
test.TestDictionary.Add(Guid.NewGuid(), "testStringValue");
test.TestDictionary.Add(Guid.NewGuid(), Guid.NewGuid());
var nancyResult = Nancy(test);
var microsoftResult = Microsoft(test);
Console.WriteLine(nancyResult);
Console.WriteLine(microsoftResult);
Console.ReadLine();
}
private static TestClass Microsoft(TestClass test)
{
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var result = serializer.Serialize(test);
var newOne = serializer.Deserialize<TestClass>(result);
return newOne;
}
private static TestClass Nancy(TestClass test)
{
var serializer = new Nancy.Json.JavaScriptSerializer();
var result = serializer.Serialize(test);
var newOne = serializer.Deserialize<TestClass>(result);
return newOne;
}
}
public class TestClass
{
public Guid TestGuid { get; set; }
public String TestString { get; set; }
public Dictionary<Guid, object> TestDictionary { get; set; }
public TestClass()
{
this.TestDictionary = new Dictionary<Guid, object>();
}
public override string ToString()
{
return String.Format(
"{0} {1} {2}",
TestGuid,
TestString,
this.TestDictionary.Select(kv => kv.Key.ToString() + ":" + kv.Value.ToString()).Aggregate(
(kv1, kv2) => kv1 + "," + kv2));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment