public void TestNamedArgumentsNotIgnored() { SerializerSettings context = new SerializerSettings(); ITypeData typeData = new TypeData <NamedOnlyExactConstructor>(context); IPropertyData stringPropA = typeData.FindProperty("StringPropA"); IPropertyData stringPropB = typeData.FindProperty("StringPropB"); Assert.IsFalse(stringPropA.Ignored, "stringPropA Constructor argument should not be ignored"); Assert.IsFalse(stringPropB.Ignored, "stringPropA Constructor argument should not be ignored"); }
public void TestNamedOnlyIgnoreCase() { SerializerSettings context = new SerializerSettings(); TypeData typeData = new TypeData <NamedOnlyIgnoreCaseConstructor>(context); IList <IPropertyData> propData = typeData.ConstructorParameters; List <IPropertyData> expected = new List <IPropertyData>(); expected.Add(typeData.FindProperty("StringPropA")); expected.Add(typeData.FindProperty("StringPropB")); CollectionAssert.AreElementsEqual(expected, propData); }
public void TestParameterAlias() { SerializerSettings context = new SerializerSettings(); ITypeData typeData = new TypeData <AliasedConstructor>(context); List <IPropertyData> expected = new List <IPropertyData>(); expected.Add(typeData.FindProperty("IntProp")); CollectionAssert.AreElementsEqual(expected, typeData.ConstructorParameters); }
/// <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.FindProperty(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"); } }
/// <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); }