public void TypeDataNamingStrategy_AppliedToProperties()
        {
            SerializerSettings config = new SerializerSettings();

            config.Types.PropertyNamingStrategy = new UnderscoreNamingStrategy();
            ITypeData <SimpleObject> td = config.Type <SimpleObject>();
            IPropertyData            pd = td.Property(t => t.ByteValue);

            Assert.AreEqual("Byte_Value", pd.Alias, "ByteValue Alias");
            IPropertyData bvByAlias = td.FindPropertyByAlias("Byte_Value");

            Assert.AreSame(pd, bvByAlias, "ByteValue By Alias");
        }
Пример #2
0
 /// <summary>
 /// Populates the properties of an object from a map
 /// </summary>
 /// <param name="instance">the object to populate, the type must match the one that this instance was created for</param>
 /// <param name="values">a dictionary of values</param>
 /// <param name="ignoreMissingProperties">true to ignore any keys in the dictionary that are not properties on the object.
 /// If false, an exception will be thrown if a property cannot be found.</param>
 public void PopulateFromDictionary(object instance, IDictionary values, bool ignoreMissingProperties)
 {
     if (_handler.ForType.IsInstanceOfType(instance))
     {
         foreach (object key in values.Keys)
         {
             string        stringKey = key.ToString();
             IPropertyData prop      = _handler.FindPropertyByAlias(stringKey);
             if (prop == null && !ignoreMissingProperties)
             {
                 throw new MissingMemberException("Can't find a property for " + stringKey);
             }
             else if (prop != null)
             {
                 prop.SetValue(instance, values[key]);
             }
         }
     }
     else
     {
         throw new ArgumentException("Object instance is of a different type than this class was constructed for");
     }
 }
Пример #3
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));
                }
            }
        }