public override Expression GetExpression(object data, JsonPath currentPath, IExpressionBuilder serializer) { DataTable table = (DataTable) data; ObjectExpression tableExpr = new ObjectExpression(); tableExpr.Add("TableName", serializer.Serialize(table.TableName, currentPath.Append("TableName"))); tableExpr.Add("Columns", GetColumnsExpression(table, currentPath.Append("Columns"), serializer)); tableExpr.Add("Rows", GetRowsExpression(table, currentPath, serializer)); return tableExpr; }
/// <summary> /// Serialize an object implementing IDictionary. The serialized data is similar to a regular /// object, except that the keys of the dictionary are used instead of properties. /// </summary> /// <param name="data">the dictionary object</param> /// <param name="currentPath">object's path</param> /// <param name="serializer">the serializer instance, used to serialize keys and values</param> public override Expression GetExpression(object data, JsonPath currentPath, IExpressionBuilder serializer) { IDictionary dictionary = (IDictionary)data; Type itemType = typeof(object); Type genericDictionary = null; if ((genericDictionary = dictionary.GetType().GetInterface(typeof(IDictionary<,>).Name)) != null) { itemType = genericDictionary.GetGenericArguments()[1]; } ObjectExpression expression = new ObjectExpression(); foreach (DictionaryEntry pair in dictionary) { //may not work in all cases object value = pair.Value; Expression valueExpr = serializer.Serialize(value, currentPath.Append(pair.Key.ToString())); if (value != null && !ReflectionUtils.AreEquivalentTypes(value.GetType(), itemType)) { valueExpr = new CastExpression(value.GetType(), valueExpr); } expression.Add(pair.Key.ToString(), valueExpr); } return expression; }
protected virtual Expression GetColumnExpression(DataColumn dc, JsonPath jsonPath, IExpressionBuilder serializer) { ObjectExpression column = new ObjectExpression(); // just DataType and column for now column.Add("DataType", serializer.Serialize(dc.DataType, jsonPath.Append("DataType"))); column.Add("ColumnName", serializer.Serialize(dc.ColumnName, jsonPath.Append("ColumnName"))); return column; }
/// <summary> /// Generates an expression for an item and adds it to the object /// </summary> /// <param name="data">the item being serialized</param> /// <param name="currentPath">the current path to the object</param> /// <param name="serializer">serializer instance</param> /// <param name="expression">the object expression</param> /// <param name="prop">the property being serialized</param> protected virtual void GenerateItemExpression(object data, JsonPath currentPath, IExpressionBuilder serializer, ObjectExpression expression, IPropertyData prop) { object value = prop.GetValue(data); if (!prop.ShouldWriteValue(this.Config, value)) return; Expression valueExpr; if (prop.HasConverter) { valueExpr = serializer.Serialize(value, currentPath.Append(prop.Alias), prop.TypeConverter); } else { valueExpr = serializer.Serialize(value, currentPath.Append(prop.Alias)); } if (value != null && !ReflectionUtils.AreEquivalentTypes(value.GetType(), prop.PropertyType)) { valueExpr = new CastExpression(value.GetType(), valueExpr); } expression.Add(prop.Alias, valueExpr); }
/// <summary> /// Parses a javascript object /// </summary> /// <returns></returns> private ObjectExpression ParseObject() { Token tok = ReadToken(); Debug.Assert(tok == LBraceToken); ObjectExpression value = new ObjectExpression() { LineNumber = tok.linenumber, CharacterPosition = tok.position }; Expression item; bool first = true; while (ReadAhead(CommaToken, RBraceToken, new ExpressionMethod(ParseKeyValue), out item, ref first)) { value.Add((KeyValueExpression)item); } return value; }