зеркало из https://github.com/stride3d/SharpYaml.git
Merge pull request #27 : Respect YamlIgnore on deserialization
This commit is contained in:
Родитель
38685a8fce
Коммит
2a53997ddd
|
@ -32,22 +32,40 @@ namespace YamlDotNet.RepresentationModel.Serialization
|
|||
|
||||
public IPropertyDescriptor GetProperty(Type type, string name)
|
||||
{
|
||||
var property = GetProperties(type)
|
||||
.FirstOrDefault(p => p.Name == name);
|
||||
var candidates = GetProperties(type)
|
||||
.Where(p => p.Name == name);
|
||||
|
||||
if(property == null)
|
||||
using(var enumerator = candidates.GetEnumerator())
|
||||
{
|
||||
throw new SerializationException(
|
||||
string.Format(
|
||||
CultureInfo.InvariantCulture,
|
||||
"Property '{0}' not found on type '{1}'.",
|
||||
name,
|
||||
type.FullName
|
||||
)
|
||||
);
|
||||
if(!enumerator.MoveNext())
|
||||
{
|
||||
throw new SerializationException(
|
||||
string.Format(
|
||||
CultureInfo.InvariantCulture,
|
||||
"Property '{0}' not found on type '{1}'.",
|
||||
name,
|
||||
type.FullName
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
var property = enumerator.Current;
|
||||
|
||||
if(enumerator.MoveNext())
|
||||
{
|
||||
throw new SerializationException(
|
||||
string.Format(
|
||||
CultureInfo.InvariantCulture,
|
||||
"Multiple properties with the name/alias '{0}' already exists on type '{1}', maybe you're misusing YamlAlias or maybe you are using the wrong naming convention? The matching properties are: {2}",
|
||||
name,
|
||||
type.FullName,
|
||||
string.Join(", ", candidates.Select(p => p.Property.Name).ToArray())
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return property;
|
||||
}
|
||||
|
||||
return property;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -758,15 +758,18 @@ namespace YamlDotNet.RepresentationModel.Serialization
|
|||
/// <returns>Aliases for this type</returns>
|
||||
private IDictionary<string, PropertyInfo> GetPropertyAliases(Type type)
|
||||
{
|
||||
IDictionary<string, PropertyInfo> mapping;
|
||||
|
||||
// Check if it's already cached
|
||||
if (propertyAliases.ContainsKey(type))
|
||||
{
|
||||
return propertyAliases[type];
|
||||
}
|
||||
if (propertyAliases.TryGetValue(type, out mapping))
|
||||
return mapping;
|
||||
|
||||
var mapping = new Dictionary<string, PropertyInfo>();
|
||||
mapping = new Dictionary<string, PropertyInfo>();
|
||||
foreach (var property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
|
||||
{
|
||||
if (property.GetCustomAttributes(typeof (YamlIgnoreAttribute), true).Length > 0)
|
||||
continue;
|
||||
|
||||
// Default to the property name
|
||||
var alias = property.Name;
|
||||
|
||||
|
@ -775,6 +778,9 @@ namespace YamlDotNet.RepresentationModel.Serialization
|
|||
if (aliasProps.Length != 0)
|
||||
alias = ((YamlAliasAttribute)aliasProps[0]).Alias;
|
||||
|
||||
if (mapping.ContainsKey(alias))
|
||||
throw new Exception(String.Format("A property with the name/alias {0} already exists, maybe you're misusing YamlAlias?", alias));
|
||||
|
||||
mapping.Add(alias, property);
|
||||
}
|
||||
|
||||
|
|
|
@ -17,24 +17,24 @@
|
|||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Drawing;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
using Xunit;
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.RepresentationModel.Serialization;
|
||||
using YamlDotNet.RepresentationModel.Serialization.NamingConventions;
|
||||
|
||||
// SOFTWARE.
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Drawing;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
using Xunit;
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.RepresentationModel.Serialization;
|
||||
using YamlDotNet.RepresentationModel.Serialization.NamingConventions;
|
||||
|
||||
namespace YamlDotNet.UnitTests.RepresentationModel
|
||||
{
|
||||
public class SerializationTests : YamlTest
|
||||
|
@ -876,6 +876,8 @@ namespace YamlDotNet.UnitTests.RepresentationModel
|
|||
public string ThirdTest { get; set; }
|
||||
[YamlAlias("fourthTest")]
|
||||
public string AliasTest { get; set; }
|
||||
[YamlIgnore]
|
||||
public string fourthTest { get; set; }
|
||||
}
|
||||
|
||||
private void DeserializeUsingNamingConvention(string yaml, INamingConvention convention)
|
||||
|
@ -1023,8 +1025,8 @@ namespace YamlDotNet.UnitTests.RepresentationModel
|
|||
[Fact]
|
||||
public void NullValuesInListsAreAlwaysEmittedWithEmitDefaults()
|
||||
{
|
||||
var input = new string[] { "foo", null, "bar" };
|
||||
|
||||
var input = new string[] { "foo", null, "bar" };
|
||||
|
||||
var serializer = new Serializer(SerializationOptions.EmitDefaults);
|
||||
var writer = new StringWriter();
|
||||
serializer.Serialize(writer, input);
|
||||
|
@ -1094,3 +1096,4 @@ Name: Charles
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче