Coder Social home page Coder Social logo

Comments (4)

q00Dree avatar q00Dree commented on June 11, 2024

Do you have any progress?

from yamldotnet.

EdwardCooke avatar EdwardCooke commented on June 11, 2024

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

from yamldotnet.

EdwardCooke avatar EdwardCooke commented on June 11, 2024

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

from yamldotnet.

EdwardCooke avatar EdwardCooke commented on June 11, 2024

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.

from yamldotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.