Ejemplo n.º 1
0
        public void TypeNameDictionary()
        {
            TypeNameDictionary <object> l = new TypeNameDictionary <object>();

            l.Add("First", new TestComponentSimple {
                MyProperty = 1
            });
            l.Add("Second", "String!");
            l.Add("Third", long.MaxValue);

            string json = JsonConvert.SerializeObject(l, Formatting.Indented);

            Assert.AreEqual(@"{
  ""First"": {
    ""$type"": ""Newtonsoft.Json.Tests.Serialization.TestComponentSimple, Newtonsoft.Json.Tests"",
    ""MyProperty"": 1
  },
  ""Second"": ""String!"",
  ""Third"": 9223372036854775807
}", json);

            TypeNameDictionary <object> l2 = JsonConvert.DeserializeObject <TypeNameDictionary <object> >(json);

            Assert.AreEqual(3, l2.Count);

            CustomAssert.IsInstanceOfType(typeof(TestComponentSimple), l2["First"]);
            Assert.AreEqual(1, ((TestComponentSimple)l2["First"]).MyProperty);
            CustomAssert.IsInstanceOfType(typeof(string), l2["Second"]);
            CustomAssert.IsInstanceOfType(typeof(long), l2["Third"]);
        }