Skip to content

Instantly share code, notes, and snippets.

@owskio
Created October 27, 2015 15:39
Show Gist options
  • Save owskio/e32a140fa6df8986c6ef to your computer and use it in GitHub Desktop.
Save owskio/e32a140fa6df8986c6ef to your computer and use it in GitHub Desktop.
#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