diff --git a/YamlDotNet.Configuration/YamlConfigurationProvider.cs b/YamlDotNet.Configuration/YamlConfigurationProvider.cs index bdbb46e..58b963f 100644 --- a/YamlDotNet.Configuration/YamlConfigurationProvider.cs +++ b/YamlDotNet.Configuration/YamlConfigurationProvider.cs @@ -76,8 +76,8 @@ namespace YamlDotNet.Configuration private void LoadSections(TextReader yaml) { - YamlSerializer serializer = new YamlSerializer(typeof(Dictionary)); - sections = (IDictionary)serializer.Deserialize(yaml); + var deserializer = new Deserializer(); + sections = (IDictionary)deserializer.Deserialize(yaml, typeof(Dictionary)); } #region IConfigurationProvider Members diff --git a/YamlDotNet.Configuration/YamlConfigurationSection.cs b/YamlDotNet.Configuration/YamlConfigurationSection.cs index 935ee9c..e903826 100644 --- a/YamlDotNet.Configuration/YamlConfigurationSection.cs +++ b/YamlDotNet.Configuration/YamlConfigurationSection.cs @@ -115,16 +115,14 @@ namespace YamlDotNet.Configuration yaml = GetYamlContent(section); } - YamlSerializer serializer; + var sectionType = typeof(object); if (section.Attributes["type"] != null) { - serializer = new YamlSerializer(Type.GetType(section.Attributes["type"].Value, true)); + sectionType = Type.GetType(section.Attributes["type"].Value, true); } - else - { - serializer = new YamlSerializer(); - } - return serializer.Deserialize(yaml); + + var deserializer = new Deserializer(); + return deserializer.Deserialize(yaml, sectionType); } #endregion diff --git a/YamlDotNet.RepresentationModel/Serialization/Deserializer.cs b/YamlDotNet.RepresentationModel/Serialization/Deserializer.cs index 87e2809..70bfaf9 100644 --- a/YamlDotNet.RepresentationModel/Serialization/Deserializer.cs +++ b/YamlDotNet.RepresentationModel/Serialization/Deserializer.cs @@ -46,7 +46,7 @@ namespace YamlDotNet.RepresentationModel.Serialization private readonly List converters; private TypeDescriptorProxy typeDescriptor = new TypeDescriptorProxy(); - public IList Deserializers { get; private set; } + public IList NodeDeserializers { get; private set; } public IList TypeResolvers { get; private set; } private class TypeDescriptorProxy : ITypeDescriptor @@ -78,17 +78,17 @@ namespace YamlDotNet.RepresentationModel.Serialization ); converters = new List(); - Deserializers = new List(); - Deserializers.Add(new TypeConverterNodeDeserializer(converters)); - Deserializers.Add(new NullNodeDeserializer()); - Deserializers.Add(new ScalarNodeDeserializer()); - Deserializers.Add(new ArrayNodeDeserializer()); - Deserializers.Add(new GenericDictionaryNodeDeserializer(objectFactory)); - Deserializers.Add(new NonGenericDictionaryNodeDeserializer(objectFactory)); - Deserializers.Add(new GenericCollectionNodeDeserializer(objectFactory)); - Deserializers.Add(new NonGenericListNodeDeserializer(objectFactory)); - Deserializers.Add(new EnumerableNodeDeserializer()); - Deserializers.Add(new ObjectNodeDeserializer(objectFactory, typeDescriptor)); + NodeDeserializers = new List(); + NodeDeserializers.Add(new TypeConverterNodeDeserializer(converters)); + NodeDeserializers.Add(new NullNodeDeserializer()); + NodeDeserializers.Add(new ScalarNodeDeserializer()); + NodeDeserializers.Add(new ArrayNodeDeserializer()); + NodeDeserializers.Add(new GenericDictionaryNodeDeserializer(objectFactory)); + NodeDeserializers.Add(new NonGenericDictionaryNodeDeserializer(objectFactory)); + NodeDeserializers.Add(new GenericCollectionNodeDeserializer(objectFactory)); + NodeDeserializers.Add(new NonGenericListNodeDeserializer(objectFactory)); + NodeDeserializers.Add(new EnumerableNodeDeserializer()); + NodeDeserializers.Add(new ObjectNodeDeserializer(objectFactory, typeDescriptor)); tagMappings = new Dictionary(predefinedTagMappings); TypeResolvers = new List(); @@ -99,7 +99,7 @@ namespace YamlDotNet.RepresentationModel.Serialization base.SetValueDeserializer( new AliasValueDeserializer( new NodeValueDeserializer( - Deserializers, + NodeDeserializers, TypeResolvers ) ) diff --git a/YamlDotNet.RepresentationModel/Serialization/NodeDeserializers/ObjectNodeDeserializer.cs b/YamlDotNet.RepresentationModel/Serialization/NodeDeserializers/ObjectNodeDeserializer.cs index 79b20c5..c5e1706 100644 --- a/YamlDotNet.RepresentationModel/Serialization/NodeDeserializers/ObjectNodeDeserializer.cs +++ b/YamlDotNet.RepresentationModel/Serialization/NodeDeserializers/ObjectNodeDeserializer.cs @@ -56,7 +56,8 @@ namespace YamlDotNet.RepresentationModel.Serialization.NodeDeserializers var property = _typeDescriptor.GetProperty(expectedType, propertyName.Value).Property; var propertyValue = nestedObjectDeserializer(reader, property.PropertyType); - property.SetValue(value, propertyValue, null); + var convertedValue = TypeConverter.ChangeType(propertyValue, property.PropertyType); + property.SetValue(value, convertedValue, null); } reader.Expect(); diff --git a/YamlDotNet.RepresentationModel/Serialization/NodeDeserializers/ScalarNodeDeserializer.cs b/YamlDotNet.RepresentationModel/Serialization/NodeDeserializers/ScalarNodeDeserializer.cs index cf300ac..640ddd4 100644 --- a/YamlDotNet.RepresentationModel/Serialization/NodeDeserializers/ScalarNodeDeserializer.cs +++ b/YamlDotNet.RepresentationModel/Serialization/NodeDeserializers/ScalarNodeDeserializer.cs @@ -116,15 +116,7 @@ namespace YamlDotNet.RepresentationModel.Serialization.NodeDeserializers } else { - TypeConverter converter = TypeDescriptor.GetConverter(expectedType); - if (converter != null && converter.CanConvertFrom(typeof(string))) - { - value = converter.ConvertFromInvariantString(scalar.Value); - } - else - { - value = Convert.ChangeType(scalar.Value, expectedType, CultureInfo.InvariantCulture); - } + value = TypeConverter.ChangeType(scalar.Value, expectedType); } break; } diff --git a/YamlDotNet.RepresentationModel/Serialization/ObjectConverter.cs b/YamlDotNet.RepresentationModel/Serialization/ObjectConverter.cs deleted file mode 100644 index 77b6841..0000000 --- a/YamlDotNet.RepresentationModel/Serialization/ObjectConverter.cs +++ /dev/null @@ -1,101 +0,0 @@ -// This file is part of YamlDotNet - A .NET library for YAML. -// Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Antoine Aubry - -// Permission is hereby granted, free of charge, to any person obtaining a copy of -// this software and associated documentation files (the "Software"), to deal in -// the Software without restriction, including without limitation the rights to -// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies -// of the Software, and to permit persons to whom the Software is furnished to do -// so, subject to the following conditions: - -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. - -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// 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.ComponentModel; -using System.Globalization; - -namespace YamlDotNet.RepresentationModel.Serialization -{ - /// - /// Performs type conversions. - /// - public static class ObjectConverter - { - /// - /// Attempts to convert the specified value to another type using various approaches. - /// - /// The value. - /// - /// - /// The conversion is first attempted by ditect casting. - /// If it fails, the it attempts to use the interface. - /// If it still fails, it tries to use the of the value's type, and then the of the type. - /// - /// is null. - /// The value cannot be converted to the specified type. - public static TTo Convert(TFrom value) - { - return (TTo)Convert(value, typeof(TTo)); - } - - /// - /// Attempts to convert the specified value to another type using various approaches. - /// - /// The value. - /// To. - /// - /// - /// The conversion is first attempted by ditect casting. - /// If it fails, it tries to use the of the value's type, and then the of the type. - /// If it still fails, the it attempts to use the interface. - /// - /// Either or are null. - /// The value cannot be converted to the specified type. - public static object Convert(object value, Type to) - { - if (value == null) - { - throw new ArgumentNullException("value"); - } - - if (to == null) - { - throw new ArgumentNullException("to"); - } - - Type from = value.GetType(); - if (from == to || to.IsAssignableFrom(from)) - { - return value; - } - - TypeConverter resultTypeConverter = TypeDescriptor.GetConverter(from); - if (resultTypeConverter != null && resultTypeConverter.CanConvertTo(to)) - { - return resultTypeConverter.ConvertTo(value, to); - } - - TypeConverter expectedTypeConverter = TypeDescriptor.GetConverter(to); - if (expectedTypeConverter != null && expectedTypeConverter.CanConvertFrom(from)) - { - return expectedTypeConverter.ConvertFrom(value); - } - - if (typeof(IConvertible).IsAssignableFrom(from)) - { - return System.Convert.ChangeType(value, to, CultureInfo.InvariantCulture); - } - - throw new InvalidCastException(string.Format(CultureInfo.InvariantCulture, "Cannot convert from type '{0}' to type '{1}'.", from.FullName, to.FullName)); - } - } -} \ No newline at end of file diff --git a/YamlDotNet.RepresentationModel/Serialization/ReflectionUtility.cs b/YamlDotNet.RepresentationModel/Serialization/ReflectionUtility.cs index db920d6..3faad8e 100644 --- a/YamlDotNet.RepresentationModel/Serialization/ReflectionUtility.cs +++ b/YamlDotNet.RepresentationModel/Serialization/ReflectionUtility.cs @@ -1,3 +1,24 @@ +// This file is part of YamlDotNet - A .NET library for YAML. +// Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Antoine Aubry + +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +// of the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// 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.Generic; using System.Linq.Expressions; diff --git a/YamlDotNet.RepresentationModel/Serialization/TypeConverter.cs b/YamlDotNet.RepresentationModel/Serialization/TypeConverter.cs new file mode 100644 index 0000000..b1ba824 --- /dev/null +++ b/YamlDotNet.RepresentationModel/Serialization/TypeConverter.cs @@ -0,0 +1,324 @@ +// This file is part of YamlDotNet - A .NET library for YAML. +// Copyright (c) 2011, 2012, 2013 Antoine Aubry + +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +// of the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// 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. + +// Remarks: This file is imported from the SixPack library. This is ok because +// the copyright holder has agreed to redistribute this file under the license +// used in YamlDotNet. + +using System; +using System.ComponentModel; +using System.Globalization; +using System.Linq; +using System.Reflection; + +namespace YamlDotNet.RepresentationModel.Serialization +{ + /// + /// Performs type conversions using every standard provided by the .NET library. + /// + public static class TypeConverter + { + /// + /// Registers a dynamically. + /// + /// The type to which the coverter should be associated. + /// The type of the converter. + public static void RegisterTypeConverter() + where TConverter : System.ComponentModel.TypeConverter + { + var alreadyRegistered = TypeDescriptor.GetAttributes(typeof(TConvertible)) + .OfType() + .Any(a => a.ConverterTypeName == typeof(TConverter).AssemblyQualifiedName); + + if (!alreadyRegistered) + { + TypeDescriptor.AddAttributes(typeof(TConvertible), new TypeConverterAttribute(typeof(TConverter))); + } + } + + /// + /// Converts the specified value. + /// + /// The type to which the value is to be converted. + /// The value to convert. + /// + public static T ChangeType(object value) + { + return (T)ChangeType(value, typeof(T)); + } + + /// + /// Converts the specified value. + /// + /// The type to which the value is to be converted. + /// The value to convert. + /// The provider. + /// + public static T ChangeType(object value, IFormatProvider provider) + { + return (T)ChangeType(value, typeof(T), provider); + } + + /// + /// Converts the specified value. + /// + /// The type to which the value is to be converted. + /// The value to convert. + /// The culture. + /// + public static T ChangeType(object value, CultureInfo culture) + { + return (T)ChangeType(value, typeof(T), culture); + } + + /// + /// Converts the specified value using the invariant culture. + /// + /// The value to convert. + /// The type to which the value is to be converted. + /// + public static object ChangeType(object value, Type destinationType) + { + return ChangeType(value, destinationType, CultureInfo.InvariantCulture); + } + + /// + /// Converts the specified value. + /// + /// The value to convert. + /// The type to which the value is to be converted. + /// The format provider. + /// + public static object ChangeType(object value, Type destinationType, IFormatProvider provider) + { + return ChangeType(value, destinationType, new CultureInfoAdapter(CultureInfo.CurrentCulture, provider)); + } + + /// + /// Converts the specified value. + /// + /// The value to convert. + /// The type to which the value is to be converted. + /// The culture. + /// + public static object ChangeType(object value, Type destinationType, CultureInfo culture) + { + // Handle null and DBNull + if (value == null || value is DBNull) + { + return destinationType.IsValueType ? Activator.CreateInstance(destinationType) : null; + } + + var sourceType = value.GetType(); + + // If the source type is compatible with the destination type, no conversion is needed + if (destinationType.IsAssignableFrom(sourceType)) + { + return value; + } + + // Nullable types get a special treatment + if (destinationType.IsGenericType) + { + var genericTypeDefinition = destinationType.GetGenericTypeDefinition(); + if (genericTypeDefinition == typeof(Nullable<>)) + { + var innerType = destinationType.GetGenericArguments()[0]; + var convertedValue = ChangeType(value, innerType, culture); + return Activator.CreateInstance(destinationType, convertedValue); + } + } + + // Enums also require special handling + if (destinationType.IsEnum) + { + var valueText = value as string; + return valueText != null ? Enum.Parse(destinationType, valueText, true) : value; + } + + // Special case for booleans to support parsing "1" and "0". This is + // necessary for compatibility with XML Schema. + if (destinationType == typeof(bool)) + { + if ("0".Equals(value)) + return false; + + if ("1".Equals(value)) + return true; + } + + // Try with the source type's converter + var sourceConverter = TypeDescriptor.GetConverter(value); + if (sourceConverter != null && sourceConverter.CanConvertTo(destinationType)) + { + return sourceConverter.ConvertTo(null, culture, value, destinationType); + } + + // Try with the destination type's converter + var destinationConverter = TypeDescriptor.GetConverter(destinationType); + if (destinationConverter != null && destinationConverter.CanConvertFrom(sourceType)) + { + return destinationConverter.ConvertFrom(null, culture, value); + } + + // Try to find a casting operator in the source or destination type + foreach (var type in new[] { sourceType, destinationType }) + { + foreach (var method in type.GetMethods(BindingFlags.Static | BindingFlags.Public)) + { + var isCastingOperator = + method.IsSpecialName && + (method.Name == "op_Implicit" || method.Name == "op_Explicit") && + destinationType.IsAssignableFrom(method.ReturnParameter.ParameterType); + + if (isCastingOperator) + { + var parameters = method.GetParameters(); + + var isCompatible = + parameters.Length == 1 && + parameters[0].ParameterType.IsAssignableFrom(sourceType); + + if (isCompatible) + { + return method.Invoke(null, new[] { value }); + } + } + } + } + + // If source type is string, try to find a Parse or TryParse method + if (sourceType == typeof(string)) + { + // Try with - public static T Parse(string, IFormatProvider) + var parseMethod = destinationType.GetMethod("Parse", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(string), typeof(IFormatProvider) }, null); + if (parseMethod != null) + { + return parseMethod.Invoke(null, new object[] { value, culture }); + } + + // Try with - public static T Parse(string) + parseMethod = destinationType.GetMethod("Parse", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(string) }, null); + if (parseMethod != null) + { + return parseMethod.Invoke(null, new object[] { value }); + } + } + + // Handle TimeSpan + if (destinationType == typeof(TimeSpan)) + { + return TimeSpan.Parse((string)ChangeType(value, typeof(string), CultureInfo.InvariantCulture)); + } + + // Default to the Convert class + return Convert.ChangeType(value, destinationType, CultureInfo.InvariantCulture); + } + + private class CultureInfoAdapter : CultureInfo + { + private readonly IFormatProvider _provider; + + public CultureInfoAdapter(CultureInfo baseCulture, IFormatProvider provider) + : base(baseCulture.LCID) + { + _provider = provider; + } + + public override object GetFormat(Type formatType) + { + return _provider.GetFormat(formatType); + } + } + + /// + /// Tries to parse the specified value. + /// + /// + /// The value. + /// + public static T? TryParse(string value) where T : struct + { + switch (Type.GetTypeCode(typeof(T))) + { + case TypeCode.Boolean: + return (T?)(object)TryParse(value, bool.TryParse); + + case TypeCode.Byte: + return (T?)(object)TryParse(value, byte.TryParse); + + case TypeCode.DateTime: + return (T?)(object)TryParse(value, DateTime.TryParse); + + case TypeCode.Decimal: + return (T?)(object)TryParse(value, decimal.TryParse); + + case TypeCode.Double: + return (T?)(object)TryParse(value, double.TryParse); + + case TypeCode.Int16: + return (T?)(object)TryParse(value, short.TryParse); + + case TypeCode.Int32: + return (T?)(object)TryParse(value, int.TryParse); + + case TypeCode.Int64: + return (T?)(object)TryParse(value, long.TryParse); + + case TypeCode.SByte: + return (T?)(object)TryParse(value, sbyte.TryParse); + + case TypeCode.Single: + return (T?)(object)TryParse(value, float.TryParse); + + case TypeCode.UInt16: + return (T?)(object)TryParse(value, ushort.TryParse); + + case TypeCode.UInt32: + return (T?)(object)TryParse(value, uint.TryParse); + + case TypeCode.UInt64: + return (T?)(object)TryParse(value, ulong.TryParse); + + default: + throw new NotSupportedException(string.Format("Cannot parse type '{0}'.", typeof(T).FullName)); + } + } + + /// + /// Tries to parse the specified value. + /// + /// + /// The value to be parsed. + /// The parse function. + /// + public static T? TryParse(string value, TryParseDelegate parse) where T : struct + { + T result; + return parse(value, out result) ? (T?)result : null; + } + + /// + /// Defines a method that is used to tentatively parse a string. + /// + public delegate bool TryParseDelegate(string value, out T result); + } +} \ No newline at end of file diff --git a/YamlDotNet.RepresentationModel/Serialization/YamlAliasAttribute.cs b/YamlDotNet.RepresentationModel/Serialization/YamlAliasAttribute.cs index a4f7f23..493c18a 100644 --- a/YamlDotNet.RepresentationModel/Serialization/YamlAliasAttribute.cs +++ b/YamlDotNet.RepresentationModel/Serialization/YamlAliasAttribute.cs @@ -3,7 +3,7 @@ namespace YamlDotNet.RepresentationModel.Serialization { /// - /// Instructs the to use a different field name for serialization. + /// Instructs the to use a different field name for serialization. /// [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)] public class YamlAliasAttribute : Attribute diff --git a/YamlDotNet.RepresentationModel/Serialization/YamlSerializer.cs b/YamlDotNet.RepresentationModel/Serialization/YamlSerializer.cs index 366ff6e..9ae948a 100644 --- a/YamlDotNet.RepresentationModel/Serialization/YamlSerializer.cs +++ b/YamlDotNet.RepresentationModel/Serialization/YamlSerializer.cs @@ -29,7 +29,6 @@ using System.IO; using System.Linq; using System.Reflection; using System.Runtime.Serialization; -using System.Text.RegularExpressions; using YamlDotNet.Core; using YamlDotNet.Core.Events; @@ -39,6 +38,7 @@ namespace YamlDotNet.RepresentationModel.Serialization /// /// Reads and writes objects from and to YAML. /// + [Obsolete("YamlSerializer has been replaced by Deserializer. This class will be removed in the next version.")] public class YamlSerializer { private readonly YamlSerializerModes mode; @@ -330,7 +330,7 @@ namespace YamlDotNet.RepresentationModel.Serialization } object result = DeserializeValueNotNull(reader, context, nodeEvent, expectedType); - return ObjectConverter.Convert(result, expectedType); + return TypeConverter.ChangeType(result, expectedType); } private bool IsNull(NodeEvent nodeEvent) @@ -468,7 +468,7 @@ namespace YamlDotNet.RepresentationModel.Serialization } else { - TypeConverter converter = TypeDescriptor.GetConverter(type); + var converter = TypeDescriptor.GetConverter(type); if (converter != null && converter.CanConvertFrom(typeof(string))) { result = converter.ConvertFromInvariantString(scalar.Value); @@ -790,6 +790,7 @@ namespace YamlDotNet.RepresentationModel.Serialization /// on the user's code. /// /// The type of the serialized. + [Obsolete("YamlSerializer has been replaced by Deserializer. This class will be removed in the next version.")] public class YamlSerializer : YamlSerializer { /// diff --git a/YamlDotNet.RepresentationModel/YamlDotNet.RepresentationModel.csproj b/YamlDotNet.RepresentationModel/YamlDotNet.RepresentationModel.csproj index e670c07..880f173 100644 --- a/YamlDotNet.RepresentationModel/YamlDotNet.RepresentationModel.csproj +++ b/YamlDotNet.RepresentationModel/YamlDotNet.RepresentationModel.csproj @@ -1,4 +1,4 @@ - + Debug @@ -102,6 +102,7 @@ + @@ -115,7 +116,6 @@ - @@ -200,9 +200,5 @@ --> - - - - - + \ No newline at end of file diff --git a/YamlDotNet.UnitTests/RepresentationModel/ObjectConverterTests.cs b/YamlDotNet.UnitTests/RepresentationModel/ObjectConverterTests.cs deleted file mode 100644 index 26e3862..0000000 --- a/YamlDotNet.UnitTests/RepresentationModel/ObjectConverterTests.cs +++ /dev/null @@ -1,38 +0,0 @@ -// This file is part of YamlDotNet - A .NET library for YAML. -// Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Antoine Aubry - -// Permission is hereby granted, free of charge, to any person obtaining a copy of -// this software and associated documentation files (the "Software"), to deal in -// the Software without restriction, including without limitation the rights to -// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies -// of the Software, and to permit persons to whom the Software is furnished to do -// so, subject to the following conditions: - -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. - -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// 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 Xunit; -using System.Drawing; -using YamlDotNet.RepresentationModel.Serialization; - -namespace YamlDotNet.UnitTests.RepresentationModel -{ - public class ObjectConverterTests - { - [Fact] - public void StringToColor() - { - Color color = ObjectConverter.Convert("white"); - Assert.Equal(unchecked((int)0xFFFFFFFF), color.ToArgb()); - } - } -} \ No newline at end of file diff --git a/YamlDotNet.UnitTests/RepresentationModel/ObjectFactoryTests.cs b/YamlDotNet.UnitTests/RepresentationModel/ObjectFactoryTests.cs index 11baff1..68b0d2b 100644 --- a/YamlDotNet.UnitTests/RepresentationModel/ObjectFactoryTests.cs +++ b/YamlDotNet.UnitTests/RepresentationModel/ObjectFactoryTests.cs @@ -21,10 +21,9 @@ namespace YamlDotNet.UnitTests.RepresentationModel [Fact] public void NotSpecifyingObjectFactoryUsesDefault() { - var serializer = new YamlSerializer(); - var options = new DeserializationOptions(); - options.Mappings.Add("!foo", typeof(FooBase)); - var result = serializer.Deserialize(new StringReader("!foo {}"), options); + var deserializer = new Deserializer(); + deserializer.RegisterTagMapping("!foo", typeof(FooBase)); + var result = deserializer.Deserialize(new StringReader("!foo {}")); Assert.IsType(result); } @@ -32,13 +31,10 @@ namespace YamlDotNet.UnitTests.RepresentationModel [Fact] public void ObjectFactoryIsInvoked() { - var serializer = new YamlSerializer(); - var options = new DeserializationOptions(); - options.Mappings.Add("!foo", typeof(FooBase)); + var deserializer = new Deserializer(new LambdaObjectFactory(t => new FooDerived())); + deserializer.RegisterTagMapping("!foo", typeof(FooBase)); - options.ObjectFactory = new LambdaObjectFactory(t => new FooDerived()); - - var result = serializer.Deserialize(new StringReader("!foo {}"), options); + var result = deserializer.Deserialize(new StringReader("!foo {}")); Assert.IsType(result); } diff --git a/YamlDotNet.UnitTests/RepresentationModel/SerializationTests.cs b/YamlDotNet.UnitTests/RepresentationModel/SerializationTests.cs index cc891aa..5685d48 100644 --- a/YamlDotNet.UnitTests/RepresentationModel/SerializationTests.cs +++ b/YamlDotNet.UnitTests/RepresentationModel/SerializationTests.cs @@ -459,23 +459,28 @@ namespace YamlDotNet.UnitTests.RepresentationModel Point value = (Point)result; Assert.Equal(10, value.X); Assert.Equal(20, value.Y); + } + + [Fact] + public void DeserializeConvertible() + { + var serializer = new Deserializer(); + object result = serializer.Deserialize(YamlFile("convertible.yaml"), typeof(Z)); + + Assert.True(typeof(Z).IsAssignableFrom(result.GetType())); + Assert.Equal("[hello, world]", ((Z)result).aaa); } - //[Fact] - //public void DeserializeConvertible() - //{ - // YamlSerializer serializer = new YamlSerializer(); - // object result = serializer.Deserialize(YamlFile("convertible.yaml")); - - // Assert.True(typeof(Z).IsAssignableFrom(result.GetType())); - // Assert.Equal("[hello, world]", ((Z)result).aaa, "The property has the wrong value."); - //} - - public class Converter : TypeConverter + public class Converter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return sourceType == typeof(string); + } + + public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) + { + return false; } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) @@ -765,13 +770,7 @@ namespace YamlDotNet.UnitTests.RepresentationModel Assert.Null(copy.MyString); } - } - - //[Fact] - //public void DeserializationIgnoresUnknownProperties() - //{ - // var serializer = new YamlSerializer(typeof(X)); - //} + } class ContainsIgnore { diff --git a/YamlDotNet.UnitTests/YamlDotNet.UnitTests.csproj b/YamlDotNet.UnitTests/YamlDotNet.UnitTests.csproj index af3c9e8..353603d 100644 --- a/YamlDotNet.UnitTests/YamlDotNet.UnitTests.csproj +++ b/YamlDotNet.UnitTests/YamlDotNet.UnitTests.csproj @@ -1,4 +1,4 @@ - + Debug @@ -54,7 +54,6 @@ -