Last active
October 20, 2015 11:46
-
-
Save ovatsus/1608951 to your computer and use it in GitHub Desktop.
Serialization of Dictionary<string, object>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using Newtonsoft.Json; | |
using NUnit.Framework; | |
using ServiceStack.Text; | |
using JsonSerializer = ServiceStack.Text.JsonSerializer; | |
namespace Tests | |
{ | |
[TestFixture] | |
public class TestJsonSerialization | |
{ | |
private static Dictionary<string, object> SetupDict() | |
{ | |
return new Dictionary<string, object> { | |
{ "a", "text" }, | |
{ "b", 32 }, | |
{ "c", false }, | |
}; | |
} | |
private static void AssertDict(Dictionary<string, object> dict) | |
{ | |
Assert.AreEqual("text", dict["a"]); | |
Assert.AreEqual(32, dict["b"]); | |
Assert.AreEqual(false, dict["c"]); | |
} | |
[Test] | |
public void Test_JsonNet() | |
{ | |
var dict = SetupDict(); | |
var json = JsonConvert.SerializeObject(dict); | |
var deserializedDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(json); | |
AssertDict(deserializedDict); | |
} | |
[Test] | |
public void Test_ServiceStack_Text_TypeSerializer() | |
{ | |
var dict = SetupDict(); | |
var json = TypeSerializer.SerializeToString(dict); | |
var deserializedDict = TypeSerializer.DeserializeFromString<Dictionary<string, object>>(json); | |
AssertDict(deserializedDict); | |
} | |
[Test] | |
public void Test_ServiceStack_Text_JsonSerializer() | |
{ | |
var dict = SetupDict(); | |
var json = JsonSerializer.SerializeToString(dict); | |
var deserializedDict = JsonSerializer.DeserializeFromString<Dictionary<string, object>>(json); | |
AssertDict(deserializedDict); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your test is incomplete:
(object)ints will be deserialized as longs, and floats as doubles.