зеркало из https://github.com/stride3d/SharpYaml.git
Use YamlAliasAttribute value when serializing.
This commit is contained in:
Родитель
4385fa5e2d
Коммит
c6a340b880
|
@ -180,10 +180,11 @@ namespace YamlDotNet.RepresentationModel.Serialization
|
|||
{
|
||||
var propertyValue = property.GetValue(value, null);
|
||||
var propertyType = property.PropertyType;
|
||||
var propertyName = GetPropertyName(type, property);
|
||||
|
||||
if(visitor.EnterMapping(property.Name, typeof(string), propertyValue, propertyType))
|
||||
if(visitor.EnterMapping(propertyName, typeof(string), propertyValue, propertyType))
|
||||
{
|
||||
Traverse(property.Name, typeof(string), visitor, currentDepth);
|
||||
Traverse(propertyName, typeof(string), visitor, currentDepth);
|
||||
Traverse(propertyValue, propertyType, visitor, currentDepth);
|
||||
}
|
||||
}
|
||||
|
@ -206,6 +207,12 @@ namespace YamlDotNet.RepresentationModel.Serialization
|
|||
property.GetCustomAttributes(typeof(YamlIgnoreAttribute), true).Length == 0;
|
||||
}
|
||||
|
||||
protected string GetPropertyName(Type type, PropertyInfo property)
|
||||
{
|
||||
var aliasProps = property.GetCustomAttributes(typeof(YamlAliasAttribute), true);
|
||||
return aliasProps.Length == 0 ? property.Name : ((YamlAliasAttribute)aliasProps[0]).Alias;
|
||||
}
|
||||
|
||||
private static Type GetObjectType(object value)
|
||||
{
|
||||
return value != null ? value.GetType() : typeof(object);
|
||||
|
|
|
@ -723,5 +723,24 @@ namespace YamlDotNet.UnitTests.RepresentationModel
|
|||
Assert.Equal("Third", result.ThirdTest);
|
||||
Assert.Equal("Fourth", result.AliasTest);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RoundtripAlias()
|
||||
{
|
||||
var input = new ConventionTest { AliasTest = "Fourth" };
|
||||
var serializer = new Serializer();
|
||||
var writer = new StringWriter();
|
||||
serializer.Serialize(writer, input, input.GetType());
|
||||
string serialized = writer.ToString();
|
||||
|
||||
// Ensure serialisation is correct
|
||||
Assert.Equal("fourthTest: Fourth", serialized);
|
||||
|
||||
var deserializer = new YamlSerializer<ConventionTest>();
|
||||
var output = deserializer.Deserialize(new StringReader(serialized));
|
||||
|
||||
// Ensure round-trip retains value
|
||||
Assert.Equal(input.AliasTest, output.AliasTest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче