Example #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 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");
        }
Example #3
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");
            }
        }
 public static Object[] testExpectedPropertyValues(AllPrimitiveLeafTypes template)
 {
     return new Object[] {template.ADecimal, template.ALong, template.AShort, template.ALongRef, template.ALongRefNullable, template.AString, template.AChar/*.ToString()*/, template.AString2, template.AUint32, template.AUint64 };
 }
 public static Object[] testExpectedFieldValues(AllPrimitiveLeafTypes template)
 {
     return new Object[] {template.aDecimal, template.aLong, template.aShort, template.aLongRef, template.aString, template.aChar.ToString(), template.aString2, template.aUint32, template.aUint64 };
 }
        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 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");
            }
        }
 public static Object[] testExpectedPropertyValues(AllPrimitiveLeafTypes template)
 {
     return(new Object[] { template.ADecimal, template.ALong, template.AShort, template.ALongRef, template.ALongRefNullable, template.AString, template.AChar /*.ToString()*/, template.AString2, template.AUint32, template.AUint64 });
 }
 public static Object[] testExpectedFieldValues(AllPrimitiveLeafTypes template)
 {
     return(new Object[] { template.aDecimal, template.aLong, template.aShort, template.aLongRef, template.aString, template.aChar.ToString(), template.aString2, template.aUint32, template.aUint64 });
 }
 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");
 }