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;
 }
 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;
 }
 public void ResolveObjectObjectTest()
 {
     ObjectExpression root = new ObjectExpression();
     ObjectExpression child1 = new ObjectExpression();
     root.Add(new ValueExpression("child1"), child1);
     ObjectExpression child2 = new ObjectExpression();
     root.Add(new ValueExpression("child2"), child2);
     Expression actual = ResolveReference(root, "$['child2']");
     Assert.AreSame(child2, actual, "this.child2 did not resolve correctly");
 }
        public void ResolveNestedObjectTest()
        {
            ObjectExpression root = new ObjectExpression();
            ObjectExpression child1 = new ObjectExpression();
            root.Add(new ValueExpression("child1"), child1);
            ObjectExpression child2 = new ObjectExpression();
            root.Add(new ValueExpression("child2"), child2);
            ObjectExpression childA = new ObjectExpression();
            child1.Add(new ValueExpression("childA"), childA);
            ObjectExpression childB = new ObjectExpression();
            child2.Add(new ValueExpression("childB"), childB);

            Expression actual = ResolveReference(root, "$['child2']['childB']");
            Assert.AreSame(childB, actual, "$['child2']['childB'] did not resolve correctly");
        }
        /// <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;
        }
 /// <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);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Parses a javascript object
 /// </summary>
 /// <returns></returns>
 private ObjectExpression ParseObject()
 {
     Token tok = ReadToken();
     Debug.Assert(tok == LBraceToken);
     ObjectExpression value = new ObjectExpression();
     Expression item;
     bool first = true;
     while (ReadAhead(CommaToken, RBraceToken, new ExpressionMethod(ParseKeyValue), out item, ref first))
     {
         value.Add((KeyValueExpression)item);
     }
     return value;
 }
        /// <summary>
        /// Creates an json object expression from object data.
        /// </summary>
        /// <param name="data">the data to serialize</param>
        /// <param name="currentPath">current path to the object</param>
        /// <param name="serializer">serializer instance used to serialize key values</param>
        /// <returns>json object expression</returns>
        public override Expression GetExpression(object data, JsonPath currentPath, ISerializerHandler serializer)
        {
            TypeData handler = Context.GetTypeHandler(data.GetType());

            ObjectExpression expression = new ObjectExpression();

            foreach (IPropertyData prop in handler.Properties)
            {
                object value = prop.GetValue(data);
                Expression valueExpr;
                if (prop.HasConverter)
                {
                    valueExpr = serializer.Serialize(value, currentPath.Append(prop.Name), prop.TypeConverter);
                }
                else
                {
                    valueExpr = serializer.Serialize(value, currentPath.Append(prop.Name));
                }
                if (value != null && !ReflectionUtils.AreEquivalentTypes(value.GetType(), prop.PropertyType))
                {
                    valueExpr = new CastExpression(value.GetType(), valueExpr);
                }
                expression.Add(prop.Name, valueExpr);
            }
            return expression;
        }