예제 #1
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;
 }
        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;
        }
        /// <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, ISerializerHandler serializer)
        {
            TypeData handler = Context.GetTypeHandler(data.GetType());

            CollectionHandler collectionHandler = handler.GetCollectionHandler();
            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;
        }