/// <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)); } }
/// <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); }