Exemplo n.º 1
0
 public override object Get(string path)
 {
     if (string.IsNullOrEmpty(path))
     {
         return((object)null);
     }
     if (path.IndexOf(".", StringComparison.Ordinal) > 0)
     {
         return(RecursiveObjectReader.GetProperty((object)this, path));
     }
     return(base.Get(path));
 }
        public void TestGetProperty()
        {
            var obj = JsonConverter.ToMap(
                "{ \"value1\": 123, \"value2\": { \"value21\": 111, \"value22\": 222 }, \"value3\": [ 444, { \"value311\": 555 } ] }"
                );

            var value = RecursiveObjectReader.GetProperty(obj, "");

            Assert.Null(value);

            value = (int)(long)RecursiveObjectReader.GetProperty(obj, "value1");
            Assert.Equal(123, value);

            value = RecursiveObjectReader.GetProperty(obj, "value2");
            Assert.NotNull(value);

            value = (int)(long)RecursiveObjectReader.GetProperty(obj, "value2.value21");
            Assert.Equal(111, value);

            value = RecursiveObjectReader.GetProperty(obj, "value2.value31");
            Assert.Null(value);

            value = RecursiveObjectReader.GetProperty(obj, "value2.value21.value211");
            Assert.Null(value);

            value = RecursiveObjectReader.GetProperty(obj, "valueA.valueB.valueC");
            Assert.Null(value);

            value = RecursiveObjectReader.GetProperty(obj, "value3");
            Assert.NotNull(value);

            value = (int)(long)RecursiveObjectReader.GetProperty(obj, "value3.0");
            Assert.Equal(444, value);

            value = RecursiveObjectReader.GetProperty(obj, "value3.0.value311");
            Assert.Null(value);

            value = RecursiveObjectReader.GetProperty(obj, "value3.1");
            Assert.NotNull(value);

            value = (int)(long)RecursiveObjectReader.GetProperty(obj, "value3.1.value311");
            Assert.Equal(555, value);

            value = RecursiveObjectReader.GetProperty(obj, "value3.2");
            Assert.Null(value);
        }