Skip to content

Instantly share code, notes, and snippets.

@rikusalminen
Created February 17, 2012 18:10
Show Gist options
  • Save rikusalminen/1854681 to your computer and use it in GitHub Desktop.
Save rikusalminen/1854681 to your computer and use it in GitHub Desktop.
JSON parsing with Parsec
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