Safe Haskell | Safe-Inferred |
---|
- This is the fifth assignment for IB016, semester spring 2015.
- Name: Name Surname
- UID: 123456
== Json parser
Your task is to implement the full JSON parser according to the format
specification provided at http://json.org/. Use Parsec
for
constructing the parser.
For simplicity, you may assume we are using common String
as input,
()
as the user state and Identity
as the underlying monad -- that is,
you can use the Parser
type provided by module Text.Parsec.String
.
If the provided input holds a valid JSON (a valid object or array
at the top level), the parser should return it as a JSON tree in your own
user-defined data type. If the input is invalid, the parse error should
be returned.
Even though you are only prescribed the behaviour of the main
function,
try to have a reasonable (flexible, modular) design of the module.
Any documentation for your own internal functions is very welcome.
=== Implementation notes
- Adhere strictly to the specification from http://json.org website. No deviations such as missing quote marks around strings should be accepted.
- For simplicity, you can consider all numbers in the JSON
being of the type
Double
(even if they do not have the decimal part). - To parse basic types, you can use the
read
function or equivalent (for examplereadLitChar
for constructing Unicode characters). - You can specify the JSON data type as you see fit. However, it should not allow for construction of invalid JSON objects.
- If you choose not to implement the bonus, an automatically derived
Show
instance for the JSON type is sufficient to use inmain
.
=== Bonus: pretty-printer
As a bonus, try to implement a reasonable pretty-printer for the JSON type
you define. You can use the traditional conventions for displaying
JSON objects on the web. If implemented, use this pretty-printer
in the main
function after a successful parse.
The pretty-printer should not be the Show
instance (this instance should
be derived automatically to retain all metadata of the used types).
- main :: IO ()
Documentation
main :: IO ()
Parse the given file as JSON printing the result. In case of successful parse, print the parsed JSON object. In case of parse failure, print the error. The filename is given as the only command line argument. If there are less/more arguments, display short usage info and exit. You do not have to check if the file exists.