Created
February 17, 2012 18:10
-
-
Save rikusalminen/1854681 to your computer and use it in GitHub Desktop.
JSON parsing with Parsec
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
module YaJSON where | |
import Text.Parsec | |
import qualified Text.Parsec.Token as Token | |
import qualified Text.Parsec.Language as Language | |
import qualified Data.Map as Map | |
language = Language.javaStyle | |
lexer = Token.makeTokenParser language | |
identifier = Token.identifier lexer | |
commaSep = Token.commaSep lexer | |
braces = Token.braces lexer | |
brackets = Token.brackets lexer | |
reservedOp = Token.reservedOp lexer | |
stringLiteral = Token.stringLiteral lexer | |
float = Token.float lexer | |
integer = Token.integer lexer | |
data JSON = | |
JSONFloat Double | | |
JSONInt Integer | | |
JSONString String | | |
JSONDict (Map.Map JSON JSON) | | |
JSONList [JSON] | |
deriving (Eq, Ord, Show) | |
json = | |
fmap JSONFloat (try float) <|> | |
fmap JSONInt (integer) <|> | |
fmap JSONString (identifier <|> stringLiteral) <|> | |
fmap (JSONDict . Map.fromList) (braces . commaSep $ pair) <|> | |
fmap JSONList (brackets . commaSep $ json) | |
where | |
pair = do | |
l <- json | |
reservedOp ":" | |
r <- json | |
return (l, r) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment