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

An exception occurs during Deserialize DateTimeOffset #815

Closed
xknife-erian opened this issue Jun 6, 2023 Discussed in #742 · 4 comments · Fixed by #836
Closed

An exception occurs during Deserialize DateTimeOffset #815

xknife-erian opened this issue Jun 6, 2023 Discussed in #742 · 4 comments · Fixed by #836
Labels

Comments

@xknife-erian
Copy link

Discussed in #742

Originally posted by hcoppen November 14, 2022
Hi,

Is there a way you should serialize and deserialize a DateTimeOffset property? By default it throws an error on deserialize

    [Fact(DisplayName = "DateTimeOffset Test")]
    public void Test2()
    {
        var abc = new Abc() { Name = "XYZ", Time = DateTimeOffset.UtcNow };
        var serializer = new SerializerBuilder()
            .WithNamingConvention(PascalCaseNamingConvention.Instance)
            .Build();
        var deserializer = new DeserializerBuilder()
            .WithNamingConvention(PascalCaseNamingConvention.Instance)
            .Build();
        var yaml = serializer.Serialize(abc);
        var abcNew = deserializer.Deserialize<Abc>(yaml);
        abcNew.Name.Should().Be(abc.Name);
        abcNew.Time.Should().Be(abc.Time);
    }
public class Abc
{
     public string Name { get; set; }
     public DateTimeOffset Time { get; set; }
}

YamlDotNet.Core.YamlException:“Property 'DateTime' not found on type 'System.DateTimeOffset'.”

@q00Dree
Copy link

q00Dree commented Jun 14, 2023

Do you have any progress?

@EdwardCooke
Copy link
Collaborator

I thought I replied to this, we probably need to create a new IYamlTypeConverter to handle the DateTimeOffset object type, just like we did with DateTime.

https://github.com/aaubry/YamlDotNet/blob/master/YamlDotNet/Serialization/Converters/DateTimeConverter.cs

Here's an example of working code, serializes and deserializes. Not the best, since it uses the local culture or whatever, but it's a starting point

using System.Globalization;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;

var serializer = new SerializerBuilder().WithTypeConverter(new DateTimeOffsetConverter()).Build();
var d = new DTO();
var yaml = serializer.Serialize(d);
Console.WriteLine(yaml);
var deserializer = new DeserializerBuilder().WithTypeConverter(new DateTimeOffsetConverter()).Build();
d = deserializer.Deserialize<DTO>(yaml);
Console.WriteLine(d.Offset);

class DTO
{
    public DateTimeOffset Offset { get; set; } = DateTimeOffset.Now;
}

class DateTimeOffsetConverter : IYamlTypeConverter
{
    private readonly bool doubleQuotes;

    public DateTimeOffsetConverter(bool doubleQuotes = false)
    {
        this.doubleQuotes = doubleQuotes;
    }

    public bool Accepts(Type type)
    {
        return type == typeof(DateTimeOffset);
    }

    public object ReadYaml(IParser parser, Type type)
    {
        var value = parser.Consume<Scalar>().Value;
        var result = DateTimeOffset.Parse(value);
        return result;
    }

    public void WriteYaml(IEmitter emitter, object? value, Type type)
    {
        var dt = (DateTimeOffset)value!;
        var formatted = dt.ToString();
        emitter.Emit(new Scalar(AnchorName.Empty, TagName.Empty, formatted, doubleQuotes ? ScalarStyle.DoubleQuoted : ScalarStyle.Any, true, false));
    }
}

Results in

Offset: 6/17/2023 8:17:48 AM -06:00

6/17/2023 8:17:48 AM -06:00

@EdwardCooke EdwardCooke added the bug label Jul 4, 2023
@EdwardCooke
Copy link
Collaborator

It's been a couple of months, did the type converter I posted above work for you? Can I close this issue?

@EdwardCooke
Copy link
Collaborator

I have added a converter into the library itself. You'll need to assign it to the serilizerbuilder and deserializerbuilder in order for it to take effect. There is an example in the samples that show how to do it.

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

Successfully merging a pull request may close this issue.

3 participants