A simple, strict JSON parser implemented in Haskell using Megaparsec.
- Parses standard JSON data types:
- Null (
null) - Booleans (
true,false) - Numbers (integers, floats, scientific notation)
- Strings (including escaped characters)
- Arrays
- Objects
- Null (
- Strict parsing: adheres closely to JSON standards (e.g., no comments allowed).
- Library & CLI: Use it as a library in your Haskell projects or as a standalone command-line tool.
- GHC (Glasgow Haskell Compiler)
- Cabal (Common Architecture for Building Applications and Libraries)
- Nix (Optional, for reproducible builds)
cabal buildThis project supports Nix flakes.
nix buildTo enter a development shell with all dependencies:
nix developYou can use the CLI to parse a JSON file and print the internal Haskell representation.
cabal run json-parser -- <filename>Example:
Given a file test.json:
{
"name": "Json Parser",
"version": 1.0,
"features": ["parsing", "cli"]
}Run:
cabal run json-parser -- test.jsonOutput:
JsonObject (fromList [("name",JsonString "Json Parser"),("version",JsonNumber 1.0),("features",JsonArray [JsonString "parsing",JsonString "cli"])])To use json-parser in your own Haskell code:
- Add
json-parserto yourbuild-dependsin.cabal. - Import the module and use
parseValueorparseFile.
{-# LANGUAGE OverloadedStrings #-}
import JsonParser (parseValue, JsonValue(..))
import Text.Megaparsec (parse)
main :: IO ()
main = do
let input = "[1, 2, 3]"
case parse parseValue "" input of
Left err -> putStrLn (show err)
Right val -> print valThe project includes a test suite using hspec.
cabal testMIT