Skip to content

Instantly share code, notes, and snippets.

@davidgrenier
Created April 22, 2012 23:26
Show Gist options
  • Save davidgrenier/2467483 to your computer and use it in GitHub Desktop.
Save davidgrenier/2467483 to your computer and use it in GitHub Desktop.
type P<'t> = Parser<'t, unit>
type Json =
| JString of string
| JNumber of float
| JBool of bool
| JNull
| JList of Json list
| JObject of Map<string, Json>
let delim c = pchar c >>. spaces
let rec key : P<_> = pchar '"' >>. (manyChars (noneOf "\"")) .>> delim '"'
and jstring : P<_> = key |>> JString
and jnumber = pfloat .>> spaces |>> JNumber
and inline listBetweenChar sOpen sClose parser f : P<_> =
between (delim sOpen) (delim sClose) (sepBy parser comma) |>> f
and jarray input = listBetweenChar '[' ']' json JList <| input
and jobject input =
listBetweenChar '{' '}' (key .>>. (delim ':' >>. json)) (Map.ofList >> JObject) <| input
and jtrue = stringReturn "true" (JBool true)
and jfalse = stringReturn "false" (JBool false)
and comma = delim ','
and json = choice [jarray; jnumber; jstring; jobject; jtrue; jfalse; stringReturn "null" JNull]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment