public void testFromJsonHierarchy() { TestTreeNode rootNode = new TestTreeNode(); rootNode.Name = "top"; rootNode.branches = new List<TestTreeNode>(); TestTreeNode child = new TestTreeNode(); child.Name = "child"; child.branches = null; rootNode.branches.Add(child); NodeExpander nodeExpander = new FieldReflectionNodeExpander(); Object2Json o2J = new Object2Json(); o2J.NodeExpander = nodeExpander; o2J.IndentSize = 2; o2J.TypeAliaser = (t) => { return t.FullName; }; Json2Object j2O = new Json2Object(); o2J.TypeAliasProperty = j2O.TypeSpecifier; String json = o2J.toJson(rootNode); System.Console.Out.WriteLine("testToJsonLeafTypesViaFields json:" + json); TestTreeNode result = (TestTreeNode) j2O.toObject(json, typeof(TestTreeNode)); Func<TestTreeNode, String> extractValue = (tn) => {return tn.Branches[0].Name;}; Assert.AreEqual(extractValue(rootNode), extractValue(result), "check nested values" ); }
/*creates a tree structure of specified depth and width*/ public static TestTreeNode createTestHierarchy(int requiredDepth, int branchesPerParent) { List<TestTreeNode> nodes2Expand; nodes2Expand = new List<TestTreeNode>(); TestTreeNode rootNode = new TestTreeNode(); nodes2Expand.Add(rootNode); for (int level = 0; level < requiredDepth; level++) { List<TestTreeNode> nextNodesToExpand = new List<TestTreeNode>(); for (int done = 0; done < nodes2Expand.Count; done++) { TestTreeNode node = nodes2Expand[done]; for (int bdone = 0; bdone < branchesPerParent; bdone++) { TestTreeNode subNode = new TestTreeNode(); subNode.Name = "level" + level + "_index" + bdone; node.Branches.Add(subNode); nextNodesToExpand.Add(subNode); } } nodes2Expand = nextNodesToExpand; } return rootNode; }
public void testUseReferences() { TestTreeNode root = new TestTreeNode(); root.Name = "root"; TestTreeNode child = TestTreeNode.createTestHierarchy(3, 3); Object2Json o2j = new Object2Json(); o2j.NodeExpander = new PropertyReflectionNodeExpander(); root.Branches.Add(child); root.Branches.Add(child); root.Branches.Add(child); o2j.IndentSize = 2; o2j.UseReferences = false; string noRefJson = o2j.toJson(root); o2j.UseReferences = true; string withRefJson = o2j.toJson(root); double minimumCompression = 2; double compression=((double)noRefJson.Length)/((double)withRefJson.Length); System.Console.WriteLine("withRefJson=" + withRefJson); Assert.IsTrue((compression>minimumCompression), "noRefJson length= " + noRefJson.Length + " withRefJson length=" + withRefJson.Length + " compression ratio = " + compression + " minumum ratio=" + minimumCompression); Object expectedValue = root.Branches[2].Branches[1].Name; string expression = "Branches[2].Branches[1].Name"; //Object value; JSExcuteUtil.JsonValueSet valueSet = new JSExcuteUtil.JsonValueSet(); valueSet.json = o2j.ObjectResolverFunctionName + "(" + withRefJson + ")"; valueSet.varname = "withRefRoot"; valueSet.expressions2ExpectedValue[expression]=expectedValue; valueSet.extraFunctions.Add(o2j.getObjectResolverJS()); valueSet.extraFunctions.Add(o2j.getAttachId2ArrayJSFunction()); valueSet.extraFunctions.Add(o2j.getMarkAsArrayJSFunction()); Dictionary<string, object> values = util.extractValuesFromJson(valueSet, GetType().Name + "." + "testUseReferences"); Assert.AreEqual(expectedValue, values[expression], "expression=" + expression + " with reference json= " + withRefJson); }