Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not deserialize DateTimeOffset in .netcoreapp2.0 #293

Closed
vCipher opened this issue Dec 13, 2017 · 3 comments
Closed

Can not deserialize DateTimeOffset in .netcoreapp2.0 #293

vCipher opened this issue Dec 13, 2017 · 3 comments

Comments

@vCipher
Copy link
Contributor

vCipher commented Dec 13, 2017

Simple example:

class Program
{
    static void Main(string[] args)
    {
        var deserializer = new Deserializer();
        var yaml = "before: 2015-10-23T12:23:15";
        var config = deserializer.Deserialize<Config>(yaml);
    }
}

class Config
{
    [YamlMember(Alias = "before")]
    public DateTimeOffset Before { get; set; }
}

Rises exception:

System.InvalidCastException: Invalid cast from 'System.String' to 'System.DateTimeOffset'.
   at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
   at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at YamlDotNet.Serialization.Utilities.TypeConverter.ChangeType(Object value, Type destinationType, CultureInfo culture)
   at YamlDotNet.Serialization.Utilities.TypeConverter.ChangeType(Object value, Type destinationType)
   at YamlDotNet.Serialization.NodeDeserializers.ScalarNodeDeserializer.YamlDotNet.Serialization.INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func`3 nestedObjectDeserializer, Object& value)
   at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer)

It think that this exception occurs because of getting public static T Parse(string, IFormatProvider) fails in

var parseMethod = destinationType.GetPublicStaticMethod("Parse", typeof(string), typeof(IFormatProvider));

This getting of Parse method fails because of Suspicious comparison: there is no type in the solution which is inherited from both 'System.Reflection.ParameterInfo' and 'System.Type'

&& parameters.Zip(parameterTypes, (pi, pt) => pi.Equals(pt)).All(r => r);

@aaubry
Copy link
Owner

aaubry commented Dec 14, 2017

Humm, this looks like a bug. The code should probably be this instead:

 && parameters.Zip(parameterTypes, (pi.ParameterType, pt) => pi.Equals(pt)).All(r => r); 

We need to add a failing test then fix this. If you want, you can send-me a pull request with that. Otherwise, I can do it myself, but I don't know when I will have time for this.
Thanks

@vCipher
Copy link
Contributor Author

vCipher commented Dec 17, 2017

@aaubry I've created pull request #294

@aaubry
Copy link
Owner

aaubry commented Dec 18, 2017

Thanks. This fix is included in release 4.2.3, which should be visible soon on NuGet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants