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
Invalid date format used in serialized JSON #891
Comments
The reason the current format was used is to match the current 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 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:
|
@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 I think this would minimize the number of developers needing to override the default formats. |
Describe the bug
Imagine the following YAML..
.. is deserialized into an object defined as follows:
With an instance of the above instantiated as "myExample", we convert to JSON as follows:
Result:
The above result is invalid because the date is formatted incorrectly.
The resultant JSON should pass validation against the following JSON schema:
As such, the following result, which passes validation, is expected:
To Reproduce
To reproduce, simply follow the above steps and observe the output's validation result.
I used the following site to validate the JSON in this example:
https://www.jsonschemavalidator.net/
The text was updated successfully, but these errors were encountered: