aeson-better-errors-0.5.0.0: Better error messages when decoding JSON values.

Safe HaskellNone
LanguageHaskell2010

Data.Aeson.BetterErrors.Internal

Synopsis

Documentation

newtype Parse err a Source

The type of parsers: things which consume JSON values and produce either detailed errors or successfully parsed values (of other types).

The err type parameter is for custom validation errors; for parsers that don't produce any custom validation errors, I recommend you just stick a type variable in for full generality:

    asTuple :: Parse e (Int, Int)
    asTuple = (,) <$> nth 0 asIntegral <*> nth 1 asIntegral

Constructors

Parse (ReaderT ParseReader (Except (ParseError err)) a) 

Instances

runParse :: Parse err a -> Value -> Either (ParseError err) a Source

mapError :: (err -> err') -> Parse err a -> Parse err' a Source

Transform the error of a parser according to the given function.

(.!) :: Parse err a -> (err -> err') -> Parse err' a Source

An infix version of mapError.

type Parse' = Parse Void Source

The type of parsers which never produce custom validation errors.

runParser :: (s -> Either String Value) -> Parse err a -> s -> Either (ParseError err) a Source

parse :: Parse err a -> ByteString -> Either (ParseError err) a Source

Run a parser with a lazy ByteString containing JSON data. Note that the normal caveat applies: the JSON supplied must contain either an object or an array for this to work.

parseStrict :: Parse err a -> ByteString -> Either (ParseError err) a Source

Run a parser with a strict ByteString containing JSON data. Note that the normal caveat applies: the JSON supplied must contain either an object or an array for this to work.

parseValue :: Parse err a -> Value -> Either (ParseError err) a Source

Run a parser with a pre-parsed JSON Value.

toAesonParser :: (err -> Text) -> Parse err a -> Value -> Parser a Source

This function is useful when you have a Parse err a and you want to obtain an instance for FromJSON a. Simply define:

   parseJSON = toAesonParser showMyCustomError myParser

toAesonParser' :: Parse' a -> Value -> Parser a Source

Take a parser which never produces custom validation errors and turn it into an Aeson parser. Note that in this case, there is no need to provide a display function.

data ParseReader Source

Data used internally by the Parse type.

Constructors

ParseReader 

Instances

data PathPiece Source

A piece of a path leading to a specific part of the JSON data. Internally, a list of these is maintained as the parser traverses the JSON data. This list is included in the error if one occurs.

Constructors

ObjectKey Text 
ArrayIndex Int 

data ParseError err Source

A value indicating that the JSON could not be decoded successfully.

Instances

Functor ParseError 
Eq err => Eq (ParseError err) 
Show err => Show (ParseError err) 
MonadError (ParseError err) (Parse err) 

data ErrorSpecifics err Source

Detailed information in the case where a value could be parsed as JSON, but a value of the required type could not be constructed from it, for some reason.

Constructors

KeyMissing Text 
OutOfBounds Int 
WrongType JSONType Value

Expected type, actual value

ExpectedIntegral Double 
CustomError err 

Instances

data JSONType Source

An enumeration of the different types that JSON values may take.

displayError :: (err -> Text) -> ParseError err -> [Text] Source

Turn a ParseError into a human-readable list of Text values. They will be in a sensible order. For example, you can feed the result to mapM putStrLn, or unlines.

jsonTypeOf :: Value -> JSONType Source

Get the type of a JSON value.

liftParse :: (Value -> Either (ErrorSpecifics err) a) -> Parse err a Source

Lift any parsing function into the Parse type.

badSchema :: ErrorSpecifics err -> Parse err a Source

Aborts parsing, due to an error in the structure of the JSON - that is, any error other than the JSON not actually being parseable into a Value.

as :: (Value -> Maybe a) -> JSONType -> Parse err a Source

asText :: Parse err Text Source

Parse a single JSON string as Text.

asString :: Parse err String Source

Parse a single JSON string as a String.

asScientific :: Parse err Scientific Source

Parse a single JSON number as a Scientific.

asIntegral :: Integral a => Parse err a Source

Parse a single JSON number as any Integral type.

asRealFloat :: RealFloat a => Parse err a Source

Parse a single JSON number as any RealFloat type.

asBool :: Parse err Bool Source

Parse a single JSON boolean as a Bool.

asObject :: Parse err Object Source

Parse a JSON object, as an Object. You should prefer functions like eachInObject where possible, since they will usually generate better error messages.

asArray :: Parse err Array Source

Parse a JSON array, as an Array. You should prefer functions like eachInArray where possible, since they will usually generate better error messages.

asNull :: Parse err () Source

Parse a single JSON null value. Useful if you want to throw an error in the case where something is not null.

perhaps :: Parse err a -> Parse err (Maybe a) Source

Given a parser, transform it into a parser which returns Nothing when supplied with a JSON null, and otherwise, attempts to parse with the original parser; if this succeeds, the result becomes a Just value.

key :: Text -> Parse err a -> Parse err a Source

Take the value corresponding to a given key in the current object.

keyOrDefault :: Text -> a -> Parse err a -> Parse err a Source

Take the value corresponding to a given key in the current object, or if no property exists with that key, use the supplied default.

keyMay :: Text -> Parse err a -> Parse err (Maybe a) Source

Take the value corresponding to a given key in the current object, or if no property exists with that key, return Nothing .

key' :: Parse err a -> Text -> Parse err a -> Parse err a Source

nth :: Int -> Parse err a -> Parse err a Source

Take the nth value of the current array.

nthOrDefault :: Int -> a -> Parse err a -> Parse err a Source

Take the nth value of the current array, or if no value exists with that index, use the supplied default.

nthMay :: Int -> Parse err a -> Parse err (Maybe a) Source

Take the nth value of the current array, or if no value exists with that index, return Nothing.

nth' :: Parse err a -> Int -> Parse err a -> Parse err a Source

eachInArray :: Parse err a -> Parse err [a] Source

Attempt to parse each value in the array with the given parser, and collect the results.

eachInObject :: Parse err a -> Parse err [(Text, a)] Source

Attempt to parse each property value in the object with the given parser, and collect the results.

eachInObjectWithKey :: (Text -> Either err k) -> Parse err a -> Parse err [(k, a)] Source

Attempt to parse each property in the object: parse the key with the given validation function, parse the value with the given parser, and collect the results.

withValue :: (Value -> Either err a) -> Parse err a Source

Lifts a function attempting to validate an arbitrary JSON value into a parser. You should only use this if absolutely necessary; the other functions in this module will generally give better error reporting.

liftEither :: Either err a -> Parse err a Source

with :: Parse err a -> (a -> Either err b) -> Parse err b Source

withText :: (Text -> Either err a) -> Parse err a Source

withString :: (String -> Either err a) -> Parse err a Source

withIntegral :: Integral a => (a -> Either err b) -> Parse err b Source

withRealFloat :: RealFloat a => (a -> Either err b) -> Parse err b Source

withBool :: (Bool -> Either err a) -> Parse err a Source

withObject :: (Object -> Either err a) -> Parse err a Source

Prefer to use functions like 'key or eachInObject to this one where possible, as they will generate better error messages.

withArray :: (Array -> Either err a) -> Parse err a Source

Prefer to use functions like nth or eachInArray to this one where possible, as they will generate better error messages.