danrockcoll
danrockcoll

Reputation: 257

How To Remove Microseconds Portion Of DateTime From ServiceStack Serialized Response?

I am using ServiceStack v8.5.2 and have configured the date handler as follows:-

JsConfig.DateHandler = DateHandler.ISO8601;

I have a response POCO which has a DateTime field and the serialized output is:-

"created": "2025-02-05T09:49:25.0000000Z"

I have a lot of DateTime fields and they all have no microsecond value so the response is wasting a lot of bandwidth in sending back .000000 for every item.

I have not been able to find a simple way to customise the date handler format to remove the microseconds portion, most of the hits I have read through are from 10 or more years ago and don't seem to work with this current version. I feel like I am missing something really obvious as it does not seem like a major thing to change - can anyone help?

Upvotes: 0

Views: 72

Answers (1)

mythz
mythz

Reputation: 143369

Here are docs for JSON configuration options. E.g. for a more succinct format you can use:

JsConfig.DateHandler = DateHandler.ISO8601DateTime;

Or you can control the serialization of a built-in Type with a custom Serialize function:

JsConfig<DateTime>.SerializeFn = o => 
    o.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);

Upvotes: 2

Related Questions