Currently I am trying to use the JsonFormatters
for serializing string in ISO 8601
spec. format in my start up config. but could not get it to work.
my Startup Config is as following,
services.AddMvcCore(
(options) => {
options.SslPort = 44321;
options.Filters.Add(new RequireHttpsAttribute());
}
)
.AddJsonFormatters(jsonSerializerSettings => {
jsonSerializerSettings.DateParseHandling = DateParseHandling.None;
jsonSerializerSettings.DateFormatString = "yyyy-MM-ddTHH:mm:ss.fffZ";
})
.AddApiExplorer()
.AddJsonOptions(options => {
options.AllowInputFormatterExceptionMessages = false;
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
I also tried ServiceStackText which is mentioned in the documentation but that did not work either,
NodaSerializerDefinitions.LocalTimeSerializer.ConfigureSerializer();
DateTimeZoneProviders.Tzdb
.CreateDefaultSerializersForNodaTime()
.ConfigureSerializersForNodaTime();
I always keep getting the following format,
i.e. for LocalDate
serialization,
{
"patientDob": "Thursday, June 15, 2017",
}
How can I configure the string ISO 8601
spec. formatting for Nodatime
date types globally?
my model,
{
public LocalDate patientDob { get; set; }
}
and my view model/api resource is
{
public string patientDob { get; set; }
}
This is the problem, in your controller:
res.NodaLocalDateString = apiResource.NodaLocalDate = nodaModel.NodaLocalDate.ToString ();
You're not converting a LocalDate
into JSON at all; you're converting a string into JSON, and you're obtaining that string by calling LocalDate.ToString()
.
Change your API resource to have a LocalDate
property instead of a string
property, so that the conversion is done by Json.NET instead of by you calling ToString()
.
See more on this question at Stackoverflow