Ejemplo n.º 1
0
        public void PrimitiveType_MetadataPropertyIgnore_WithNoType()
        {
            ItemWithUntypedPayload actual = JsonConvertX.DeserializeObject <ItemWithUntypedPayload>(@"{
  ""Payload"": {
    ""$type"": ""System.Single, mscorlib"",
    ""$value"": ""5""
  }
}",
                                                                                                    new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.Auto
            });

            Assert.AreEqual(5f, actual.Payload);

            actual = JsonConvertX.DeserializeObject <ItemWithUntypedPayload>(@"{
  ""Payload"": {
    ""$type"": ""System.Single, mscorlib"",
    ""$value"": ""5""
  }
}",
                                                                             new JsonSerializerSettings
            {
                TypeNameHandling         = TypeNameHandling.Auto,
                MetadataPropertyHandling = MetadataPropertyHandling.Ignore
            });

            Assert.IsTrue(actual.Payload is JObject);
        }
Ejemplo n.º 2
0
        public void ExtensionDataTest()
        {
            string json = @"{
  ""Ints"": [1,2,3],
  ""Ignored"": [1,2,3],
  ""Readonly"": ""Readonly"",
  ""Name"": ""Actually set!"",
  ""CustomName"": ""Wrong name!"",
  ""GetPrivate"": true,
  ""GetOnly"": true,
  ""NewValueSimple"": true,
  ""NewValueComplex"": [1,2,3]
}";

            ExtensionDataTestClass c = JsonConvertX.DeserializeObject <ExtensionDataTestClass>(json);

            Assert.AreEqual("Actually set!", c.Name);
            Assert.AreEqual(4, c.Ints.Count);

            Assert.AreEqual("Readonly", (string)c.ExtensionData["Readonly"]);
            Assert.AreEqual("Wrong name!", (string)c.ExtensionData["CustomName"]);
            Assert.AreEqual(true, (bool)c.ExtensionData["GetPrivate"]);
            Assert.AreEqual(true, (bool)c.ExtensionData["GetOnly"]);
            Assert.AreEqual(true, (bool)c.ExtensionData["NewValueSimple"]);
            Assert.IsTrue(JToken.DeepEquals(new JArray(1, 2, 3), c.ExtensionData["NewValueComplex"]));
            Assert.IsTrue(JToken.DeepEquals(new JArray(1, 2, 3), c.ExtensionData["Ignored"]));

            Assert.AreEqual(7, c.ExtensionData.Count);
        }
        public void ErrorDeserializingListHandled()
        {
            string json = @"[
  {
    ""Name"": ""Jim"",
    ""BirthDate"": ""\/Date(978048000000)\/"",
    ""LastModified"": ""\/Date(978048000000)\/""
  },
  {
    ""Name"": ""Jim"",
    ""BirthDate"": ""\/Date(978048000000)\/"",
    ""LastModified"": ""\/Date(978048000000)\/""
  }
]";

            var possibleMsgs = new[]
            {
                "[1] - Error message for member 1 = An item with the same key has already been added.",
                "[1] - Error message for member 1 = An element with the same key already exists in the dictionary.", // mono
                "[1] - Error message for member 1 = An item with the same key has already been added. Key: Jim"      // netcore
            };
            VersionKeyedCollection c = JsonConvertX.DeserializeObject <VersionKeyedCollection>(json);

            Assert.AreEqual(1, c.Count);
            Assert.AreEqual(1, c.Messages.Count);

            Console.WriteLine(c.Messages[0]);
            Assert.IsTrue(possibleMsgs.Any(m => m == c.Messages[0]), "Expected One of: " + Environment.NewLine + string.Join(Environment.NewLine, possibleMsgs) + Environment.NewLine + "Was: " + Environment.NewLine + c.Messages[0]);
        }
        public void ApproxEquals()
        {
            Assert.IsTrue(MathUtils.ApproxEquals(0.0, 0.0));
            Assert.IsTrue(MathUtils.ApproxEquals(1000.0, 1000.0000000000001));

            Assert.IsFalse(MathUtils.ApproxEquals(1000.0, 1000.000000000001));
            Assert.IsFalse(MathUtils.ApproxEquals(0.0, 0.00001));
        }
Ejemplo n.º 5
0
        public void DeserializeSortedSet()
        {
            string json = @"[
  ""One"",
  ""II"",
  ""3""
]";

            ImmutableSortedSet <string> l = JsonConvertX.DeserializeObject <ImmutableSortedSet <string> >(json);

            Assert.AreEqual(3, l.Count());
            Assert.IsTrue(l.Contains("3"));
            Assert.IsTrue(l.Contains("II"));
            Assert.IsTrue(l.Contains("One"));
        }
Ejemplo n.º 6
0
        public void ShouldDeserialize_True()
        {
            string json = @"{'HasName':true,'Name':'Name!'}";

            MemoryTraceWriter          traceWriter = new MemoryTraceWriter();
            ShouldDeserializeTestClass c           = JsonConvertX.DeserializeObject <ShouldDeserializeTestClass>(json, new JsonSerializerSettings
            {
                ContractResolver = ShouldDeserializeContractResolver.Instance,
                TraceWriter      = traceWriter
            });

            Assert.AreEqual(null, c.ExtensionData);
            Assert.AreEqual(true, c.HasName);
            Assert.AreEqual("Name!", c.Name);

            Assert.IsTrue(traceWriter.GetTraceMessages().Any(m => m.EndsWith("Verbose ShouldDeserialize result for property 'Name' on JsonExtensions.Tests.Serialization.ShouldDeserializeTestClass: True. Path 'Name'.")));
        }
Ejemplo n.º 7
0
        public void SpecifiedTest()
        {
            SpecifiedTestClass c = new SpecifiedTestClass();

            c.Name          = "James";
            c.Age           = 27;
            c.NameSpecified = false;

            string json = JsonConvertX.SerializeObject(c, Formatting.Indented);

            StringAssert.AreEqual(@"{
  ""Age"": 27
}", json);

            SpecifiedTestClass deserialized = JsonConvertX.DeserializeObject <SpecifiedTestClass>(json);

            Assert.IsNull(deserialized.Name);
            Assert.IsFalse(deserialized.NameSpecified);
            Assert.IsFalse(deserialized.WeightSpecified);
            Assert.IsFalse(deserialized.HeightSpecified);
            Assert.IsFalse(deserialized.FavoriteNumberSpecified);
            Assert.AreEqual(27, deserialized.Age);

            c.NameSpecified   = true;
            c.WeightSpecified = true;
            c.HeightSpecified = true;
            c.FavoriteNumber  = 23;
            json = JsonConvertX.SerializeObject(c, Formatting.Indented);

            StringAssert.AreEqual(@"{
  ""Name"": ""James"",
  ""Age"": 27,
  ""Weight"": 0,
  ""Height"": 0,
  ""FavoriteNumber"": 23
}", json);

            deserialized = JsonConvertX.DeserializeObject <SpecifiedTestClass>(json);
            Assert.AreEqual("James", deserialized.Name);
            Assert.IsTrue(deserialized.NameSpecified);
            Assert.IsTrue(deserialized.WeightSpecified);
            Assert.IsTrue(deserialized.HeightSpecified);
            Assert.IsTrue(deserialized.FavoriteNumberSpecified);
            Assert.AreEqual(27, deserialized.Age);
            Assert.AreEqual(23, deserialized.FavoriteNumber);
        }
        public void AddPropertyIncludesPrivateImplementations()
        {
            var value = new PrivateImplementationBClass
            {
                OverriddenProperty = "OverriddenProperty",
                PropertyA = "PropertyA",
                PropertyB = "PropertyB"
            };

            var resolver = new DefaultContractResolver();
            var contract = (JsonObjectContract)resolver.ResolveContract(value.GetType());

            Assert.AreEqual(3, contract.Properties.Count);
            Assert.IsTrue(contract.Properties.Contains("OverriddenProperty"), "Contract is missing property 'OverriddenProperty'");
            Assert.IsTrue(contract.Properties.Contains("PropertyA"), "Contract is missing property 'PropertyA'");
            Assert.IsTrue(contract.Properties.Contains("PropertyB"), "Contract is missing property 'PropertyB'");
        }
Ejemplo n.º 9
0
        public void RoundTripJObjectExtensionData()
        {
            JObjectExtensionDataTestClass c = new JObjectExtensionDataTestClass();

            c.Name          = "Name!";
            c.ExtensionData = new JObject
            {
                { "one", 1 },
                { "two", "II" },
                { "three", new JArray(1, 1, 1) }
            };

            string json = JsonConvertX.SerializeObject(c, Formatting.Indented);

            JObjectExtensionDataTestClass c2 = JsonConvertX.DeserializeObject <JObjectExtensionDataTestClass>(json);

            Assert.AreEqual("Name!", c2.Name);
            Assert.IsTrue(JToken.DeepEquals(c.ExtensionData, c2.ExtensionData));
        }
Ejemplo n.º 10
0
        public void DeserializeListsWithPreserveObjectReferences()
        {
            string json = @"{
  ""$id"": ""1"",
  ""$values"": [
    null,
    {
      ""$id"": ""2"",
      ""$values"": [
        null
      ]
    },
    {
      ""$id"": ""3"",
      ""$values"": [
        {
          ""$id"": ""4"",
          ""$values"": [
            {
              ""$ref"": ""1""
            }
          ]
        }
      ]
    }
  ]
}";

            PreserveReferencesHandlingTests.CircularList circularList = JsonConvertX.DeserializeObject <PreserveReferencesHandlingTests.CircularList>(json,
                                                                                                                                                      new JsonSerializerSettings
            {
                PreserveReferencesHandling = PreserveReferencesHandling.All,
                MetadataPropertyHandling   = MetadataPropertyHandling.ReadAhead
            });

            Assert.AreEqual(3, circularList.Count);
            Assert.AreEqual(null, circularList[0]);
            Assert.AreEqual(1, circularList[1].Count);
            Assert.AreEqual(1, circularList[2].Count);
            Assert.AreEqual(1, circularList[2][0].Count);
            Assert.IsTrue(ReferenceEquals(circularList, circularList[2][0][0]));
        }
Ejemplo n.º 11
0
        public void ExtensionDataTest_DeserializeWithNamingStrategy()
        {
            string json = @"{
  ""Ints"": [1,2,3],
  ""Ignored"": [1,2,3],
  ""Readonly"": ""Readonly"",
  ""Name"": ""Actually set!"",
  ""CustomName"": ""Wrong name!"",
  ""GetPrivate"": true,
  ""GetOnly"": true,
  ""NewValueSimple"": true,
  ""NewValueComplex"": [1,2,3]
}";

            ExtensionDataTestClass c = JsonConvertX.DeserializeObject <ExtensionDataTestClass>(json, new JsonSerializerSettings
            {
                ContractResolver = new DefaultContractResolver
                {
                    NamingStrategy = new CamelCaseNamingStrategy
                    {
                        ProcessExtensionDataNames = true
                    }
                }
            });

            Assert.AreEqual("Actually set!", c.Name);
            Assert.AreEqual(4, c.Ints.Count);

            Assert.AreEqual("Readonly", (string)c.ExtensionData["Readonly"]);
            Assert.AreEqual("Wrong name!", (string)c.ExtensionData["CustomName"]);
            Assert.AreEqual(true, (bool)c.ExtensionData["GetPrivate"]);
            Assert.AreEqual(true, (bool)c.ExtensionData["GetOnly"]);
            Assert.AreEqual(true, (bool)c.ExtensionData["NewValueSimple"]);
            Assert.IsTrue(JToken.DeepEquals(new JArray(1, 2, 3), c.ExtensionData["NewValueComplex"]));
            Assert.IsTrue(JToken.DeepEquals(new JArray(1, 2, 3), c.ExtensionData["Ignored"]));

            Assert.AreEqual(7, c.ExtensionData.Count);
        }
        public void DeserializeDictionarysWithPreserveObjectReferences()
        {
            string json = @"{
  ""$id"": ""1"",
  ""other"": {
    ""$id"": ""2"",
    ""blah"": null
  },
  ""self"": {
    ""$ref"": ""1""
  }
}";

            CircularDictionary circularDictionary = JsonConvertX.DeserializeObject <CircularDictionary>(json,
                                                                                                        new JsonSerializerSettings
            {
                PreserveReferencesHandling = PreserveReferencesHandling.All
            });

            Assert.AreEqual(2, circularDictionary.Count);
            Assert.AreEqual(1, circularDictionary["other"].Count);
            Assert.IsTrue(ReferenceEquals(circularDictionary, circularDictionary["self"]));
        }
Ejemplo n.º 13
0
        public void DeserializePublicExtensionData()
        {
            string json = @"{
  'Name':'Name!',
  'NoMatch':'NoMatch!',
  'ExtensionData':{'HAI':true}
}";

            var c = JsonConvertX.DeserializeObject <PublicExtensionDataAttributeTestClass>(json);

            Assert.AreEqual("Name!", c.Name);
            Assert.AreEqual(2, c.ExtensionData.Count);

            Assert.AreEqual("NoMatch!", (string)c.ExtensionData["NoMatch"]);

            // the ExtensionData property is put into the extension data
            // inception
            var o = (JObject)c.ExtensionData["ExtensionData"];

            Assert.AreEqual(1, o.Count);
            Assert.IsTrue(JToken.DeepEquals(new JObject {
                { "HAI", true }
            }, o));
        }