Ejemplo n.º 1
0
 protected virtual void EvaluateColumns(DataTable table, ArrayExpression columnsExpression, IDeserializerHandler deserializer)
 {
     foreach (Expression colExpr in columnsExpression.Items)
     {
         table.Columns.Add(EvaluateColumn(colExpr, deserializer));
     }
 }
        /// <summary>
        /// Evaluates the expression and populates an existing object with keys and values.
        /// </summary>
        /// <param name="expression">the expression to evaluate</param>
        /// <param name="existingObject">the existing object to populate</param>
        /// <param name="deserializer">the deserializer instance to use to deserialize other expressions</param>
        /// <returns>a populated object</returns>
        public override object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer)
        {
            Type _dictionaryKeyType = typeof(string);
            Type _dictionaryValueType = null;
            Type genDict = existingObject.GetType().GetInterface(typeof(IDictionary<,>).Name);
            // attempt to figure out what the types of the values are, if no type is set already
            if (genDict != null)
            {
                Type[] genArgs = genDict.GetGenericArguments();
                _dictionaryKeyType = genArgs[0];
                _dictionaryValueType = genArgs[1];
            }

            ObjectExpression objectExpression = CastExpression<ObjectExpression>(expression);
            foreach (KeyValueExpression keyValue in objectExpression.Properties)
            {
                // if no type set, set one
                keyValue.KeyExpression.ResultType = _dictionaryKeyType;
                if (_dictionaryValueType != null)
                    keyValue.ValueExpression.ResultType = _dictionaryValueType;

                object keyObject = deserializer.Evaluate(keyValue.KeyExpression);
                object result = deserializer.Evaluate(keyValue.ValueExpression);
                ((IDictionary)existingObject)[keyObject] = result;
            }
            return existingObject;
        }
        /// <summary>
        /// Evaluates the expression and populates an existing object with the expression's properties
        /// </summary>
        /// <param name="expression">json object expression</param>
        /// <param name="existingObject">the existing object to populate</param>
        /// <param name="deserializer">deserializer for deserializing key values</param>
        /// <returns>deserialized object</returns>
        public override object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer)
        {
            TypeData typeHandler = Context.GetTypeHandler(existingObject.GetType());
            ObjectExpression objectExpression = (ObjectExpression)expression;
            foreach (KeyValueExpression Item in objectExpression.Properties)
            {
                // evaluate the item and let it assign itself?
                IPropertyData hndlr = typeHandler.FindProperty(Item.Key);
                if (hndlr == null)
                {
                    throw new Exception(string.Format("Could not find property {0} for type {1}", Item.Key, typeHandler.ForType));
                }
                if (hndlr.Ignored)
                {
                    switch (Context.IgnoredPropertyAction)
                    {
                        case SerializationContext.IgnoredPropertyOption.Ignore:
                            continue;
                        case SerializationContext.IgnoredPropertyOption.SetIfPossible:
                            if (!hndlr.CanWrite)
                                continue;
                            break;
                        case SerializationContext.IgnoredPropertyOption.ThrowException:
                            throw new Exception(string.Format("Can not set property {0} for type {1} because it is ignored and IgnorePropertyAction is set to ThrowException", Item.Key, typeHandler.ForType));
                    }
                }
                Expression valueExpression = Item.ValueExpression;
                valueExpression.ResultType = hndlr.PropertyType;
                object result = null;
                TypeConverterExpressionHandler converterHandler = null;
                IJsonTypeConverter converter = null;
                if (hndlr.HasConverter)
                {
                    converterHandler = (TypeConverterExpressionHandler) Context.ExpressionHandlers.Find(typeof(TypeConverterExpressionHandler));
                    converter = hndlr.TypeConverter;
                }

                if (!hndlr.CanWrite)
                {
                    result = hndlr.GetValue(existingObject);
                    if (converterHandler != null)
                    {
                        converterHandler.Evaluate(valueExpression, result, deserializer, converter);

                    }
                    else
                    {
                        deserializer.Evaluate(valueExpression, result);
                    }
                }
                else
                {
                    if (hndlr.HasConverter)
                        hndlr.SetValue(existingObject, converterHandler.Evaluate(valueExpression,deserializer,converter));
                    else
                        hndlr.SetValue(existingObject, deserializer.Evaluate(valueExpression));
                }
            }
            return existingObject;
        }
        /// <summary>
        /// Evaluates an expression and constructs the correct object instance
        /// </summary>
        /// <param name="expression">the epxression to evaluate</param>
        /// <param name="deserializer">the deserializer instance</param>
        /// <returns>constructed object</returns>
        public override object Evaluate(Expression expression, IDeserializerHandler deserializer)
        {
            Expression innerExpression = ((CastExpression)expression).Expression;

            innerExpression.ResultType = expression.ResultType;
            return(deserializer.Evaluate(innerExpression));
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Converts the expression back into an object
 /// </summary>
 /// <param name="expression">the expression</param>
 /// <param name="deserializer">the deserializer</param>
 /// <returns>null</returns>
 public override object Evaluate(Expression expression, IDeserializerHandler deserializer)
 {
     if (!(expression is NullExpression))
     {
         throw new ArgumentException("expression should be NullExpression");
     }
     return(null);
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Evaluates the expression and deserializes it.
 /// </summary>
 /// <param name="expression">json object expression</param>
 /// <param name="deserializer">deserializer for deserializing key values</param>
 /// <returns>deserialized object</returns>
 public override object Evaluate(Expression expression, IDeserializerHandler deserializer)
 {
     object value = ConstructObject(CastExpression<ObjectExpression>(expression), deserializer);
     value = Evaluate(expression, value, deserializer);
     if (value is IDeserializationCallback)
         ((IDeserializationCallback)value).OnAfterDeserialization();
     return value;
 }
 public override object Evaluate(Expression Expression, object existingObject, IDeserializerHandler deserializer)
 {
     Expression.OnObjectConstructed(existingObject);
     Type itemType = GetItemType(existingObject.GetType());
     EvaluateItems((ArrayExpression) Expression, existingObject, itemType, deserializer);
     if (existingObject is IDeserializationCallback)
         ((IDeserializationCallback)existingObject).OnAfterDeserialization();
     return existingObject;
 }
 protected virtual void EvaluateItems(ArrayExpression Expression, object Collection, Type ItemType, IDeserializerHandler deserializer)
 {
     foreach (Expression item in Expression.Items)
     {
         item.ResultType = ItemType;
         object itemResult = deserializer.Evaluate(item);
         AddItem(Collection, itemResult);
     }
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Evaluates the expression and populates an existing object with the expression's properties
 /// </summary>
 /// <param name="expression">json object expression</param>
 /// <param name="existingObject">the existing object to populate</param>
 /// <param name="deserializer">deserializer for deserializing key values</param>
 /// <returns>deserialized object</returns>
 public override object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer)
 {
     TypeData typeHandler = Config.GetTypeHandler(existingObject.GetType());
     ObjectExpression objectExpression = CastExpression<ObjectExpression>(expression);
     foreach (KeyValueExpression Item in objectExpression.Properties)
     {
         EvaluateItem(existingObject, deserializer, typeHandler, Item);
     }
     return existingObject;
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Evaluates the expression and deserializes it.
        /// </summary>
        /// <param name="expression">json object expression</param>
        /// <param name="deserializer">deserializer for deserializing key values</param>
        /// <returns>deserialized object</returns>
        public override object Evaluate(Expression expression, IDeserializerHandler deserializer)
        {
            object value = ConstructObject(CastExpression <ObjectExpression>(expression), deserializer);

            value = Evaluate(expression, value, deserializer);
            if (value is IDeserializationCallback)
            {
                ((IDeserializationCallback)value).OnAfterDeserialization();
            }
            return(value);
        }
 public override object Evaluate(Expression expression, IDeserializerHandler deserializer)
 {
     ValueExpression value = (ValueExpression)expression;
     if (value.ResultType.IsEnum)
         return Enum.Parse(value.ResultType, value.StringValue);
     else if (value.ResultType == typeof(object))
         return value.StringValue;
     else if (value.ResultType == typeof(string))
         return value.StringValue;
     else
         return Convert.ChangeType(value.Value, value.ResultType, CultureInfo.InvariantCulture);
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Deserializes an expression by populating an existing object collection with the expression's items.
 /// </summary>
 /// <param name="expression">the expression to deserialize</param>
 /// <param name="existingObject">the collection to populate</param>
 /// <param name="deserializer">deserializer to deserialize list items</param>
 /// <returns>deserialized object</returns>
 public override object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer)
 {
     Type ItemType;
     ArrayExpression list = (ArrayExpression)expression;
     ICollectionBuilder builder = ConstructBuilder(existingObject, list, out ItemType);
     object result = EvaluateItems(list, builder, ItemType, deserializer);
     if (result is IDeserializationCallback)
     {
         ((IDeserializationCallback)result).OnAfterDeserialization();
     }
     return result;
 }
 /// <summary>
 /// Converts an expression to an object by first Evaluating the expression as its converted type and
 /// then converting that result using the specified type converter.
 /// </summary>
 /// <param name="expression">the expression to convert</param>
 /// <param name="deserializer">deserializer instance</param>
 /// <param name="converter">the converter to use to convert the object</param>
 /// <returns>an object created from the expression</returns>
 public object Evaluate(Expression expression, IDeserializerHandler deserializer, IJsonTypeConverter converter)
 {
     Type sourceType = expression.ResultType;
     Type destType = converter.GetSerializedType(sourceType);
     expression.ResultType = destType;
     object tempResult = deserializer.Evaluate(expression);
     object result = converter.ConvertTo(tempResult, sourceType, Context);
     expression.OnObjectConstructed(result);
     if (result is IDeserializationCallback)
     {
         ((IDeserializationCallback)result).OnAfterDeserialization();
     }
     return result;
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Constructs a new instance of the object represented by the expression.
        /// </summary>
        /// <param name="expression">json object expression</param>
        /// <param name="deserializer">deserializer for deserializing constructor arguments if any</param>
        /// <returns>constructed, but unpopulated object</returns>
        protected virtual object ConstructObject(ObjectExpression expression, IDeserializerHandler deserializer)
        {
            ITypeData handler = Settings.Types[expression.ResultType];

            // set the default type if none set
            if (expression.ConstructorArguments.Count > 0)
            {
                // old way expects parameters in the constructor list
                ResolveConstructorTypes(Settings, expression);
            }
            else
            {
                foreach (IPropertyData parameter in handler.ConstructorParameters)
                {
                    int propLocation = expression.IndexOf(parameter.Alias);
                    if (propLocation >= 0)
                    {
                        Expression arg = expression.Properties[propLocation].ValueExpression;
                        arg.ResultType = parameter.PropertyType;
                        expression.ConstructorArguments.Add(arg);
                        expression.Properties.RemoveAt(propLocation);
                    }
                    else
                    {
                        expression.ConstructorArguments.Add(new NullExpression());
                    }
                }
            }

            object[] args = new object[expression.ConstructorArguments.Count];

            for (int i = 0; i < args.Length; i++)
            {
                Expression carg = expression.ConstructorArguments[i];
                if (i < handler.ConstructorParameters.Count && handler.ConstructorParameters[i].HasConverter)
                {
                    TypeConverterExpressionHandler converterHandler = (TypeConverterExpressionHandler)Settings.ExpressionHandlers.Find(typeof(TypeConverterExpressionHandler));
                    args[i] = converterHandler.Evaluate(carg, deserializer, handler.ConstructorParameters[i].TypeConverter);
                }
                else
                {
                    args[i] = deserializer.Evaluate(carg);
                }
            }
            object result = handler.CreateInstance(args);

            expression.OnObjectConstructed(result);
            return(result);
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Converts an expression to an object by first Evaluating the expression as its converted type and
 /// then converting that result using the specified type converter.
 /// </summary>
 /// <param name="expression">the expression to convert</param>
 /// <param name="deserializer">deserializer instance</param>
 /// <param name="converter">the converter to use to convert the object</param>
 /// <returns>an object created from the expression</returns>
 public object Evaluate(Expression expression, IDeserializerHandler deserializer, IJsonTypeConverter converter)
 {
     Type sourceType = expression.ResultType;
     Type destType = converter.GetSerializedType(sourceType);
     expression.ResultType = destType;
     object tempResult = deserializer.Evaluate(expression);
     //TODO: Cast for now to avoid breaking compatibility
     object result = converter.ConvertTo(tempResult, sourceType, (SerializationContext) Config);
     expression.OnObjectConstructed(result);
     if (result is IDeserializationCallback)
     {
         ((IDeserializationCallback)result).OnAfterDeserialization();
     }
     return result;
 }
Ejemplo n.º 16
0
 protected virtual void EvaluateRows(DataTable table, ArrayExpression rowsExpression, IDeserializerHandler deserializer)
 {
     for (int i = 0; i < rowsExpression.Items.Count; i++)
     {
         ArrayExpression rowExpr = (ArrayExpression) rowsExpression.Items[i];
         for (int j = 0; j < rowExpr.Items.Count; j++)
         {
             rowExpr.Items[j] = new CastExpression(table.Columns[j].DataType, rowExpr.Items[j]);
         }
         rowExpr.ResultType = typeof(object[]);
         DataRow dr = table.NewRow();
         dr.ItemArray = (object[]) deserializer.Evaluate(rowExpr);
         table.Rows.Add(dr);
     }
 }
Ejemplo n.º 17
0
        /// <summary>
        /// Converts an expression to an object by first Evaluating the expression as its converted type and
        /// then converting that result using the specified type converter.
        /// </summary>
        /// <param name="expression">the expression to convert</param>
        /// <param name="deserializer">deserializer instance</param>
        /// <param name="converter">the converter to use to convert the object</param>
        /// <returns>an object created from the expression</returns>
        public object Evaluate(Expression expression, IDeserializerHandler deserializer, IJsonTypeConverter converter)
        {
            Type sourceType = expression.ResultType;
            Type destType   = converter.GetSerializedType(sourceType);

            expression.ResultType = destType;
            object tempResult = deserializer.Evaluate(expression);
            object result     = converter.ConvertTo(tempResult, sourceType, Context);

            expression.OnObjectConstructed(result);
            if (result is IDeserializationCallback)
            {
                ((IDeserializationCallback)result).OnAfterDeserialization();
            }
            return(result);
        }
        /// <summary>
        /// Converts an expression to an object by first Evaluating the expression as its converted type and
        /// then converting that result using a type converter.
        /// </summary>
        /// <param name="expression">the expression to convert</param>
        /// <param name="deserializer">deserializer instance</param>
        /// <returns>an object created from the expression</returns>
        public override object Evaluate(Expression expression, IDeserializerHandler deserializer)
        {
            Type sourceType = expression.ResultType;
            TypeData handler = Context.TypeHandlerFactory[sourceType];
            IJsonTypeConverter converter;
            if (typeof(IJsonTypeConverter).IsAssignableFrom(sourceType))
            {
                converter = (IJsonTypeConverter) Activator.CreateInstance(sourceType);
            }
            else
            {
                converter = handler.TypeConverter;
            }

            return Evaluate(expression, deserializer, converter);
        }
Ejemplo n.º 19
0
        public override object Evaluate(Expression expression, IDeserializerHandler deserializer)
        {
            DataTable table = new DataTable();
            ObjectExpression tableExpr = (ObjectExpression)expression;
            EvaluateColumns(table, (ArrayExpression)tableExpr["Columns"], deserializer);
            // remove the columns expression, it's been evaluated already
            tableExpr.Properties.RemoveAt(tableExpr.IndexOf("Columns"));

            // now the rows
            EvaluateRows(table, (ArrayExpression)tableExpr["Rows"], deserializer);
            // remove the rows expression, it's been evaluated already
            tableExpr.Properties.RemoveAt(tableExpr.IndexOf("Rows"));

            // fill in any remaining properties
            return base.Evaluate(expression, table, deserializer);
        }
        public override object Evaluate(Expression expression, IDeserializerHandler deserializer)
        {
            DataTable        table     = new DataTable();
            ObjectExpression tableExpr = (ObjectExpression)expression;

            EvaluateColumns(table, (ArrayExpression)tableExpr["Columns"], deserializer);
            // remove the columns expression, it's been evaluated already
            tableExpr.Properties.RemoveAt(tableExpr.IndexOf("Columns"));

            // now the rows
            EvaluateRows(table, (ArrayExpression)tableExpr["Rows"], deserializer);
            // remove the rows expression, it's been evaluated already
            tableExpr.Properties.RemoveAt(tableExpr.IndexOf("Rows"));

            // fill in any remaining properties
            return(base.Evaluate(expression, table, deserializer));
        }
        /// <summary>
        /// Converts an expression to an object by first Evaluating the expression as its converted type and
        /// then converting that result using the specified type converter.
        /// </summary>
        /// <param name="expression">the expression to convert</param>
        /// <param name="deserializer">deserializer instance</param>
        /// <param name="converter">the converter to use to convert the object</param>
        /// <returns>an object created from the expression</returns>
        public object Evaluate(Expression expression, IDeserializerHandler deserializer, IJsonTypeConverter converter)
        {
            Type sourceType = expression.ResultType;
            Type destType   = converter.GetSerializedType(sourceType);

            expression.ResultType = destType;
            object tempResult = deserializer.Evaluate(expression);
            //TODO: Cast for now to avoid breaking compatibility
            object result = converter.ConvertTo(tempResult, sourceType, (SerializerSettings)Settings);

            expression.OnObjectConstructed(result);
            if (result is IDeserializationCallback)
            {
                ((IDeserializationCallback)result).OnAfterDeserialization();
            }
            return(result);
        }
        /// <summary>
        /// Converts an expression to an object by first Evaluating the expression as its converted type and
        /// then converting that result using a type converter.
        /// </summary>
        /// <param name="expression">the expression to convert</param>
        /// <param name="deserializer">deserializer instance</param>
        /// <returns>an object created from the expression</returns>
        public override object Evaluate(Expression expression, IDeserializerHandler deserializer)
        {
            Type               sourceType = expression.ResultType;
            ITypeData          handler    = Settings.Types[sourceType];
            IJsonTypeConverter converter;

            if (typeof(IJsonTypeConverter).IsAssignableFrom(sourceType))
            {
                converter = (IJsonTypeConverter)Activator.CreateInstance(sourceType);
            }
            else
            {
                converter = handler.TypeConverter;
            }

            return(Evaluate(expression, deserializer, converter));
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Constructs a new instance of the object represented by the expression.
        /// </summary>
        /// <param name="expression">json object expression</param>
        /// <param name="deserializer">deserializer for deserializing constructor arguments if any</param>
        /// <returns>constructed, but unpopulated object</returns>
        protected virtual object ConstructObject(ObjectExpression expression, IDeserializerHandler deserializer)
        {
            TypeData handler = Context.GetTypeHandler(expression.ResultType);

            // set the default type if none set
            if (expression.ConstructorArguments.Count > 0)
            {
                // old way expects parameters in the constructor list
                ResolveConstructorTypes(Context, expression);
            }
            else
            {
                foreach (IPropertyData parameter in handler.ConstructorParameters)
                {
                    int propLocation = expression.IndexOf(parameter.Name);
                    if (propLocation >= 0)
                    {
                        Expression arg = expression.Properties[propLocation].ValueExpression;
                        arg.ResultType = parameter.PropertyType;
                        expression.ConstructorArguments.Add(arg);
                        expression.Properties.RemoveAt(propLocation);
                    }
                    else
                    {
                        expression.ConstructorArguments.Add(new NullExpression());
                    }
                }
            }

            object[] args = new object[expression.ConstructorArguments.Count];

            for (int i = 0; i < args.Length; i++)
            {
                Expression carg = expression.ConstructorArguments[i];
                args[i] = deserializer.Evaluate(carg);
            }
            object result = handler.CreateInstance(args);

            expression.OnObjectConstructed(result);
            return(result);
        }
Ejemplo n.º 24
0
        public override object Evaluate(Expression expression, IDeserializerHandler deserializer)
        {
            ValueExpression value = CastExpression <ValueExpression>(expression);

            if (value.ResultType.IsEnum)
            {
                return(Enum.Parse(value.ResultType, value.StringValue));
            }
            else if (value.ResultType == typeof(object))
            {
                return(value.StringValue);
            }
            else if (value.ResultType == typeof(string))
            {
                return(value.StringValue);
            }
            else
            {
                return(Convert.ChangeType(value.Value, value.ResultType, CultureInfo.InvariantCulture));
            }
        }
 /// <summary>
 /// This method is invalid for Reference expressions.  References can't be updated
 /// </summary>
 /// <param name="expression">reference expression</param>
 /// <param name="existingObject">existing object, ignored</param>
 /// <param name="deserializer">deserializer instance</param>
 /// <returns>nothing</returns>
 /// <exception cref="InvalidOperationException">References cannot be updated</exception>
 public override object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer)
 {
     throw new InvalidOperationException("Cannot update a reference");
 }
 /// <summary>
 /// Resolves the reference to another object and returns that object
 /// </summary>
 /// <param name="expression">the expression to deserialize</param>
 /// <param name="deserializer">deserializer instance to use to deserialize any child expressions</param>
 /// <returns>a fully deserialized object</returns>
 public override object Evaluate(Expression expression, IDeserializerHandler deserializer)
 {
     return ((ReferenceExpression)expression).ReferencedValue;
 }
Ejemplo n.º 27
0
 /// <summary>
 /// Deserializes an expression into a collection instance
 /// </summary>
 /// <param name="expression">the expression to deserialize</param>
 /// <param name="deserializer">deserializer to deserialize list items</param>
 /// <returns>deserialized object</returns>
 public override object Evaluate(Expression expression, IDeserializerHandler deserializer)
 {
     return(Evaluate(expression, null, deserializer));
 }
Ejemplo n.º 28
0
        /// <summary>
        /// Evaluates the expression and populates an existing object with the expression's properties
        /// </summary>
        /// <param name="expression">json object expression</param>
        /// <param name="existingObject">the existing object to populate</param>
        /// <param name="deserializer">deserializer for deserializing key values</param>
        /// <returns>deserialized object</returns>
        public override object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer)
        {
            ITypeData        typeHandler      = Settings.Types[existingObject.GetType()];
            ObjectExpression objectExpression = CastExpression <ObjectExpression>(expression);

            foreach (KeyValueExpression Item in objectExpression.Properties)
            {
                EvaluateItem(existingObject, deserializer, typeHandler, Item);
            }
            return(existingObject);
        }
 /// <summary>
 /// This method is invalid for TypeConverterExpressionHandler
 /// </summary>
 /// <exception cref="NotSupportedException">Evaluating an existing object is not supported by TypeConverterExpressionHandler</exception>
 public void Evaluate(Expression valueExpression, object result, IDeserializerHandler deserializer, IJsonTypeConverter converter)
 {
     //TODO: possibly allow this if the type implements IJsonTypeConverter itself
     throw new NotSupportedException("Cannot convert an existing object.");
 }
Ejemplo n.º 30
0
        /// <summary>
        /// Evaluates the items in the expression and assigns them to the collection using the builder
        /// </summary>
        /// <param name="expression">the expression to evaluate</param>
        /// <param name="builder">builder used to build the collection</param>
        /// <param name="itemType">the type of the collection's elements</param>
        /// <param name="deserializer">deserializer instance to deserialize items</param>
        /// <returns>evaluated collection object</returns>
        protected virtual object EvaluateItems(ArrayExpression expression, ICollectionBuilder builder, Type itemType, IDeserializerHandler deserializer)
        {
            object result = null;
            bool   constructedEventSent = false;

            try
            {
                result = builder.GetReference();
                expression.OnObjectConstructed(result);
                constructedEventSent = true;
            }
            catch
            {
                // this might fail if the builder's not ready
            }
            foreach (Expression item in expression.Items)
            {
                item.ResultType = itemType;
                object itemResult = deserializer.Evaluate(item);
                builder.Add(itemResult);
            }
            result = builder.GetResult();
            if (!constructedEventSent)
            {
                expression.OnObjectConstructed(result);
            }
            return(result);
        }
Ejemplo n.º 31
0
 protected virtual DataColumn EvaluateColumn(Expression colExpr, IDeserializerHandler deserializer)
 {
     colExpr.ResultType = typeof(DataColumn);
     return (DataColumn) deserializer.Evaluate(colExpr);
 }
Ejemplo n.º 32
0
        /// <summary>
        /// Deserializes an expression by populating an existing object collection with the expression's items.
        /// </summary>
        /// <param name="expression">the expression to deserialize</param>
        /// <param name="existingObject">the collection to populate</param>
        /// <param name="deserializer">deserializer to deserialize list items</param>
        /// <returns>deserialized object</returns>
        public override object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer)
        {
            Type               ItemType;
            ArrayExpression    list    = (ArrayExpression)expression;
            ICollectionBuilder builder = ConstructBuilder(existingObject, list, out ItemType);
            object             result  = EvaluateItems(list, builder, ItemType, deserializer);

            if (result is IDeserializationCallback)
            {
                ((IDeserializationCallback)result).OnAfterDeserialization();
            }
            return(result);
        }
 /// <summary>
 /// Converts an existing object
 /// </summary>
 /// <param name="expression">the expression</param>
 /// <param name="existingObject">an existing object</param>
 /// <param name="deserializer">deserializer</param>
 /// <returns>the existing object</returns>
 public override object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer)
 {
     return existingObject;
 }
 protected virtual void EvaluateRows(DataTable table, ArrayExpression rowsExpression, IDeserializerHandler deserializer)
 {
     for (int i = 0; i < rowsExpression.Items.Count; i++)
     {
         ArrayExpression rowExpr = (ArrayExpression)rowsExpression.Items[i];
         for (int j = 0; j < rowExpr.Items.Count; j++)
         {
             rowExpr.Items[j] = new CastExpression(table.Columns[j].DataType, rowExpr.Items[j]);
         }
         rowExpr.ResultType = typeof(object[]);
         DataRow dr = table.NewRow();
         dr.ItemArray = (object[])deserializer.Evaluate(rowExpr);
         table.Rows.Add(dr);
     }
 }
Ejemplo n.º 35
0
        protected virtual void EvaluateItem(object existingObject, IDeserializerHandler deserializer, TypeData typeHandler, KeyValueExpression Item)
        {
            // evaluate the item and let it assign itself?
            IPropertyData hndlr = typeHandler.FindPropertyByAlias(Item.Key);
            if (hndlr == null)
            {
                switch (this.Config.MissingPropertyAction)
                {
                    case MissingPropertyOptions.Ignore:
                        return;
                    case MissingPropertyOptions.ThrowException:
                        throw new Exception(string.Format("Could not find property {0} for type {1}", Item.Key, typeHandler.ForType));
                    default:
                        throw new InvalidOperationException("Unhandled MissingPropertyAction: " + this.Config.MissingPropertyAction);
                }
            }
            if (hndlr.Ignored)
            {
                switch (Config.IgnoredPropertyAction)
                {
                    case SerializationContext.IgnoredPropertyOption.Ignore:
                        return;
                    case SerializationContext.IgnoredPropertyOption.SetIfPossible:
                        if (!hndlr.CanWrite)
                            return;
                        break;
                    case SerializationContext.IgnoredPropertyOption.ThrowException:
                        throw new Exception(string.Format("Can not set property {0} for type {1} because it is ignored and IgnorePropertyAction is set to ThrowException", Item.Key, typeHandler.ForType));
                }
            }
            Expression valueExpression = Item.ValueExpression;
            valueExpression.ResultType = hndlr.PropertyType;
            object result = null;
            TypeConverterExpressionHandler converterHandler = null;
            IJsonTypeConverter converter = null;
            if (hndlr.HasConverter)
            {
                converterHandler = (TypeConverterExpressionHandler)Config.ExpressionHandlers.Find(typeof(TypeConverterExpressionHandler));
                converter = hndlr.TypeConverter;
            }

            if (!hndlr.CanWrite)
            {
                result = hndlr.GetValue(existingObject);
                if (converterHandler != null)
                {
                    converterHandler.Evaluate(valueExpression, result, deserializer, converter);

                }
                else
                {
                    deserializer.Evaluate(valueExpression, result);
                }
            }
            else
            {
                if (hndlr.HasConverter)
                    hndlr.SetValue(existingObject, converterHandler.Evaluate(valueExpression, deserializer, converter));
                else
                    hndlr.SetValue(existingObject, deserializer.Evaluate(valueExpression));
            }
        }
Ejemplo n.º 36
0
        /// <summary>
        /// Constructs a new instance of the object represented by the expression.
        /// </summary>
        /// <param name="expression">json object expression</param>
        /// <param name="deserializer">deserializer for deserializing constructor arguments if any</param>
        /// <returns>constructed, but unpopulated object</returns>
        protected virtual object ConstructObject(ObjectExpression expression, IDeserializerHandler deserializer)
        {
            TypeData handler = Config.GetTypeHandler(expression.ResultType);
            // set the default type if none set
            if (expression.ConstructorArguments.Count > 0)
            {
                // old way expects parameters in the constructor list
                ResolveConstructorTypes(Config, expression);
            }
            else
            {
                foreach (IPropertyData parameter in handler.ConstructorParameters)
                {
                    int propLocation = expression.IndexOf(parameter.Alias);
                    if (propLocation >= 0)
                    {
                        Expression arg = expression.Properties[propLocation].ValueExpression;
                        arg.ResultType = parameter.PropertyType;
                        expression.ConstructorArguments.Add(arg);
                        expression.Properties.RemoveAt(propLocation);
                    }
                    else
                    {
                        expression.ConstructorArguments.Add(new NullExpression());
                    }
                }
            }

            object[] args = new object[expression.ConstructorArguments.Count];

            for (int i = 0; i < args.Length; i++)
            {
                Expression carg = expression.ConstructorArguments[i];
                if (i < handler.ConstructorParameters.Count && handler.ConstructorParameters[i].HasConverter)
                {
                    TypeConverterExpressionHandler converterHandler = (TypeConverterExpressionHandler)Config.ExpressionHandlers.Find(typeof(TypeConverterExpressionHandler));
                    args[i] = converterHandler.Evaluate(carg, deserializer, handler.ConstructorParameters[i].TypeConverter);
                }
                else
                {
                    args[i] = deserializer.Evaluate(carg);
                }
            }
            object result = handler.CreateInstance(args);
            expression.OnObjectConstructed(result);
            return result;
        }
 protected virtual void EvaluateColumns(DataTable table, ArrayExpression columnsExpression, IDeserializerHandler deserializer)
 {
     foreach (Expression colExpr in columnsExpression.Items)
     {
         table.Columns.Add(EvaluateColumn(colExpr, deserializer));
     }
 }
        public override object Evaluate(Expression expression, IDeserializerHandler deserializer)
        {
            ValueExpression valueExpr = CastExpression <ValueExpression>(expression);

            return(DateTime.ParseExact(valueExpr.StringValue, dateFormat, Culture, dateTimeStyle));
        }
Ejemplo n.º 39
0
 /// <summary>
 /// Deserializes an expression into a collection instance
 /// </summary>
 /// <param name="expression">the expression to deserialize</param>
 /// <param name="deserializer">deserializer to deserialize list items</param>
 /// <returns>deserialized object</returns>
 public override object Evaluate(Expression expression, IDeserializerHandler deserializer)
 {
     return Evaluate(expression, null, deserializer);
 }
Ejemplo n.º 40
0
 /// <summary>
 /// Convert the expression into an object by populating an existing object with any necessary values.
 /// The existingObject will usually come from the get method of a property on an object that doesn't
 /// allow writing to the property.
 /// </summary>
 /// <param name="expression">the epxression to deserialize</param>
 /// <param name="existingObject">an existing object to populate</param>
 /// <param name="deserializer">deserializer instance to use to deserialize any child expressions</param>
 /// <returns>a fully deserialized object</returns>
 public abstract object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer);
Ejemplo n.º 41
0
 public override object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer)
 {
     throw new InvalidOperationException("Value types can not be updated");
 }
Ejemplo n.º 42
0
 /// <summary>
 /// Converts an existing object
 /// </summary>
 /// <param name="expression">the expression</param>
 /// <param name="existingObject">an existing object</param>
 /// <param name="deserializer">deserializer</param>
 /// <returns>the existing object</returns>
 public override object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer)
 {
     return(existingObject);
 }
Ejemplo n.º 43
0
 /// <summary>
 /// Evaluates the items in the expression and assigns them to the collection using the builder
 /// </summary>
 /// <param name="expression">the expression to evaluate</param>
 /// <param name="builder">builder used to build the collection</param>
 /// <param name="itemType">the type of the collection's elements</param>
 /// <param name="deserializer">deserializer instance to deserialize items</param>
 /// <returns>evaluated collection object</returns>
 protected virtual object EvaluateItems(ArrayExpression expression, ICollectionBuilder builder, Type itemType, IDeserializerHandler deserializer)
 {
     object result = null;
     bool constructedEventSent = false;
     try
     {
         result = builder.GetReference();
         expression.OnObjectConstructed(result);
         constructedEventSent = true;
     }
     catch
     {
         // this might fail if the builder's not ready
     }
     foreach (Expression item in expression.Items)
     {
         item.ResultType = itemType;
         object itemResult = deserializer.Evaluate(item);
         builder.Add(itemResult);
     }
     result = builder.GetResult();
     if (!constructedEventSent)
         expression.OnObjectConstructed(result);
     return result;
 }
Ejemplo n.º 44
0
        /// <summary>
        /// Evaluates the expression and populates an existing object with keys and values.
        /// </summary>
        /// <param name="expression">the expression to evaluate</param>
        /// <param name="existingObject">the existing object to populate</param>
        /// <param name="deserializer">the deserializer instance to use to deserialize other expressions</param>
        /// <returns>a populated object</returns>
        public override object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer)
        {
            Type _dictionaryKeyType   = typeof(string);
            Type _dictionaryValueType = null;
            Type genDict = existingObject.GetType().GetInterface(typeof(IDictionary <,>).Name);

            // attempt to figure out what the types of the values are, if no type is set already
            if (genDict != null)
            {
                Type[] genArgs = genDict.GetGenericArguments();
                _dictionaryKeyType   = genArgs[0];
                _dictionaryValueType = genArgs[1];
            }

            ObjectExpression objectExpression = CastExpression <ObjectExpression>(expression);

            foreach (KeyValueExpression keyValue in objectExpression.Properties)
            {
                // if no type set, set one
                keyValue.KeyExpression.ResultType = _dictionaryKeyType;
                if (_dictionaryValueType != null)
                {
                    keyValue.ValueExpression.ResultType = _dictionaryValueType;
                }

                object keyObject = deserializer.Evaluate(keyValue.KeyExpression);
                object result    = deserializer.Evaluate(keyValue.ValueExpression);
                ((IDictionary)existingObject)[keyObject] = result;
            }
            return(existingObject);
        }
 /// <summary>
 /// Resolves the reference to another object and returns that object
 /// </summary>
 /// <param name="expression">the expression to deserialize</param>
 /// <param name="deserializer">deserializer instance to use to deserialize any child expressions</param>
 /// <returns>a fully deserialized object</returns>
 public override object Evaluate(Expression expression, IDeserializerHandler deserializer)
 {
     return(((ReferenceExpression)expression).ReferencedValue);
 }
Ejemplo n.º 46
0
        protected virtual void EvaluateItem(object existingObject, IDeserializerHandler deserializer, ITypeData typeHandler, KeyValueExpression Item)
        {
            // evaluate the item and let it assign itself?
            IPropertyData hndlr = typeHandler.FindPropertyByAlias(Item.Key);

            if (hndlr == null)
            {
                switch (this.Settings.MissingPropertyAction)
                {
                case MissingPropertyOptions.Ignore:
                    return;

                case MissingPropertyOptions.ThrowException:
                    throw new Exception(string.Format("Could not find property {0} for type {1}", Item.Key, typeHandler.ForType));

                default:
                    throw new InvalidOperationException("Unhandled MissingPropertyAction: " + this.Settings.MissingPropertyAction);
                }
            }
            if (hndlr.Ignored)
            {
                switch (Settings.IgnoredPropertyAction)
                {
                case IgnoredPropertyOption.Ignore:
                    return;

                case IgnoredPropertyOption.SetIfPossible:
                    if (!hndlr.CanWrite)
                    {
                        return;
                    }
                    break;

                case IgnoredPropertyOption.ThrowException:
                    throw new Exception(string.Format("Can not set property {0} for type {1} because it is ignored and IgnorePropertyAction is set to ThrowException", Item.Key, typeHandler.ForType));
                }
            }
            Expression valueExpression = Item.ValueExpression;

            valueExpression.ResultType = hndlr.PropertyType;
            object result = null;
            TypeConverterExpressionHandler converterHandler = null;
            IJsonTypeConverter             converter        = null;

            if (hndlr.HasConverter)
            {
                converterHandler = (TypeConverterExpressionHandler)Settings.ExpressionHandlers.Find(typeof(TypeConverterExpressionHandler));
                converter        = hndlr.TypeConverter;
            }

            if (!hndlr.CanWrite)
            {
                result = hndlr.GetValue(existingObject);
                if (converterHandler != null)
                {
                    converterHandler.Evaluate(valueExpression, result, deserializer, converter);
                }
                else
                {
                    deserializer.Evaluate(valueExpression, result);
                }
            }
            else
            {
                if (hndlr.HasConverter)
                {
                    hndlr.SetValue(existingObject, converterHandler.Evaluate(valueExpression, deserializer, converter));
                }
                else
                {
                    hndlr.SetValue(existingObject, deserializer.Evaluate(valueExpression));
                }
            }
        }
 public override object Evaluate(Expression expression, IDeserializerHandler deserializer)
 {
     ValueExpression valueExpr = (ValueExpression)expression;
     return DateTime.ParseExact(valueExpr.StringValue, dateFormat, Culture, dateTimeStyle);
 }
Ejemplo n.º 48
0
 /// <summary>
 /// Evaluates an expression and populates an existing object with any necessary values
 /// </summary>
 /// <param name="expression">expression to evaluate</param>
 /// <param name="existingObject">the object to populate</param>
 /// <param name="deserializer">the deserializer instance</param>
 /// <returns>constructed object</returns>
 public override object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer)
 {
     Expression innerExpression = ((CastExpression)expression).Expression;
     innerExpression.ResultType = expression.ResultType;
     return deserializer.Evaluate(innerExpression, existingObject);
 }
 /// <summary>
 /// This method is invalid for TypeConverterExpressionHandler
 /// </summary>
 /// <exception cref="NotSupportedException">Evaluating an existing object is not supported by TypeConverterExpressionHandler</exception>
 public void Evaluate(Expression valueExpression, object result, IDeserializerHandler deserializer, IJsonTypeConverter converter)
 {
     //TODO: possibly allow this if the type implements IJsonTypeConverter itself
     throw new NotSupportedException("Cannot convert an existing object.");
 }
Ejemplo n.º 50
0
        /// <summary>
        /// Evaluates the expression and populates an existing object with the expression's properties
        /// </summary>
        /// <param name="expression">json object expression</param>
        /// <param name="existingObject">the existing object to populate</param>
        /// <param name="deserializer">deserializer for deserializing key values</param>
        /// <returns>deserialized object</returns>
        public override object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer)
        {
            TypeData         typeHandler      = Context.GetTypeHandler(existingObject.GetType());
            ObjectExpression objectExpression = (ObjectExpression)expression;

            foreach (KeyValueExpression Item in objectExpression.Properties)
            {
                // evaluate the item and let it assign itself?
                IPropertyData hndlr = typeHandler.FindProperty(Item.Key);
                if (hndlr == null)
                {
                    throw new Exception(string.Format("Could not find property {0} for type {1}", Item.Key, typeHandler.ForType));
                }
                if (hndlr.Ignored)
                {
                    switch (Context.IgnoredPropertyAction)
                    {
                    case SerializationContext.IgnoredPropertyOption.Ignore:
                        continue;

                    case SerializationContext.IgnoredPropertyOption.SetIfPossible:
                        if (!hndlr.CanWrite)
                        {
                            continue;
                        }
                        break;

                    case SerializationContext.IgnoredPropertyOption.ThrowException:
                        throw new Exception(string.Format("Can not set property {0} for type {1} because it is ignored and IgnorePropertyAction is set to ThrowException", Item.Key, typeHandler.ForType));
                    }
                }
                Expression valueExpression = Item.ValueExpression;
                valueExpression.ResultType = hndlr.PropertyType;
                object result = null;
                TypeConverterExpressionHandler converterHandler = null;
                IJsonTypeConverter             converter        = null;
                if (hndlr.HasConverter)
                {
                    converterHandler = (TypeConverterExpressionHandler)Context.ExpressionHandlers.Find(typeof(TypeConverterExpressionHandler));
                    converter        = hndlr.TypeConverter;
                }

                if (!hndlr.CanWrite)
                {
                    result = hndlr.GetValue(existingObject);
                    if (converterHandler != null)
                    {
                        converterHandler.Evaluate(valueExpression, result, deserializer, converter);
                    }
                    else
                    {
                        deserializer.Evaluate(valueExpression, result);
                    }
                }
                else
                {
                    if (hndlr.HasConverter)
                    {
                        hndlr.SetValue(existingObject, converterHandler.Evaluate(valueExpression, deserializer, converter));
                    }
                    else
                    {
                        hndlr.SetValue(existingObject, deserializer.Evaluate(valueExpression));
                    }
                }
            }
            return(existingObject);
        }
 protected virtual object ConstructCollection(ArrayExpression Expression, IDeserializerHandler deserializer)
 {
     object result = Activator.CreateInstance(Expression.ResultType);
     return result;
 }
