Exposing a custom type as a JSON string in an ASP.NET Core API

added by DotNetKicks
6/27/2020 9:29:18 PM


Sometimes your API needs to expose a non-primitive type that has a "natural" string representation. For instance, a standard representation for a duration is the ISO 8601 format, where "1 month, 2 days, 3 hours and 4 minutes" can be represented as P1M2DT3H4M (note that this isn't the same as a Timespan, which has no notion of calendar months and years).