Exemplo n.º 1
0
        public void ErrorHandlingEndOfContentDynamic()
        {
            IList <string> errors = new List <string>();

            string json = @"{
  ""Explicit"": true,
  ""Decimal"": 99.9,
  ""Int"": 1,
  ""ChildObject"": {
    ""Integer"": 123";

            TestDynamicObject newDynamicObject = JsonConvert.DeserializeObject <TestDynamicObject>(json, new JsonSerializerSettings
            {
                Error = (sender, e) =>
                {
                    errors.Add(e.ErrorContext.Error.Message);
                    e.ErrorContext.Handled = true;
                },
                SpecialPropertyHandling = SpecialPropertyHandling.Default
            });

            Assert.AreEqual(true, newDynamicObject.Explicit);

            dynamic d = newDynamicObject;

            Assert.AreEqual(99.9, d.Decimal);
            Assert.AreEqual(1, d.Int);
            Assert.AreEqual(123, d.ChildObject.Integer);

            Assert.AreEqual(2, errors.Count);
            Assert.AreEqual(@"Unexpected end when deserializing object. Path 'ChildObject.Integer', line 6, position 19.", errors[0]);
            Assert.AreEqual(@"Unexpected end when deserializing object. Path 'ChildObject.Integer', line 6, position 19.", errors[1]);
        }
Exemplo n.º 2
0
        public void SerializeDynamicObject()
        {
            TestDynamicObject dynamicObject = new TestDynamicObject();

            dynamicObject.Explicit = true;

            dynamic d = dynamicObject;

            d.Int         = 1;
            d.Decimal     = 99.9d;
            d.ChildObject = new DynamicChildObject();

            Dictionary <string, object> values = new Dictionary <string, object>();

            IContractResolver   c = DefaultContractResolver.Instance;
            JsonDynamicContract dynamicContract = (JsonDynamicContract)c.ResolveContract(dynamicObject.GetType());

            foreach (string memberName in dynamicObject.GetDynamicMemberNames())
            {
                object value;
                dynamicContract.TryGetMember(dynamicObject, memberName, out value);

                values.Add(memberName, value);
            }

            Assert.AreEqual(d.Int, values["Int"]);
            Assert.AreEqual(d.Decimal, values["Decimal"]);
            Assert.AreEqual(d.ChildObject, values["ChildObject"]);

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

            Assert.AreEqual(@"{
  ""Explicit"": true,
  ""Decimal"": 99.9,
  ""Int"": 1,
  ""ChildObject"": {
    ""Text"": null,
    ""Integer"": 0
  }
}", json);

            TestDynamicObject newDynamicObject = JsonConvert.DeserializeObject <TestDynamicObject>(json);

            Assert.AreEqual(true, newDynamicObject.Explicit);

            d = newDynamicObject;

            Assert.AreEqual(99.9, d.Decimal);
            Assert.AreEqual(1, d.Int);
            Assert.AreEqual(dynamicObject.ChildObject.Integer, d.ChildObject.Integer);
            Assert.AreEqual(dynamicObject.ChildObject.Text, d.ChildObject.Text);
        }
        public void DynamicCamelCasePropertyNames()
        {
            dynamic o = new TestDynamicObject();

            o.Text    = "Text!";
            o.Integer = int.MaxValue;

            string json = JsonConvert.SerializeObject(o, Formatting.Indented,
                                                      new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });

            Assert.AreEqual(@"{
  ""explicit"": false,
  ""text"": ""Text!"",
  ""integer"": 2147483647,
  ""int"": 0,
  ""childObject"": null
}", json);
        }
Exemplo n.º 4
0
        public void SerializeDynamicObjectWithNullValueHandlingIgnore()
        {
            dynamic o = new TestDynamicObject();

            o.Text               = "Text!";
            o.Int                = int.MaxValue;
            o.ChildObject        = null; // Tests an explicitly defined property of a dynamic object with a null value.
            o.DynamicChildObject = null; // vs. a completely dynamic defined property.

            string json = JsonConvert.SerializeObject(o, Formatting.Indented, new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore,
            });

            Console.WriteLine(json);

            Assert.AreEqual(@"{
  ""Explicit"": false,
  ""Text"": ""Text!"",
  ""Int"": 2147483647
}", json);
        }