public void WhenDefaultValuesSetOnContext_PropertyInheritsFromContextIfNotSetOnType() { ISerializerSettings config = new SerializerSettings(); ITypeData typeData = config.Types[typeof(SimpleObject)]; typeData.DefaultValueSetting = DefaultValueOption.SuppressDefaultValues; config.DefaultValues[typeof(string)] = "FromType"; typeData.DefaultValues[typeof(int)] = 22; IPropertyData stringProperty = config.Types[typeof(SimpleObject)].FindProperty("StringValue"); Assert.IsFalse(stringProperty.ShouldWriteValue(config, "FromType")); Assert.IsTrue(stringProperty.ShouldWriteValue(config, "")); IPropertyData intProperty = config.Types[typeof(SimpleObject)].FindProperty("IntValue"); Assert.IsFalse(intProperty.ShouldWriteValue(config, 22)); Assert.IsTrue(intProperty.ShouldWriteValue(config, 0)); IPropertyData shortProperty = config.Types[typeof(SimpleObject)].FindProperty("ShortValue"); shortProperty.DefaultValue = (short)9; Assert.IsFalse(shortProperty.ShouldWriteValue(config, (short)9)); Assert.IsTrue(shortProperty.ShouldWriteValue(config, 0)); }
public void DefaultValuesOnPropertyAreConvertedIfNotSameType() { ISerializerSettings config = new SerializerSettings(); ITypeData typeData = config.Types[typeof(MockDefaultValues)]; IPropertyData property = typeData.FindProperty("ConvertedValue"); Assert.IsFalse(property.ShouldWriteValue(config, (short)32)); Assert.IsTrue(property.ShouldWriteValue(config, 0)); }
public void WhenDefaultValuesSetByAttributeOnType_PropertyInheritsIt() { ISerializerSettings config = new SerializerSettings(); ITypeData typeData = config.Types[typeof(MockDefaultValuesCascade)]; IPropertyData property = typeData.FindProperty("EmptyString"); Assert.IsFalse(property.ShouldWriteValue(config, "")); Assert.IsTrue(property.ShouldWriteValue(config, null)); }
public void WhenDefaultValuesSetOnType_PropertyInheritsIt() { ISerializerSettings config = new SerializerSettings(); ITypeData typeData = config.Types[typeof(SimpleObject)]; typeData.DefaultValueSetting = DefaultValueOption.SuppressDefaultValues; typeData.DefaultValues[typeof(string)] = "FromType"; IPropertyData property = config.Types[typeof(SimpleObject)].FindProperty("StringValue"); Assert.IsFalse(property.ShouldWriteValue(config, "FromType")); Assert.IsTrue(property.ShouldWriteValue(config, "")); }
public void WhenSuppressDefaultValuesOnProperty_ValueIsNotDefault_PropertyIsWritten(string propertyName, object value) { Serializer serializer = new Serializer(); ISerializerSettings config = serializer.Settings; IPropertyData property = config.Types[typeof(SimpleObject)].FindProperty(propertyName); property.DefaultValueSetting = DefaultValueOption.SuppressDefaultValues; Assert.IsTrue(property.ShouldWriteValue(config, value)); }
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 void WhenSuppressDefaultValuesOnType_CascadesToProperty() { ISerializerSettings config = new SerializerSettings(); ITypeData typeData = config.Types[typeof(SimpleObject)]; typeData.DefaultValueSetting = DefaultValueOption.SuppressDefaultValues; IPropertyData property = config.Types[typeof(SimpleObject)].FindProperty("IntValue"); Assert.IsFalse(property.ShouldWriteValue(config, 0)); }
public void TestDefaultPropertyAttributes() { ISerializerSettings config = new SerializerSettings(); ITypeData typeData = config.Types.Type <MockDefaultValues>(); IPropertyData intDefault = typeData.FindProperty("IntDefault"); Assert.AreEqual(DefaultValueOption.SuppressDefaultValues, intDefault.DefaultValueSetting, "IntDefault DefaultValueSetting"); Assert.IsFalse(intDefault.ShouldWriteValue(config, 0)); IPropertyData intCustomDefault = typeData.FindProperty("IntCustomDefault"); Assert.AreEqual(DefaultValueOption.SuppressDefaultValues, intCustomDefault.DefaultValueSetting, "IntCustomDefault DefaultValueSetting"); Assert.IsFalse(intCustomDefault.ShouldWriteValue(config, 10)); IPropertyData stringDefaultDisabled = typeData.FindProperty("StringDefaultDisabled"); Assert.AreEqual(DefaultValueOption.WriteAllValues, stringDefaultDisabled.DefaultValueSetting, "StringDefaultDisabled DefaultValueSetting"); Assert.IsTrue(stringDefaultDisabled.ShouldWriteValue(config, null)); }
/// <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); }