Created
April 22, 2012 23:26
-
-
Save davidgrenier/2467483 to your computer and use it in GitHub Desktop.
JSon parser as per http://www.quanttec.com/fparsec/tutorial.html#fs-value-restriction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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