API response Include milliseconds when serializing DateTime even if component is zero

204 views Asked by At

DateTimeStamp : 2022-04-29 15:19:41.350 after serialization 2022-04-29T22:19:41.35Z instead of 2022-04-29T22:19:41.350Z

I have fixed this using below code :

public static void ServiceJsonSettings(HttpConfiguration config, IServiceMetadata serviceMetadata)
    {
        config.Formatters.Insert(0, new JsonNewtonsoftJilFallbackFormatter());
        IsoDateTimeConverter dateConverter = new IsoDateTimeConverter
        {
            DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fff'Z'",
            DateTimeStyles = DateTimeStyles.AdjustToUniversal
        };
        config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(dateConverter);

    }

Now for large object serialization i am using Jil Serializer configured as below:

public static readonly Options JilOptions = new Options(excludeNulls: true, includeInherited: true,
            serializationNameFormat: SerializationNameFormat.CamelCase, dateFormat: DateTimeFormat.ISO8601);

Using both Serializer Newtonsoft and JIL the output I am getting expected result : 2022-04-29T22:19:41.350Z.

But after testing it i found JIL serializer Serializer is trimming the zeros in millisecond part if it ends with 00 for ex: DateTimeStamp : 2022-04-29 15:19:41.300 serialized to 2022-04-29T22:19:41.3Z. On the other hand newtonsoft serializer produce expected result 2022-04-29T22:19:41.300Z.

Is there any way to provide dateFormat as "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fff'Z'" in JilOptions ?

0

There are 0 answers