Merge pull request #3 from roji/ignoreattr

Added YamlIgnore attribute that makes the serializer skip properties
This commit is contained in:
Antoine Aubry 2012-10-17 13:18:19 -07:00
Родитель 703d47f5e3 9050dad8ba
Коммит 77f3e22ccd
4 изменённых файлов: 40 добавлений и 3 удалений

Просмотреть файл

@ -202,7 +202,10 @@ namespace YamlDotNet.RepresentationModel.Serialization
protected virtual bool IsTraversableProperty(PropertyInfo property)
{
return property.CanRead && property.GetGetMethod().GetParameters().Length == 0;
return
property.CanRead &&
property.GetGetMethod().GetParameters().Length == 0 &&
property.GetCustomAttributes(typeof(YamlIgnoreAttribute), true).Length == 0;
}
private static Type GetObjectType(object value)

Просмотреть файл

@ -0,0 +1,13 @@
using System;
namespace YamlDotNet.RepresentationModel.Serialization
{
/// <summary>
/// Instructs the YamlSerializer not to serialize the public field or public read/write property value.
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public sealed class YamlIgnoreAttribute : Attribute
{
}
}

Просмотреть файл

@ -104,6 +104,7 @@
<Compile Include="Serialization\ObjectConverter.cs" />
<Compile Include="Serialization\TagMappings.cs" />
<Compile Include="Serialization\TypeAssigningEventEmitter.cs" />
<Compile Include="Serialization\YamlIgnoreAttribute.cs" />
<Compile Include="YamlAliasNode.cs" />
<Compile Include="YamlDocument.cs" />
<Compile Include="YamlMappingNode.cs" />
@ -157,4 +158,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>

Просмотреть файл

@ -135,7 +135,6 @@ namespace YamlDotNet.UnitTests.RepresentationModel
myPoint = value;
}
}
}
[Fact]
@ -652,5 +651,26 @@ namespace YamlDotNet.UnitTests.RepresentationModel
//{
// var serializer = new YamlSerializer(typeof(X));
//}
class ContainsIgnore {
[YamlIgnore]
public String IgnoreMe { get; set; }
}
[Fact]
public void SerializationRespectsYamlIgnoreAttribute()
{
var serializer = new Serializer();
var deserializer = new YamlSerializer<ContainsIgnore>(YamlSerializerModes.EmitDefaults | YamlSerializerModes.JsonCompatible | YamlSerializerModes.Roundtrip);
using (StringWriter buffer = new StringWriter())
{
var orig = new ContainsIgnore { IgnoreMe = "Some Text" };
serializer.Serialize(buffer, orig);
Console.WriteLine(buffer.ToString());
var copy = deserializer.Deserialize(new StringReader(buffer.ToString()));
Assert.Null(copy.IgnoreMe);
}
}
}
}