Comments (2)
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.
@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)
- Questions about Custom Serialization HOT 2
- YamlDotNet does not write accurately equal to yaml being loaded HOT 1
- JsonCompatible serializer is not Json compatible HOT 4
- Is the IDeserializer thread safe? HOT 2
- Update benchmark file
- 15.1.0 EnsureRoundTrip no longer works without tag mapping and protected default constructors HOT 4
- yamldotnet is missing NuGet package README file
- Json Compatible Serialization does not produce JSON for nodes.
- Including (parts of) another YAML file HOT 12
- Serilizing an object with tags similar to AWS Cloudformation HOT 4
- FSharp deserialization of records with optional record fields HOT 3
- Overriding the name of a type dynamically HOT 2
- ObjectNodeDeserializer: Naming Convention Error on Deserialization HOT 2
- is it possible to style a string in a special way? HOT 3
- Set license expression on nuget package HOT 1
- Improve nuget package readme HOT 2
- Serialization / deserialization is not thread safe HOT 2
- Very large files fail to parse HOT 6
- Using ```YamlStream``` will cause "property not found" in ```IDeserializer.Deserialize()``` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yamldotnet.