public void PopulatePerson()
        {
            Person p = new Person();

            JsonConvertX.PopulateObject(@"{""Name"":""James""}", p);

            Assert.AreEqual("James", p.Name);
        }
Exemplo n.º 2
0
        public void PopulateWithExtensionData()
        {
            string jsonStirng = @"{ ""ForJson"" : 33 , ""extra1"" : 11, ""extra2"" : 22 }";

            MyClass c = new MyClass();

            JsonConvertX.PopulateObject(jsonStirng, c);

            Assert.AreEqual(2, c.ExtraInfoJson.Count);
            Assert.AreEqual(11, (int)c.ExtraInfoJson["extra1"]);
            Assert.AreEqual(22, (int)c.ExtraInfoJson["extra2"]);
        }
        public void PopulateArray()
        {
            IList <Person> people = new List <Person>
            {
                new Person {
                    Name = "Initial"
                }
            };

            JsonConvertX.PopulateObject(@"[{""Name"":""James""}, null]", people);

            Assert.AreEqual(3, people.Count);
            Assert.AreEqual("Initial", people[0].Name);
            Assert.AreEqual("James", people[1].Name);
            Assert.AreEqual(null, people[2]);
        }
Exemplo n.º 4
0
        public void DeserializeCircularReferencesWithConverter()
        {
            string json = @"{
  ""$id"": ""1"",
  ""$type"": ""CircularReferenceClass""
}";

            MetadataPropertyDisabledTestClass c = new MetadataPropertyDisabledTestClass();

            JsonConvertX.PopulateObject(json, c, new JsonSerializerSettings
            {
                MetadataPropertyHandling = MetadataPropertyHandling.Ignore
            });

            Assert.AreEqual("1", c.Id);
            Assert.AreEqual("CircularReferenceClass", c.Type);
        }
Exemplo n.º 5
0
        public void MissingMemberHandling_InnerObject()
        {
            IList <string> errors = new List <string>();

            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                //This works on properties but not on a objects property.
                /* So nameERROR:{"first":"ni"} would throw. The payload name:{"firstERROR":"hi"} would not */
                MissingMemberHandling = MissingMemberHandling.Error,
                Error = (sender, args) =>
                {
                    // A more concrete error type would be nice but we are limited by Newtonsofts library here.
                    errors.Add(args.ErrorContext.Error.Message);
                    args.ErrorContext.Handled = true;
                }
            };

            Person p = new Person();

            JsonConvertX.PopulateObject(@"{name:{""firstERROR"":""hi""}}", p, settings);

            Assert.AreEqual(1, errors.Count);
            Assert.AreEqual("Could not find member 'firstERROR' on object of type 'Name'. Path 'name.firstERROR', line 1, position 20.", errors[0]);
        }
        public void PopulateStore()
        {
            Store s = new Store();

            s.Color   = StoreColor.Red;
            s.product = new List <Product>
            {
                new Product
                {
                    ExpiryDate = new DateTime(2000, 12, 3, 0, 0, 0, DateTimeKind.Utc),
                    Name       = "ProductName!",
                    Price      = 9.9m
                }
            };
            s.Width  = 99.99d;
            s.Mottos = new List <string> {
                "Can do!", "We deliver!"
            };

            string json = @"{
  ""Color"": 2,
  ""Establised"": ""\/Date(1264122061000+0000)\/"",
  ""Width"": 99.99,
  ""Employees"": 999,
  ""RoomsPerFloor"": [
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9
  ],
  ""Open"": false,
  ""Symbol"": ""@"",
  ""Mottos"": [
    ""Fail whale""
  ],
  ""Cost"": 100980.1,
  ""Escape"": ""\r\n\t\f\b?{\\r\\n\""'"",
  ""product"": [
    {
      ""Name"": ""ProductName!"",
      ""ExpiryDate"": ""\/Date(975801600000)\/"",
      ""Price"": 9.9,
      ""Sizes"": null
    }
  ]
}";

            JsonConvertX.PopulateObject(json, s, new JsonSerializerSettings
            {
                ObjectCreationHandling = ObjectCreationHandling.Replace
            });

            Assert.AreEqual(1, s.Mottos.Count);
            Assert.AreEqual("Fail whale", s.Mottos[0]);
            Assert.AreEqual(1, s.product.Count);

            //Assert.AreEqual("James", p.Name);
        }
 public void PopulateWithBadJson()
 {
     ExceptionAssert.Throws <JsonSerializationException>(() => { JsonConvertX.PopulateObject("1", new Person()); }, "Unexpected initial token 'Integer' when populating object. Expected JSON object or array. Path '', line 1, position 1.");
 }