/// <summary>
 /// Add a property to this object
 /// </summary>
 /// <param name="expression">the key value expression to add</param>
 /// <returns>KeyValueExpression that was added</returns>
 public KeyValueExpression Add(KeyValueExpression expression)
 {
     expression.Parent = this;
     expression.ValueExpression.Parent = this;
     Properties.Add(expression);
     return expression;
 }
 public void VisitKeyValue(KeyValueExpression keyValue)
 {
     if (_refID.Top == keyValue.Key)
     {
         Visit(keyValue.ValueExpression);
     }
     else
     {
         throw new Exception("Unable to resolve reference: " + _refID);
     }
 }
예제 #3
0
 /// <summary>
 /// Finds the index of the given key in the property list
 /// </summary>
 /// <param name="key">the key to find</param>
 /// <returns>0-based index of the key/value property in the list or -1 if not found</returns>
 public int IndexOf(string key)
 {
     for (int i = 0; i < Properties.Count; i++)
     {
         KeyValueExpression keyValue = Properties[i];
         if (keyValue.KeyExpression is ValueExpression && keyValue.Key == key)
         {
             return(i);
         }
     }
     return(-1);
 }
 public virtual void Visit(KeyValueExpression expression)
 {
     expression.KeyExpression.Accept(this);
     expression.ValueExpression.Accept(this);
 }
        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));
            }
        }
 public void Visit(KeyValueExpression KeyValue)
 {
     Visit(KeyValue.ValueExpression);
 }