I have recently began using NodaTime, and wanted to use the JSON.NET serializer settings which come with it. There is however one thing I do not understand and fail to find in the documentation.
From what I understand, the following exists now:
ConfigureForNodaTime()
// Disable automatic conversion of anything that looks like a date and time to BCL types.
settings.DateParseHandling = DateParseHandling.None;
converters.Add(NodaConverters.InstantConverter);
converters.Add(NodaConverters.IntervalConverter);
converters.Add(NodaConverters.LocalDateConverter);
converters.Add(NodaConverters.LocalDateTimeConverter);
converters.Add(NodaConverters.LocalTimeConverter);
converters.Add(NodaConverters.DateIntervalConverter);
converters.Add(NodaConverters.OffsetConverter);
converters.Add(NodaConverters.CreateDateTimeZoneConverter(provider));
converters.Add(NodaConverters.DurationConverter);
converters.Add(NodaConverters.RoundtripPeriodConverter);
converters.Add(NodaConverters.OffsetDateTimeConverter);
converters.Add(NodaConverters.CreateZonedDateTimeConverter(provider));
.WithIsoIntervalConverter()
ReplaceExistingConverters<Interval>(settings.Converters, NodaConverters.IsoIntervalConverter);
.WithIsoDateIntervalConverter()
ReplaceExistingConverters<DateInterval>(settings.Converters, NodaConverters.IsoDateIntervalConverter);
What is not clear to me, do I have to pick between WithIsoIntervalConverter
and WithIsoDateIntervalConverter
, or do I need to use both like ConfigureForNodaTime().WithIsoIntervalConverter().WithIsoDateIntervalConverter()
? Or do I simply not have to use any of them and just use ConfigureForNodaTime()
?
WithIsoIntervalConverter
replaces the converter for the Interval
type.
WithIsoDateIntervalConverter
replaces the converter for the DateInterval
type.
If you're not using Interval
or DateInterval
, you won't care what the converter does with them. If you are using either or both of those types, you need to think about how you want them to be serialized.
To stick with DateInterval
for example, the default serialization format (as configured by ConfigureForNodaTime
) produces JSON like this:
"value": { "Start": "2018-03-15" End: "2018-04-01" }
However, if you're trying to interoperate with code that expects an ISO representation of date intervals, you can use WithIsoDateIntervalConverter()
and the serialized data will look like this instead:
"value": "2018-03-15/2018-04-01"
See more on this question at Stackoverflow