Coder Social home page Coder Social logo

Comments (2)

EdwardCooke avatar EdwardCooke commented on June 12, 2024

The reason the current format was used is to match the current DateTimeFormatter output of yyyy/MM/dd hh:mm:ss Which in theory would also fail the date time format of that json spec. However, from what I'm seeing, the JSON spec itself doesn't specify what the format of a date/time is supposed to be. It's up to the json parsers as to what to support. I'm a little hesitant to change what the current format is of the date/time because of how many people are using this library and consistency between the different date/time objects is a good thing. That being said, there is a strong case to be made for changing the format for JsonCompatible methods to match what most parsers are expecting for dates and times.

What you could do in the meantime is specify the patterns to use if you want to do something other than what we have currently. You would want to do something similar to this for the DateTimeConverter and potentially the TimeOnlyConverter if needed.

For example:

using YamlDotNet.Serialization;
using YamlDotNet.Serialization.Converters;

var serializer = new SerializerBuilder()
    .JsonCompatible()
    .WithoutTypeConverter<DateOnlyConverter>()
    .WithTypeConverter(new DateOnlyConverter(formats: new[] { "yyyy-MM-dd" }, doubleQuotes: true))
    .Build();

var o = new O();
Console.WriteLine(serializer.Serialize(o));

class O
{
    public DateOnly MyDate { get; set; } = new DateOnly(2024, 1, 16);
}

Produces:

{"MyDate": "2024-01-16"}

from yamldotnet.

pm64 avatar pm64 commented on June 12, 2024

@EdwardCooke, thank you for this guidance, it is greatly appreciated.

I agree that changing the format could cause disruption, and would definitely save this for a major version bump.

But if you do reconsider the date format for JsonCompatible methods at that time, I think the JSON Schema spec provides a solid framework.

For reference, here's how the spec would have you format CLR types DateTime, TimeOnly, DateOnly, and TimeSpan (from https://json-schema.org/understanding-json-schema/reference/string):

Dates and times are represented in RFC 3339, section 5.6. This is a subset of the date format also commonly known as ISO8601 format.

DateTime: 2018-11-13T20:20:39+00:00
TimeOnly: 20:20:39+00:00
DateOnly: 2018-11-13
TimeSpan: A duration as defined by the ISO 8601 ABNF for "duration". For example, P3D expresses a duration of 3 days.

I think this would minimize the number of developers needing to override the default formats.

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.