Exemple #1
0
        public void testToJsonLeafTypesViaProperties()
        {
            AllPrimitiveLeafTypes template = new AllPrimitiveLeafTypes();

            String[] expressions = AllPrimitiveLeafTypes.testPropertyExpressions;
            //{ "ALong", "AShort", "ALongRef", "AString", "AChar", "AString2" };
            Object[] expectedValues = AllPrimitiveLeafTypes.testExpectedPropertyValues(template);
            //{ template.ALong, template.AShort, template.ALongRef, template.AString, template.AChar, template.AString2, template.AUint32, template.AUint64 };

            AllPrimitiveLeafTypes result;
            Json2Object           j2O = new Json2Object();
            //j2O.setToUseProperties();
            Type        type = typeof(AllPrimitiveLeafTypes);
            Object2Json o2J  = new Object2Json();

            o2J.NodeExpander = new PropertyReflectionNodeExpander();
            //todo json 2 object should understand TypeAliaser
            o2J.TypeAliaser       = (t) => { return(t.FullName); };
            o2J.TypeAliasProperty = j2O.TypeSpecifier;
            string json = o2J.toJson(template);

            System.Console.Out.WriteLine("testToJsonLeafTypesViaFields json:" + json);
            result = (AllPrimitiveLeafTypes)j2O.toObject(json);

            for (int done = 0; done < expressions.Length; done++)
            {
                string expression = expressions[done];
                object value      = type.GetProperty(expression).GetValue(result);
                Assert.AreEqual(expectedValues[done], value, expression + " value");
            }
        }
        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");
        }