public void testToJsonLeafTypesViaFields()
        {
            // todo - this fails with FieldReflectionNodeExpander
            NodeExpander          nodeExpander = new FieldReflectionNodeExpander();
            AllPrimitiveLeafTypes testData     = new AllPrimitiveLeafTypes();

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

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

            validateJSON(json, expressions, expectedValues, "testToJsonLeafTypesViaFields");
        }
Esempio n. 2
0
        public void testToJsonLeafTypesViaFields()
        {
            AllPrimitiveLeafTypes template = new AllPrimitiveLeafTypes();

            String [] expressions    = AllPrimitiveLeafTypes.testFieldExpressions;
            Object[]  expectedValues = AllPrimitiveLeafTypes.testExpectedFieldValues(template);

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

            o2J.NodeExpander = new FieldReflectionNodeExpander();
            //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.GetField(expression).GetValue(result);
                if (value != null)
                {
                    Type underlyingType = value.GetType();
                    if (Nullable.GetUnderlyingType(underlyingType) != null)
                    {
                        underlyingType = Nullable.GetUnderlyingType(underlyingType);
                    }
                    if (underlyingType == typeof(Char) || underlyingType == typeof(char))
                    {
                        value = value.ToString();
                    }
                }
                Assert.AreEqual(expectedValues[done], value, expression + " value");
            }
        }