Created
October 27, 2015 15:39
-
-
Save owskio/e32a140fa6df8986c6ef to your computer and use it in GitHub Desktop.
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
#r @"C:\GitHub\FParsec\FParsec.1.0.2\lib\net40-client\FParsecCS.dll" | |
#r @"C:\GitHub\FParsec\FParsec.1.0.2\lib\net40-client\FParsec.dll" | |
open System | |
open FParsec | |
let print s = printfn "%A" s | |
type kvp = { key: String; value: int} | |
let quote = pchar('"') | |
let notQuote = noneOf(['"']) | |
let notQuotesTill = manyCharsTill(notQuote) | |
let notQuotes = notQuotesTill(quote) | |
let quotedParser = parse{do! spaces | |
do! skipChar('"') | |
let! listChar = notQuotes | |
do! spaces | |
return listChar } | |
let intParser = quotedParser |>> Convert.ToInt32 | |
let toyJsonParser = parse {do! spaces | |
do! skipChar('{') | |
let! key = quotedParser | |
do! skipChar(':') | |
let! value = intParser | |
do! skipChar('}') | |
do! spaces | |
return({ key = key; | |
value = value })} | |
run toyJsonParser """ | |
{ "age": "29" } | |
""" | |
|> function | |
| Failure(err,_,_) -> print ("Fail: " + err) | |
| Success(result,_,_) -> | |
printfn "KVP: %A" result | |
printfn "Key: %A" result.key | |
printfn "Val: %A" result.value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment