зеркало из https://github.com/stride3d/SharpYaml.git
Merge pull request #3 from roji/ignoreattr
Added YamlIgnore attribute that makes the serializer skip properties
This commit is contained in:
Коммит
77f3e22ccd
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче