Skip to content

maboke123/jparse-hs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jparse-hs

A JSON parser built from scratch in Haskell using parser combinators.

What it does

Parses any valid JSON input into a typed Haskell AST:

data JSON = JSONNULL
          | JSONBool Bool
          | JSONNumber Double
          | JSONString String
          | JSONArray [JSON]
          | JSONObject [(String, JSON)]

Usage

parseJSON :: String -> Either String JSON

parseJSON "null"                          -- Right JSONNULL
parseJSON "42"                            -- Right (JSONNumber 42.0)
parseJSON "[1, true, \"hello\"]"          -- Right (JSONArray [...])
parseJSON "{\"key\": 42}"                 -- Right (JSONObject [...])
parseJSON "invalid"                       -- Left "..."

Features

  • All JSON types: null, booleans, numbers, strings, arrays, objects
  • Full string escape support: \", \\, \/, \n, \t, \r, \b, \f, \uXXXX
  • Numbers as Double: integers, negatives, floats, scientific notation (1.5e+10)
  • Strict number validation: no leading zeros, no trailing dot, no multiple signs
  • Descriptive error messages via Either String
  • Recursive: arbitrarily nested arrays and objects

How it works

The core type is a simple newtype:

newtype JSONParser a = JSONParser { runJSONParser :: String -> Either String (a, String) }

Functor, Applicative, and Alternative instances are implemented from scratch with no parsing libraries, only Control.Applicative and Data.Char from the standard library. Parsers are composed from small primitives (charParser, spanParser, many) into the full JSON grammar.

Running

cabal run

Reads from example.json in the project root and prints the parsed AST.

Known limitations

  • Error messages point to the last-attempted alternative rather than the actual failure site — a consequence of the Alternative instance discarding earlier errors
  • No \uXXXX surrogate pair support
  • Numbers use Double so integer values display as 42.0

About

A JSON parser built from scratch in Haskell using parser combinators.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors