Ejemplo n.º 1
0
 protected virtual void EvaluateColumns(DataTable table, ArrayExpression columnsExpression, IDeserializerHandler deserializer)
 {
     foreach (Expression colExpr in columnsExpression.Items)
     {
         table.Columns.Add(EvaluateColumn(colExpr, deserializer));
     }
 }
Ejemplo n.º 2
0
        public void Visit(ArrayExpression list)
        {
            VisitComplex(list);

            foreach (Expression item in list.Items)
                Visit(item);
        }
Ejemplo n.º 3
0
 public virtual void Visit(ArrayExpression expression)
 {
     OnArrayStart(expression);
     Visit((ComplexExpressionBase)expression);
     foreach (Expression item in expression.Items)
         item.Accept(this);
     OnArrayEnd(expression);
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 5
0
 protected virtual void EvaluateRows(DataTable table, ArrayExpression rowsExpression, IDeserializerHandler deserializer)
 {
     for (int i = 0; i < rowsExpression.Items.Count; i++)
     {
         ArrayExpression rowExpr = (ArrayExpression) rowsExpression.Items[i];
         for (int j = 0; j < rowExpr.Items.Count; j++)
         {
             rowExpr.Items[j] = new CastExpression(table.Columns[j].DataType, rowExpr.Items[j]);
         }
         rowExpr.ResultType = typeof(object[]);
         DataRow dr = table.NewRow();
         dr.ItemArray = (object[]) deserializer.Evaluate(rowExpr);
         table.Rows.Add(dr);
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Called on an array expression before the items are visited
 /// </summary>
 /// <param name="expression"></param>
 public virtual void OnArrayStart(ArrayExpression expression)
 {
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Called on an array expression after the items are visited
 /// </summary>
 /// <param name="expression"></param>
 public virtual void OnArrayEnd(ArrayExpression expression)
 {
 }
Ejemplo n.º 8
0
 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;
 }
Ejemplo n.º 9
0
 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;
 }
Ejemplo n.º 10
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)
        {
            TypeData handler = Config.GetTypeHandler(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;
        }
Ejemplo n.º 11
0
 /// <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;
 }
Ejemplo n.º 12
0
        /// <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 = Config.GetTypeHandler(listType);
            CollectionHandler collHandler = typeHandler.CollectionHandler;
            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);
        }
Ejemplo n.º 13
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;
 }