api-tools-0.4: DSL for generating API boilerplate and docs

Safe HaskellNone

Data.API.JSON

Contents

Description

This module defines a JSON parser, like Aeson's FromJSON, but with more detailed error-reporting capabilities. In particular, it reports errors in a structured format, and can report multiple independent errors rather than stopping on the first one encountered.

Synopsis

Representation of JSON parsing errors

data Expected Source

JSON type expected at a particular position, when a value of a different type was encountered

data FormatExpected Source

Special format expected of a string

Constructors

FmtBinary 
FmtUTC 
FmtOther 

type Position = [Step]Source

A position inside a JSON value is a list of steps, ordered innermost first (so going inside an object prepends a step).

data Step Source

Each step may be into a field of an object, or a specific element of an array.

Constructors

InField Text 
InElem Int 

prettyJSONErrorPositions :: [(JSONError, Position)] -> StringSource

Human-readable presentation of a list of parse errors with their positions

prettyJSONError :: JSONError -> StringSource

Human-readable description of a JSON parse error

prettyStep :: Step -> StringSource

Human-readable description of a single step in a position

Parser with multiple error support

data ParserWithErrs a Source

Like Parser, but keeping track of locations within the JSON structure and able to report multiple errors.

Careful! The Monad instance does not agree with the Applicative instance in all circumstances, and you should use the Applicative instance where possible. In particular:

  • pf <*> ps returns errors from both arguments
  • pf `ap` ps returns errors from pf only

data ParseFlags Source

Options to modify the behaviour of the JSON parser

defaultParseFlags :: ParseFlagsSource

Use this as a basis for overriding individual fields of the ParseFlags record, in case more flags are added in the future.

FromJSON class with multiple error support

class FromJSONWithErrs a whereSource

Like FromJSON, but keeping track of multiple errors and their positions. Moreover, this class is more liberal in accepting invalid inputs:

  • a string like "3" is accepted as an integer; and
  • the integers 0 and 1 are accepted as booleans.

Methods

parseJSONWithErrs :: Value -> ParserWithErrs aSource

Parse a JSON value with structured error-reporting support. If this method is omitted, fromJSON will be used instead: note that this will result in less precise errors.

fromJSONWithErrs :: FromJSONWithErrs a => Value -> Either [(JSONError, Position)] aSource

Run the JSON parser on a value to produce a result or a list of errors with their positions. This should not be used inside an implementation of parseJSONWithErrs as it will not pass on the current position.

fromJSONWithErrs' :: FromJSONWithErrs a => ParseFlags -> Value -> Either [(JSONError, Position)] aSource

Run the JSON parser on a value to produce a result or a list of errors with their positions. This version allows the ParseFlags to be specified.

decodeWithErrs :: FromJSONWithErrs a => ByteString -> Either [(JSONError, Position)] aSource

Decode a ByteString and run the JSON parser

decodeWithErrs' :: FromJSONWithErrs a => ParseFlags -> ByteString -> Either [(JSONError, Position)] aSource

Decode a ByteString and run the JSON parser, allowing the ParseFlags to be specified

ParserWithErrs combinators

withField :: Text -> (Value -> ParserWithErrs a) -> Object -> ParserWithErrs aSource

Look up the value of a field, treating missing fields as null

withDefaultField :: Bool -> Maybe Value -> Text -> (Value -> ParserWithErrs a) -> Object -> ParserWithErrs aSource

Look up the value of a field, which may be read-only or use a default value (depending on the ParseFlags).

(.:.) :: FromJSONWithErrs a => Object -> Text -> ParserWithErrs aSource

Parse the value of a field, treating missing fields as null

(.::) :: FromJSONWithErrs a => Object -> Text -> ParserWithErrs aSource

Parse the value of a field, failing on missing fields

withUnion :: [(Text, Value -> ParserWithErrs a)] -> Value -> ParserWithErrs aSource

Match an inhabitant of a disjoint union, which should be an object with a single field, and call the continuation corresponding to the field name.