fsprojects / FSharp.Json

F# JSON Reflection based serialization library
Apache License 2.0
226 stars 26 forks source link

Feature request - ISO8601 date transformer(s) #46

Open mfrawley opened 3 years ago

mfrawley commented 3 years ago

Hey, I needed this for interop with another language (scala), there are possibly other betters ways to achieve this, but as every language implements ISO8601 (de-)serialization to/from JSON differently it would be nice to have a couple of the more popular string formats of the standard ready to help people whose goal is interop with other languages/libs. I did something like this but hardcoded the 'Z' as it is commonly used/expected as a shortcut for the 0 UTC offset, but it could be made more generic.

    type ISO8601UTCDate() =
        interface ITypeTransform with
            member x.targetType () = (fun _ -> typeof<string>) ()
            member x.toTargetType value = (fun (v: obj) -> (v :?> DateTime).ToString("yyyy-MM-ddTHH:mm:ssZ") :> obj) value
            member x.fromTargetType value = (fun (v: obj) -> DateTime.Parse(v :?> string) :> obj) value