api-tools-0.9.0.0: DSL for generating API boilerplate and docs
Safe HaskellNone
LanguageHaskell2010

Data.API.JSON

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

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

Instances

Instances details
Monad ParserWithErrs Source # 
Instance details

Defined in Data.API.JSON

Functor ParserWithErrs Source # 
Instance details

Defined in Data.API.JSON

Methods

fmap :: (a -> b) -> ParserWithErrs a -> ParserWithErrs b #

(<$) :: a -> ParserWithErrs b -> ParserWithErrs a #

MonadFail ParserWithErrs Source # 
Instance details

Defined in Data.API.JSON

Methods

fail :: String -> ParserWithErrs a #

Applicative ParserWithErrs Source # 
Instance details

Defined in Data.API.JSON

Alternative ParserWithErrs Source # 
Instance details

Defined in Data.API.JSON

data ParseFlags Source #

Options to modify the behaviour of the JSON parser

defaultParseFlags :: ParseFlags Source #

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

runParserWithErrsTop :: ParseFlags -> ParserWithErrs a -> Either [(JSONError, Position)] (a, [(JSONWarning, Position)]) Source #

Run a parser with given flags, starting in the outermost location, and returning warnings even if the parse was successful

FromJSON class with multiple error support

class FromJSONWithErrs a where Source #

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.

Minimal complete definition

Nothing

Methods

parseJSONWithErrs :: Value -> ParserWithErrs a Source #

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.

Instances

Instances details
FromJSONWithErrs Bool Source # 
Instance details

Defined in Data.API.JSON

FromJSONWithErrs Int Source # 
Instance details

Defined in Data.API.JSON

FromJSONWithErrs Integer Source # 
Instance details

Defined in Data.API.JSON

FromJSONWithErrs () Source # 
Instance details

Defined in Data.API.JSON

FromJSONWithErrs Version Source # 
Instance details

Defined in Data.API.JSON

FromJSONWithErrs Text Source # 
Instance details

Defined in Data.API.JSON

FromJSONWithErrs UTCTime Source # 
Instance details

Defined in Data.API.JSON

FromJSONWithErrs Value Source # 
Instance details

Defined in Data.API.JSON

FromJSONWithErrs Binary Source # 
Instance details

Defined in Data.API.JSON

FromJSONWithErrs Thing Source #

Generate an API spec from the JSON

Instance details

Defined in Data.API.API

FromJSONWithErrs DefaultValue Source # 
Instance details

Defined in Data.API.API.Gen

FromJSONWithErrs BasicType Source # 
Instance details

Defined in Data.API.API.Gen

FromJSONWithErrs TypeRef Source # 
Instance details

Defined in Data.API.API.Gen

FromJSONWithErrs APIType Source # 
Instance details

Defined in Data.API.API.Gen

FromJSONWithErrs Field Source # 
Instance details

Defined in Data.API.API.Gen

FromJSONWithErrs Conversion Source # 
Instance details

Defined in Data.API.API.Gen

FromJSONWithErrs UTCRange Source # 
Instance details

Defined in Data.API.API.Gen

FromJSONWithErrs IntRange Source # 
Instance details

Defined in Data.API.API.Gen

FromJSONWithErrs RegularExpression Source # 
Instance details

Defined in Data.API.API.Gen

FromJSONWithErrs Filter Source # 
Instance details

Defined in Data.API.API.Gen

FromJSONWithErrs SpecNewtype Source # 
Instance details

Defined in Data.API.API.Gen

FromJSONWithErrs Spec Source # 
Instance details

Defined in Data.API.API.Gen

FromJSONWithErrs APINode Source # 
Instance details

Defined in Data.API.API.Gen

FromJSONWithErrs a => FromJSONWithErrs [a] Source # 
Instance details

Defined in Data.API.JSON

FromJSONWithErrs a => FromJSONWithErrs (Maybe a) Source # 
Instance details

Defined in Data.API.JSON

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

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)] a Source #

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.

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

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, and produces warnings even if the parse succeeded.

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

Decode a ByteString and run the JSON parser

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

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

parseJSONDefault :: FromJSONWithErrs a => Value -> Parser a Source #

Suitable as an implementation of parseJSON that uses the FromJSONWithErrs instance (provided said instance was not defined using fromJSON!).

ParserWithErrs combinators

withInt :: String -> (Int -> ParserWithErrs a) -> Value -> ParserWithErrs a Source #

It's contrary to my principles, but I'll accept a string containing a number instead of an actual number, and will silently truncate floating point numbers to integers...

withField :: Text -> (Value -> ParserWithErrs a) -> Object -> ParserWithErrs a Source #

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

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

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 a Source #

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

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

Parse the value of a field, failing on missing fields

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

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.

Representation of JSON parsing errors

type JSONWarning = JSONError Source #

At present, we do not distinguish between errors and warnings

data Expected Source #

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

Instances

Instances details
Eq Expected Source # 
Instance details

Defined in Data.API.Error

Show Expected Source # 
Instance details

Defined in Data.API.Error

ToJSON Expected Source # 
Instance details

Defined in Data.API.Error

FromJSON Expected Source # 
Instance details

Defined in Data.API.Error

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 

Instances

Instances details
Eq Step Source # 
Instance details

Defined in Data.API.Error

Methods

(==) :: Step -> Step -> Bool #

(/=) :: Step -> Step -> Bool #

Show Step Source # 
Instance details

Defined in Data.API.Error

Methods

showsPrec :: Int -> Step -> ShowS #

show :: Step -> String #

showList :: [Step] -> ShowS #

ToJSON Step Source # 
Instance details

Defined in Data.API.Error

FromJSON Step Source # 
Instance details

Defined in Data.API.Error

SafeCopy Step Source # 
Instance details

Defined in Data.API.Error

PPLines Step Source # 
Instance details

Defined in Data.API.Error

Methods

ppLines :: Step -> [String] Source #

prettyJSONErrorPositions :: [(JSONError, Position)] -> String Source #

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

prettyJSONError :: JSONError -> String Source #

Human-readable description of a JSON parse error

prettyStep :: Step -> String Source #

Human-readable description of a single step in a position

Error construction