public List <InstancePathElement> findSingleObjectPath(object root, object object2Find)
        {
            bool found = false;
            ObjectExplorerImpl          explorer    = new ObjectExplorerImpl();
            Stack <InstancePathElement> currentPath = new Stack <InstancePathElement>();

            MoveAway down = delegate(Object from, string propertyName, Object to, bool isIndexed, int?index)
            {
                if (found)
                {
                    return(false);
                }
                InstancePathElement element;
                InstancePathElement thepeek = currentPath.Count > 0 ? currentPath.Peek() : null;
                bool parentIsIndexed        = currentPath.Count > 0 && currentPath.Peek().IsIndexed;
                if (parentIsIndexed)
                {
                    IndexPathElement ielement = new IndexPathElement();
                    ielement.Index = (int)index;
                    element        = ielement;
                }
                else
                {
                    NamePathElement nElement = new NamePathElement();
                    nElement.Name = propertyName;
                    element       = nElement;
                }
                element.IsIndexed = isIndexed;
                currentPath.Push(element);
                found = object2Find == to;
                return(!found);
            };
            MoveBack up = (from, propertyName, to, isIndexed) =>
            {
                if (!found)
                {
                    currentPath.Pop();
                }
            };

            OnLeaf onLeaf = (from, propertyName, to, index) =>
            {
            };

            PropertyReflectionNodeExpander expander = new PropertyReflectionNodeExpander();

            expander.ExcludeReadOnlyProperties = true;
            explorer.NodeExpander = expander;

            explorer.explore(root, down, up, onLeaf);
            List <InstancePathElement> result = null;

            if (found)
            {
                result = new List <InstancePathElement>(currentPath.ToArray().Reverse());
                // remove the first element because it is to the root
                result.RemoveAt(0);
            }
            return(result);
        }
        public void testExploration()
        {
            NodeExpander nodeExpander = new PropertyReflectionNodeExpander();
            TestData testData = new TestData();
            ObjectExplorerImpl explorer = new ObjectExplorerImpl();
            explorer.NodeExpander = nodeExpander;
            Object valueFound = null;
            int depth = 0;
            int? depthFound=null;
            MoveBack up = delegate(Object from, String propertyName, Object to, bool isIndexed)
                 {
                     depth--;
                 };
            MoveAway down = delegate(Object from, String propertyName, Object to, bool isIndexed, int ?index)
            {
                depth++;
                return true;
            };
            OnLeaf leaf = delegate(Object from, String propertyName, Object to, int? index)
            {
                if (propertyName!=null && propertyName.Equals("Greeting") )
                {
                    valueFound = to;
                    depthFound = depth + 1;
                }

            };
            explorer.explore(testData, down, up, leaf);
            Assert.AreEqual(testData.TheSub.TheSubSub.Greeting, valueFound, "expected greeting ");
            Assert.AreEqual(4, depthFound, "expected depth");
        }
        public void testExploration()
        {
            NodeExpander       nodeExpander = new PropertyReflectionNodeExpander();
            TestData           testData     = new TestData();
            ObjectExplorerImpl explorer     = new ObjectExplorerImpl();

            explorer.NodeExpander = nodeExpander;
            Object   valueFound = null;
            int      depth      = 0;
            int?     depthFound = null;
            MoveBack up         = delegate(Object from, String propertyName, Object to, bool isIndexed)
            {
                depth--;
            };
            MoveAway down = delegate(Object from, String propertyName, Object to, bool isIndexed, int?index)
            {
                depth++;
                return(true);
            };
            OnLeaf leaf = delegate(Object from, String propertyName, Object to, int?index)
            {
                if (propertyName != null && propertyName.Equals("Greeting"))
                {
                    valueFound = to;
                    depthFound = depth + 1;
                }
            };

            explorer.explore(testData, down, up, leaf);
            Assert.AreEqual(testData.TheSub.TheSubSub.Greeting, valueFound, "expected greeting ");
            Assert.AreEqual(4, depthFound, "expected depth");
        }
        public void testToJsonProperties()
        {
            NodeExpander nodeExpander = new PropertyReflectionNodeExpander();
            TestData     testData     = new TestData();

            String[]    expressions    = { "TheSub.TheSubSub.Greeting", "TheSub.TheSubSub.AgeYears", "TheSub.TheSubSub.HeightMetres" };
            Object[]    expectedValues = { testData.TheSub.TheSubSub.Greeting, testData.TheSub.TheSubSub.AgeYears, testData.TheSub.TheSubSub.HeightMetres };
            Object2Json o2J            = new Object2Json();

            o2J.NodeExpander = nodeExpander;
            o2J.IndentSize   = 2;
            String json = o2J.toJson(testData);

            validateJSON(json, expressions, expectedValues, "testToJsonProperties");
        }
        public void testToJsonPropertiesIndexed()
        {
            NodeExpander nodeExpander = new PropertyReflectionNodeExpander();
            TestData     testData     = new TestData();

            String[]    expressions    = { "TheSub.TheSubSub.Seasons[1]", "TheSub.TheSubSub.GoodYears[1]" };
            Object[]    expectedValues = { testData.TheSub.TheSubSub.Seasons[1], testData.TheSub.TheSubSub.GoodYears[1] };
            Object2Json o2J            = new Object2Json();

            //o2J.OmitMarkAsArrayFunction = true;
            o2J.NodeExpander = nodeExpander;
            o2J.IndentSize   = 2;
            String json = o2J.toJson(testData);

            validateJSON(json, expressions, expectedValues, "testToJsonPropertiesIndexed");
        }
        public void testToJsonLeafTypesViaProperties()
        {
            // todo - this fails with FieldReflectionNodeExpander
            NodeExpander          nodeExpander = new PropertyReflectionNodeExpander();
            AllPrimitiveLeafTypes testData     = new AllPrimitiveLeafTypes();

            String[]    expressions    = AllPrimitiveLeafTypes.testPropertyExpressions;
            Object[]    expectedValues = AllPrimitiveLeafTypes.testExpectedPropertyValues(testData);
            Object2Json o2J            = new Object2Json();

            o2J.NodeExpander = nodeExpander;
            o2J.IndentSize   = 2;
            String json = o2J.toJson(testData);

            validateJSON(json, expressions, expectedValues, "testToJsonLeafTypesViaProperties");
        }
 public void testToJsonPropertiesIndexed()
 {
     NodeExpander nodeExpander = new PropertyReflectionNodeExpander();
     TestData testData = new TestData();
     String[] expressions = { "TheSub.TheSubSub.Seasons[1]", "TheSub.TheSubSub.GoodYears[1]" };
     Object[] expectedValues = {  testData.TheSub.TheSubSub.Seasons[1], testData.TheSub.TheSubSub.GoodYears[1] };
     Object2Json o2J = new Object2Json();
     //o2J.OmitMarkAsArrayFunction = true;
     o2J.NodeExpander = nodeExpander;
     o2J.IndentSize = 2;
     String json = o2J.toJson(testData);
     validateJSON(json, expressions, expectedValues, "testToJsonPropertiesIndexed");
 }
 public void testToJsonProperties()
 {
     NodeExpander nodeExpander = new PropertyReflectionNodeExpander();
     TestData testData = new TestData();
     String[] expressions = { "TheSub.TheSubSub.Greeting", "TheSub.TheSubSub.AgeYears", "TheSub.TheSubSub.HeightMetres" };
     Object[] expectedValues = { testData.TheSub.TheSubSub.Greeting, testData.TheSub.TheSubSub.AgeYears, testData.TheSub.TheSubSub.HeightMetres };
     Object2Json o2J = new Object2Json();
     o2J.NodeExpander = nodeExpander;
     o2J.IndentSize = 2;
     String json = o2J.toJson(testData);
     validateJSON(json, expressions, expectedValues, "testToJsonProperties");
 }
 public void testToJsonLeafTypesViaProperties()
 {
     // todo - this fails with FieldReflectionNodeExpander
     NodeExpander nodeExpander = new PropertyReflectionNodeExpander();
     AllPrimitiveLeafTypes testData = new AllPrimitiveLeafTypes();
     String[] expressions = AllPrimitiveLeafTypes.testPropertyExpressions;
     Object[] expectedValues = AllPrimitiveLeafTypes.testExpectedPropertyValues(testData);
     Object2Json o2J = new Object2Json();
     o2J.NodeExpander = nodeExpander;
     o2J.IndentSize = 2;
     String json = o2J.toJson(testData);
     validateJSON(json, expressions, expectedValues, "testToJsonLeafTypesViaProperties");
 }