Skip to content

eliseuv/json-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Json Parser

A simple, strict JSON parser implemented in Haskell using Megaparsec.

Features

  • Parses standard JSON data types:
    • Null (null)
    • Booleans (true, false)
    • Numbers (integers, floats, scientific notation)
    • Strings (including escaped characters)
    • Arrays
    • Objects
  • 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.

Prerequisites

  • GHC (Glasgow Haskell Compiler)
  • Cabal (Common Architecture for Building Applications and Libraries)
  • Nix (Optional, for reproducible builds)

Installation & Build

Using Cabal

cabal build

Using Nix

This project supports Nix flakes.

nix build

To enter a development shell with all dependencies:

nix develop

Usage

Command Line Interface (CLI)

You 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.json

Output:

JsonObject (fromList [("name",JsonString "Json Parser"),("version",JsonNumber 1.0),("features",JsonArray [JsonString "parsing",JsonString "cli"])])

Library

To use json-parser in your own Haskell code:

  1. Add json-parser to your build-depends in .cabal.
  2. Import the module and use parseValue or parseFile.
{-# 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 val

Testing

The project includes a test suite using hspec.

cabal test

License

MIT

About

Simple JSON parser in Haskell

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published