Web API is not able to serialize the DateTime sent as "LOGINDATE": "2020-04-05T01:00:21.45+04:00". it only accepts Date(milliseconds)

Viewed 388

Maybe I am confused or due to working from home I am not able to think. I have Web API 2.0 project which is receiving models in end points. I have android client which is sending Date in milliseconds format and for same end point i have MVC client which is sending date in "LOGINDATE": "2020-04-05T01:00:21.45+04:00". the date from MVC is not accepted by API and gives an error on model state like

System.FormatException: DateTime content '2020-04-05T10:52:42.333+04:00' does not start with '/Date(' and end with ')/' as required for JSON.

I have forced the WebApi to use the newtonsoft json formatter but when i debug the exception i found

System.Runtime.Serialization.Json.JsonReaderDelegator.ParseJsonDateInDefaultFormat(String originalDateTimeValue)

Following is the WEBAPICONFIG code for forcing use the newtonsoft mediatype.

var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
jsonFormatter.UseDataContractJsonSerializer = true;
JsonSerializerSettings CustomJsonSetting = new JsonSerializerSettings
{
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
    PreserveReferencesHandling = PreserveReferencesHandling.Objects,
    DateTimeZoneHandling = DateTimeZoneHandling.Local,
    DateFormatHandling = DateFormatHandling.IsoDateFormat,
    //DateFormatString = "yyyy-mm-dd HH:MM:ss"
};

CustomJsonSetting.Converters.Clear();
CustomJsonSetting.Converters.Add(new IsoDateTimeConverter());
JsonConvert.DefaultSettings = () => CustomJsonSetting;
jsonFormatter.SerializerSettings = CustomJsonSetting;
config.Formatters.Clear();
config.Formatters.Insert(0,jsonFormatter);

Then from MVC client i am compressing GZIP and sending the call like following:

var frmt = UniversalFormatters.GetJsonFormatter();
if (IsCompressionEnabled)
{
    var json = JsonConvert.SerializeObject(request, frmt.SerializerSettings);
    //new JavaScriptSerializer().Serialize(request);
    //
    client.DefaultRequestHeaders.Add("pHubCompression", "1");
    var content = new CompressedContent(
  new StringContent(json, Encoding.UTF8, "application/json"),
  CompressionMethod.GZip);
    return await client.PostAsync(EndPoint, content);
}
else
  return await client.PostAsync(EndPoint, request, frmt);

And Universal Formatter is defined as

public static JsonMediaTypeFormatter GetJsonFormatter()
{
    if (JsonFormatter==null)
    {
        JsonFormatter = new JsonMediaTypeFormatter();
        JsonFormatter.UseDataContractJsonSerializer = true;

        JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        JsonFormatter.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
        JsonFormatter.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
        JsonFormatter.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
        JsonFormatter.SerializerSettings.DateFormatString = "yyyy-MM-ddTHH:mm:ss";
    }
    return JsonFormatter;
}

I have done a bit R&D and people mentioned to create my own date formater which is a work around but i wanted to know if there is something wrong with my setting.

My Model is defined like following:

  [System.Runtime.Serialization.DataContractAttribute(Namespace = "http://MyProj.Core.Entities", Name = "RequestObject{0}")]
public class RequestObject<T> : IDisposable//, IEntity
    //where T : IEntity, IEntityCollection, ICompositeEntity
{
    private UserInfo m_userInfo;
    private T m_obj;}

and UserInfo is defined like

  [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Namespace = "http://MyProj.Business.Contracts", Name = "UserInfo")]
public partial class UserInfo : BaseEntity, IEntity, IDisposable
{

    [System.Runtime.Serialization.DataMember(EmitDefaultValue = true)]
    public DateTime? LOGINDATE { set; get; }}

Sorry for my bad English.

1 Answers

according to above comment by @dbc I removed JsonFormatter.UseDataContractJsonSerializer = true; this line from my startups and everything starts working. Thanks for the help.

Related