public void WhenWriteAllValuesOnProperty_ValueIsDefault_PropertyWritten(string propertyName) { Serializer serializer = new Serializer(); ISerializerSettings config = serializer.Settings; IPropertyData property = config.Types[typeof(SimpleObject)].FindProperty(propertyName); SimpleObject testObject = new SimpleObject(); property.DefaultValueSetting = DefaultValueOption.WriteAllValues; Assert.IsTrue(property.ShouldWriteValue(config, property.GetValue(testObject))); }
public static T TryGetPropertyValue <T>(this IFunctionData data, string propertyName, T defaultValue) { IPropertyData property = data.Properties.Values.FirstOrDefault(p => p.Name == propertyName); if (property != null) { if (property.ValueUsage != ValueUseOption.DesignTime) { throw new NotSupportedException("Cannot run GetValue on a dynamic value."); } return(property.GetValue <T>()); } return(defaultValue); }
public override object ConvertTo(object item, Type sourceType, ISerializerSettings serializationContext) { IDictionary dictionary = (IDictionary)Activator.CreateInstance(sourceType); ICollection coll = (ICollection)item; foreach (object colItem in coll) { IPropertyData propHandler = serializationContext.Types[colItem.GetType()].FindProperty(Context.ToString()); if (propHandler == null) { throw new MissingMemberException("Type: " + item.GetType().Name + " does not have an accessible property: " + Context); } dictionary[propHandler.GetValue(colItem)] = colItem; } return(dictionary); }
/// <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); }
/// <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 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)); } } }
/// <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); }