Skip to content

Instantly share code, notes, and snippets.

@ascjones
Created June 20, 2014 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ascjones/557a3de1726a04d433b6 to your computer and use it in GitHub Desktop.
Save ascjones/557a3de1726a04d433b6 to your computer and use it in GitHub Desktop.
[<AutoOpen>]
module JsonExtensions =
open System
open Fleece
open Fleece.Operators
open FSharpPlus
open System.Globalization
type FromJSONClass with
static member ToJSON(x: TimeSpan) =
let s = x.ToString("d\.hh\:mm\:ss", CultureInfo.InvariantCulture)
JString s
static member FromJSON(_: TimeSpan) =
function
| JString s ->
if s = null
then Failure "Expected TimeSpan, got null"
else match TimeSpan.TryParseExact(s, "d\.hh\:mm\:ss", CultureInfo.InvariantCulture) with
| true,ts -> Success ts
| _ -> failwithf "Invalid TimeSpan %s" s
| x -> Failure (sprintf "Expected JString, found %A" x)
@mausch
Copy link

mausch commented Jun 20, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment