public void StringTypeHandlerTest()
        {
            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);
            var jsonBase = new JsonBase {
                JsonObject = JObject.Parse(myJson)
            };

            var stringTypeHandler = new PrimitiveTypeHandler <string>();

            var nameValue     = stringTypeHandler.GetPropertyValue(jsonBase, nameof(myObject.Name));
            var nameNullValue = stringTypeHandler.GetPropertyValue(jsonBase, nameof(myObject.NameNull));

            Assert.AreEqual("David", nameValue);
            Assert.AreEqual(null, nameNullValue);

            stringTypeHandler.SetPropertyValue(jsonBase, jsonBase.GetJsonPropertyNameFromPropertyName(nameof(myObject.Name)), "Seth");
            stringTypeHandler.SetPropertyValue(jsonBase, jsonBase.GetJsonPropertyNameFromPropertyName(nameof(myObject.NameNull)), "SethNotNull");

            nameValue     = stringTypeHandler.GetPropertyValue(jsonBase, jsonBase.GetJsonPropertyNameFromPropertyName(nameof(myObject.Name)));
            nameNullValue = stringTypeHandler.GetPropertyValue(jsonBase, jsonBase.GetJsonPropertyNameFromPropertyName(nameof(myObject.NameNull)));

            Assert.AreEqual("Seth", nameValue);
            Assert.AreEqual("SethNotNull", nameNullValue);
        }
Example #2
0
 public T GetPropertyValue(JsonBase jsonBase, string propertyName) =>
 FromToken(jsonBase.JsonObject[jsonBase.GetJsonPropertyNameFromPropertyName(propertyName)], jsonBase);