public void Visit(ArrayExpression list)
        {
            VisitComplex(list);

            foreach (Expression item in list.Items)
                Visit(item);
        }
 protected virtual void EvaluateItems(ArrayExpression Expression, object Collection, Type ItemType, IDeserializerHandler deserializer)
 {
     foreach (Expression item in Expression.Items)
     {
         item.ResultType = ItemType;
         object itemResult = deserializer.Evaluate(item);
         AddItem(Collection, itemResult);
     }
 }
        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;
        }
Пример #4
0
        /// <summary>
        /// Resolve a reference to an item within the collection
        /// </summary>
        /// <param name="refID">the reference to resolve</param>
        /// <returns>the referenced expression</returns>
        public void VisitListExpression(ArrayExpression expression)
        {
            Visit(typeof(ComplexExpressionBase), expression);
            if (_expr != null)
                return; // found it

            int index = _refID.TopAsInt;
            if (index < 0 || index >= expression.Items.Count)
            {
                throw new ArgumentOutOfRangeException("Reference to collection item out of range: " + _refID);
            }
            Expression expr = expression.Items[index];
            Visit(expr);
        }
 protected virtual object ConstructCollection(ArrayExpression Expression, IDeserializerHandler deserializer)
 {
     object result = Activator.CreateInstance(Expression.ResultType);
     return result;
 }
Пример #6
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;
 }
        /// <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;
        }
 /// <summary>
 /// Evaluates the items in the expression and assigns them to the collection using the builder
 /// </summary>
 /// <param name="expression">the expression to evaluate</param>
 /// <param name="builder">builder used to build the collection</param>
 /// <param name="itemType">the type of the collection's elements</param>
 /// <param name="deserializer">deserializer instance to deserialize items</param>
 /// <returns>evaluated collection object</returns>
 protected virtual object EvaluateItems(ArrayExpression expression, ICollectionBuilder builder, Type itemType, IDeserializerHandler deserializer)
 {
     object result = null;
     bool constructedEventSent = false;
     try
     {
         result = builder.GetReference();
         expression.OnObjectConstructed(result);
         constructedEventSent = true;
     }
     catch
     {
         // this might fail if the builder's not ready
     }
     foreach (Expression item in expression.Items)
     {
         item.ResultType = itemType;
         object itemResult = deserializer.Evaluate(item);
         builder.Add(itemResult);
     }
     result = builder.GetResult();
     if (!constructedEventSent)
         expression.OnObjectConstructed(result);
     return result;
 }
        /// <summary>
        /// Constructs a builder used to build the deserialized collection
        /// </summary>
        /// <param name="collection">an existing collection object or null for a new collection</param>
        /// <param name="list">the list expression</param>
        /// <param name="itemType">the type of the items</param>
        /// <returns>collection builder</returns>
        protected virtual ICollectionBuilder ConstructBuilder(object collection, ArrayExpression list, out Type itemType)
        {
            Type listType = collection != null ? collection.GetType() : list.ResultType;
            TypeData typeHandler = Context.GetTypeHandler(listType);
            CollectionHandler collHandler = typeHandler.GetCollectionHandler();
            itemType = collHandler.GetItemType(listType);
            if (itemType == null)
                throw new Exception("Null item type returned from " + collHandler.GetType() + " for Collection type: " + listType);

            if (collection != null)
                return collHandler.ConstructBuilder(collection);
            else
                return collHandler.ConstructBuilder(listType, list.Items.Count);
        }