Skip to content

Instantly share code, notes, and snippets.

@grumpydev
Created September 30, 2011 12:26

Revisions

  1. grumpydev created this gist Sep 30, 2011.
    62 changes: 62 additions & 0 deletions JsonTest.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    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("testString", "testStringValue");
    test.TestDictionary.Add("testGuid", 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<string, object> TestDictionary { get; set; }

    public TestClass()
    {
    this.TestDictionary = new Dictionary<string, 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));
    }
    }
    }