Ejemplo n.º 52
0
 /// <summary>
 /// Convert the expression into an object by populating an existing object with any necessary values.
 /// The existingObject will usually come from the get method of a property on an object that doesn't
 /// allow writing to the property.
 /// </summary>
 /// <param name="expression">the epxression to deserialize</param>
 /// <param name="existingObject">an existing object to populate</param>
 /// <param name="deserializer">deserializer instance to use to deserialize any child expressions</param>
 /// <returns>a fully deserialized object</returns>
 public abstract object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer);
 public override object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer)
 {
     throw new InvalidOperationException("Value types can not be updated");
 }
Ejemplo n.º 54
0
 protected override void EvaluateItems(ArrayExpression Expression, object Collection, Type ItemType, IDeserializerHandler deserializer)
 {
     base.EvaluateItems(Expression, Collection, ItemType, deserializer);
 }
 /// <summary>
 /// This method is invalid for Reference expressions.  References can't be updated
 /// </summary>
 /// <param name="expression">reference expression</param>
 /// <param name="existingObject">existing object, ignored</param>
 /// <param name="deserializer">deserializer instance</param>
 /// <returns>nothing</returns>
 /// <exception cref="InvalidOperationException">References cannot be updated</exception>
 public override object Evaluate(Expression expression, object existingObject, IDeserializerHandler deserializer)
 {
     throw new InvalidOperationException("Cannot update a reference");
 }
 public override object Evaluate(Expression Expression, IDeserializerHandler deserializer)
 {
     object collection = ConstructCollection((ArrayExpression)Expression, deserializer);
     return Evaluate(Expression, collection, deserializer);
 }
 /// <summary>
 /// Converts the expression back into an object
 /// </summary>
 /// <param name="expression">the expression</param>
 /// <param name="deserializer">the deserializer</param>
 /// <returns>null</returns>
 public override object Evaluate(Expression expression, IDeserializerHandler deserializer)
 {
     if (!(expression is NullExpression))
         throw new ArgumentException("expression should be NullExpression");
     return null;
 }
 protected virtual DataColumn EvaluateColumn(Expression colExpr, IDeserializerHandler deserializer)
 {
     colExpr.ResultType = typeof(DataColumn);
     return((DataColumn)deserializer.Evaluate(colExpr));
 }
Ejemplo n.º 59
0
        public override object Evaluate(Expression Expression, object existingObject, IDeserializerHandler deserializer)
        {
            Expression.OnObjectConstructed(existingObject);
            Type collectionType = null;

            if (existingObject != null)
            {
                collectionType = existingObject.GetType();
            }
            else
            {
                collectionType = Expression.ResultType;
            }
            Type  itemType    = GetItemType(collectionType);
            Type  wrapperType = typeof(GenericQueueWrapper <>).MakeGenericType(itemType);
            IList wrapper     = (IList)Activator.CreateInstance(wrapperType, existingObject);

            foreach (Expression itemExpr in ((ArrayExpression)Expression).Items)
            {
                itemExpr.ResultType = itemType;
                wrapper.Add(deserializer.Evaluate(itemExpr));
            }
            if (existingObject is IDeserializationCallback)
            {
                ((IDeserializationCallback)existingObject).OnAfterDeserialization();
            }
            return(existingObject);
        }
Ejemplo n.º 60
0
 protected virtual void EvaluateItems(ArrayExpression Expression, object Collection, Type ItemType, IDeserializerHandler deserializer)
 {
     foreach (Expression item in Expression.Items)
     {
         item.ResultType = ItemType;
         object itemResult = deserializer.Evaluate(item);
         AddItem(Collection, itemResult);
     }
 }