Esempio n. 1
0
        /// <summary>
        /// Parses a reference to an object
        /// </summary>
        /// <returns></returns>
        private Expression ParseReference()
        {
            JsonPath refID = new JsonPath();
            Token    tok   = ReadToken();

            if (tok != ReferenceStartToken && tok != OldReferenceStartToken)
            {
                throw new ParseException(string.Format("Invalid starting token for ParseReference, Expected: {0} or {1}, got: {2}", ReferenceStartToken, OldReferenceStartToken, tok));
            }
            while (PeekToken() == PeriodToken || PeekToken() == LSquareToken)
            {
                tok = ReadToken(); // separator "."
                if (tok == PeriodToken)
                {
                    tok = ReadToken(); // type part
                }
                if (tok == LSquareToken)
                {
                    refID = refID.Append(ReadToken().value); // index
                    ReadToken();                             // ]
                }
                else if (tok.type == TokenType.Identifier)
                {
                    refID = refID.Append(tok.value);
                }
                else
                {
                    throw new ParseException("Invalid Reference, must be an identifier or array value: " + tok);
                }
            }
            return(new ReferenceExpression(refID));
        }
Esempio n. 2
0
        /// <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);
        }
Esempio n. 3
0
        public void TestMultiPath()
        {
            JsonPath path = new JsonPath(JsonPath.Root);

            path = path.Append("foo");
            path = path.Append("bar");
            Assert.AreEqual(JsonPath.Root + "['foo']['bar']", path.ToString());
        }
 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;
 }
        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 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;
        }
Esempio n. 9
0
        public void TestImmutableAdd()
        {
            JsonPath path  = new JsonPath(JsonPath.Root);
            JsonPath path2 = path.Append("foo");

            Assert.AreNotSame(path, path2);
        }
Esempio n. 10
0
        /// <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);
        }
Esempio n. 11
0
        private JsonPath GetPath(Expression expression)
        {
            if (expression.Parent == null)
            {
                return(new JsonPath());
            }

            Expression parent = GetRealExpression(expression.Parent);

            if (parent is ComplexExpressionBase)
            {
                ComplexExpressionBase complexParent = (ComplexExpressionBase)parent;
                JsonPath path = GetPath(parent);
                if (parent is ArrayExpression)
                {
                    ArrayExpression parentArray = (ArrayExpression)parent;
                    for (int i = 0; i < parentArray.Items.Count; i++)
                    {
                        if (ExpressionEqual(parentArray.Items[i], expression))
                        {
                            return(path.Append(i));
                        }
                    }
                }
                else if (parent is ObjectExpression)
                {
                    ObjectExpression parentObject = (ObjectExpression)parent;
                    foreach (KeyValueExpression kve in parentObject.Properties)
                    {
                        if (ExpressionEqual(kve.KeyExpression, expression) ||
                            ExpressionEqual(kve.ValueExpression, expression))
                        {
                            return(path.Append(kve.Key));
                        }
                    }
                }
                for (int i = 0; i < complexParent.ConstructorArguments.Count; i++)
                {
                    if (ExpressionEqual(complexParent.ConstructorArguments[i], expression))
                    {
                        return(path.Append("carg" + i));
                    }
                }
            }
            return(null);
        }
Esempio n. 12
0
        public void TestImmutableChild()
        {
            JsonPath path = new JsonPath(JsonPath.Root);

            path = path.Append("foo");
            JsonPath path2 = path.ChildReference();

            Assert.AreNotSame(path, path2);
        }
 /// <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);
 }
 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 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);
        }
Esempio n. 16
0
        /// <summary>
        /// Parses a reference to an object
        /// </summary>
        /// <returns></returns>
        private Expression ParseReference()
        {
            JsonPath refID = new JsonPath();
            Token    tok   = ReadToken();

            if (tok != ReferenceStartToken && tok != OldReferenceStartToken)
            {
                throw new ParseException(string.Format("Invalid starting token for ParseReference at Line: {0}, Position: {1}, Expected: {2} or {3}, got: {4}", tok.linenumber, tok.position, ReferenceStartToken, OldReferenceStartToken, tok));
            }
            int line = tok.linenumber;
            int pos  = tok.position;

            while (PeekToken() == PeriodToken || PeekToken() == LSquareToken)
            {
                tok = ReadToken(); // separator "."
                if (tok == PeriodToken)
                {
                    tok = ReadToken(); // type part
                }
                if (tok == LSquareToken)
                {
                    refID = refID.Append(ReadToken().value); // index
                    ReadToken();                             // ]
                }
                else if (tok.type == TokenType.Identifier)
                {
                    refID = refID.Append(tok.value);
                }
                else
                {
                    throw new ParseException(string.Format("Invalid Reference at Line: {0}, Position: {1}, must be an identifier or array value: {2}", tok.linenumber, tok.position, tok));
                }
            }
            return(new ReferenceExpression(refID)
            {
                LineNumber = line, CharacterPosition = pos
            });
        }
Esempio n. 17
0
        /// <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.Settings, 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);
        }
 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;
        }
        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);
        }
Esempio n. 21
0
        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;
        }
Esempio n. 23
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)
        {
            ITypeData handler = Settings.Types[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);
        }
        /// <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;
        }
Esempio n. 25
0
        /// <summary>
        /// Parses a reference to an object
        /// </summary>
        /// <returns></returns>
        private Expression ParseReference()
        {
            JsonPath refID = new JsonPath();
            Token tok = ReadToken();
            if (tok != ReferenceStartToken && tok != OldReferenceStartToken)
                throw new ParseException(string.Format("Invalid starting token for ParseReference, Expected: {0} or {1}, got: {2}", ReferenceStartToken, OldReferenceStartToken,tok));            
            while (PeekToken() == PeriodToken || PeekToken() == LSquareToken)
            {
                tok = ReadToken(); // separator "."
                if (tok == PeriodToken)
                    tok = ReadToken(); // type part

                if (tok == LSquareToken)
                {
                    refID = refID.Append(ReadToken().value); // index
                    ReadToken(); // ]
                }
                else if (tok.type == TokenType.Identifier)
                {
                    refID = refID.Append(tok.value);
                }
                else
                {
                    throw new ParseException("Invalid Reference, must be an identifier or array value: " + tok);
                }                
            }
            return new ReferenceExpression(refID);
        }