public void ResolveCollectionItemTest()
        {
            ArrayExpression  root   = new ArrayExpression();
            ObjectExpression child1 = new ObjectExpression();
            ObjectExpression child2 = new ObjectExpression();

            root.Add(child1);
            root.Add(child2);

            Expression actual = ResolveReference(root, "$[1]");

            Assert.AreSame(child2, actual, "$[1] did not resolve correctly");
        }
        public void ResolveNestedCollectionTest()
        {
            ArrayExpression root   = new ArrayExpression();
            ArrayExpression child1 = new ArrayExpression();
            ArrayExpression child2 = new ArrayExpression();

            root.Add(child1);
            root.Add(child2);
            ObjectExpression childA = new ObjectExpression();

            child1.Add(childA);
            ObjectExpression childB = new ObjectExpression();

            child1.Add(childB);


            Expression actual = ResolveReference(root, "$[0][1]");

            Assert.AreSame(childB, actual, "$[0][1] did not resolve correctly");
        }
        protected virtual Expression GetColumnsExpression(DataTable table, JsonPath currentPath, IExpressionBuilder serializer)
        {
            ArrayExpression columns  = new ArrayExpression();
            int             colCount = 0;

            foreach (DataColumn dc in table.Columns)
            {
                columns.Add(GetColumnExpression(dc, currentPath.Append(colCount), serializer));
                colCount++;
            }
            return(columns);
        }
示例#4
0
        /// <summary>
        /// Parses a javascript array
        /// </summary>
        /// <returns></returns>
        private ArrayExpression ParseCollection()
        {
            Token tok = ReadToken();

            Debug.Assert(tok == LSquareToken);
            ArrayExpression value = new ArrayExpression();
            Expression      item;

            while (ReadAhead(CommaToken, RSquareToken, new ExpressionMethod(ParseExpression), out item))
            {
                value.Add(item);
            }
            return(value);
        }
示例#5
0
        /// <summary>
        /// Parses a javascript array
        /// </summary>
        /// <returns></returns>
        private ArrayExpression ParseCollection()
        {
            Token tok = ReadToken();

            Debug.Assert(tok == LSquareToken);
            ArrayExpression value = new ArrayExpression()
            {
                LineNumber = tok.linenumber, CharacterPosition = tok.position
            };
            Expression item;
            bool       first = true;

            while (ReadAhead(CommaToken, RSquareToken, new ExpressionMethod(ParseExpression), out item, ref first))
            {
                value.Add(item);
            }
            return(value);
        }
        protected virtual Expression GetRowsExpression(DataTable table, JsonPath currentPath, IExpressionBuilder serializer)
        {
            ArrayExpression rowsExpr = new ArrayExpression();

            for (int i = 0; i < table.Rows.Count; i++)
            {
                DataRow         row     = table.Rows[i];
                object[]        values  = row.ItemArray;
                JsonPath        rowPath = currentPath.Append(i);
                ArrayExpression rowExpr = new ArrayExpression();
                for (int j = 0; j < values.Length; j++)
                {
                    rowExpr.Add(serializer.Serialize(values[j], rowPath.Append(j)));
                }
                rowsExpr.Add(rowExpr);
            }
            return(rowsExpr);
        }
示例#7
0
        protected virtual Expression GetExpression(IEnumerable Items, Type ItemType, JsonPath CurrentPath, ISerializerHandler serializer)
        {
            int index = 0;

            ArrayExpression expression = new ArrayExpression();

            foreach (object value in Items)
            {
                Expression itemExpr = serializer.Serialize(value, CurrentPath.Append(index));
                if (value != null && !ReflectionUtils.AreEquivalentTypes(value.GetType(), ItemType))
                {
                    itemExpr = new CastExpression(value.GetType(), itemExpr);
                }
                expression.Add(itemExpr);
                index++;
            }
            return(expression);
        }
示例#8
0
        /// <summary>
        /// Serializes the data into a json array expression.
        /// </summary>
        /// <param name="data">the data to serialize</param>
        /// <param name="currentPath">the current path to the data</param>
        /// <param name="serializer">serializer instance to use to serialize list items</param>
        /// <returns>a json array expression representation</returns>
        public override Expression GetExpression(object data, JsonPath currentPath, IExpressionBuilder serializer)
        {
            ITypeData handler = Settings.Types[data.GetType()];

            CollectionHandler collectionHandler = handler.CollectionHandler;
            Type elemType = collectionHandler.GetItemType(handler.ForType);

            int index = 0;

            ArrayExpression expression = new ArrayExpression();

            foreach (object value in collectionHandler.GetEnumerable(data))
            {
                Expression itemExpr = serializer.Serialize(value, currentPath.Append(index));
                if (value != null && !ReflectionUtils.AreEquivalentTypes(value.GetType(), elemType))
                {
                    itemExpr = new CastExpression(value.GetType(), itemExpr);
                }
                expression.Add(itemExpr);
                index++;
            }
            return(expression);
        }