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"); }
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; }
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; }
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, 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; }
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"); }
/// <summary> /// Parses a javascript array /// </summary> /// <returns></returns> private ArrayExpression ParseCollection() { Token tok = ReadToken(); Debug.Assert(tok == LSquareToken); ArrayExpression value = new ArrayExpression(); Expression item; bool first = true; while (ReadAhead(CommaToken, RSquareToken, new ExpressionMethod(ParseExpression), out item, ref first)) { value.Add(item); } return value; }