public void TestGenericStuff()
        {
            var noCamelSerializerSettings = new JsonSerializerSettings();

            var myObject = new
            {
                Age       = (int?)27,
                AgeNull   = (int?)null,
                Name      = "David",
                NameNull  = (string)null,
                MyProfile = new { Address = "1234 Any Street" },
                MyValues  = new[] { new { Storms = "Norm" }, new { Storms = "Strange" } }
            };
            var myJson = JsonConvert.SerializeObject(myObject, noCamelSerializerSettings);

            var jsonBase = new JsonBase {
                JsonObject = JObject.Parse(myJson)
            };

            var resultAge      = new PrimitiveTypeHandler <int?>().GetPropertyValue(jsonBase, "Age");
            var resultNull     = new PrimitiveTypeHandler <int?>().GetPropertyValue(jsonBase, "AgeNull");
            var nameResult     = new PrimitiveTypeHandler <string>().GetPropertyValue(jsonBase, "Name");
            var nameNullResult = new PrimitiveTypeHandler <string>().GetPropertyValue(jsonBase, "NameNull");

            Assert.AreEqual(27, resultAge);
            Assert.AreEqual(null, resultNull);
            Assert.AreEqual("David", nameResult);
            Assert.AreEqual(null, nameNullResult);

            var valueToken = jsonBase.ForceGetValuePropertyToken("Hokey");

            Assert.IsNotNull(valueToken);
            Assert.IsNull(valueToken.Value <string>());
        }
Beispiel #2
0
 public void SetPropertyValue(JsonBase jsonBase, string propertyName, T value) =>
 jsonBase.ForceGetValuePropertyToken(propertyName).Value = value;