Z-Data-0.9.0.0: Array, vector and text
Copyright(c) Dong Han 2019
LicenseBSD
Maintainerwinterland1989@gmail.com
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Z.Data.JSON.Converter

Description

This module provides tools for converting protocol IR (e.g. Value) to Haskell ADTs:

Synopsis

Documentation

convert :: (a -> Converter r) -> a -> Either ConvertError r Source #

Run a Converter with input value.

data PathElement Source #

Elements of a (JSON) Value path used to describe the location of an error.

Constructors

Key !Text

Path element of a key into an object, "object.key".

Index !Int

Path element of an index into an array, "array[index]".

Embedded

path of a embedded (JSON) String

Instances

Instances details
Eq PathElement Source # 
Instance details

Defined in Z.Data.JSON.Converter

Ord PathElement Source # 
Instance details

Defined in Z.Data.JSON.Converter

Show PathElement Source # 
Instance details

Defined in Z.Data.JSON.Converter

Generic PathElement Source # 
Instance details

Defined in Z.Data.JSON.Converter

Associated Types

type Rep PathElement :: Type -> Type #

NFData PathElement Source # 
Instance details

Defined in Z.Data.JSON.Converter

Methods

rnf :: PathElement -> () #

type Rep PathElement Source # 
Instance details

Defined in Z.Data.JSON.Converter

type Rep PathElement = D1 ('MetaData "PathElement" "Z.Data.JSON.Converter" "Z-Data-0.9.0.0-9CZLncR3XfBEnMnIuwgUs7" 'False) (C1 ('MetaCons "Key" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Text)) :+: (C1 ('MetaCons "Index" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Int)) :+: C1 ('MetaCons "Embedded" 'PrefixI 'False) (U1 :: Type -> Type)))

data ConvertError Source #

Error info with (JSON) Path info.

Constructors

ConvertError 

Fields

Instances

Instances details
Eq ConvertError Source # 
Instance details

Defined in Z.Data.JSON.Converter

Ord ConvertError Source # 
Instance details

Defined in Z.Data.JSON.Converter

Show ConvertError Source # 
Instance details

Defined in Z.Data.JSON.Converter

Generic ConvertError Source # 
Instance details

Defined in Z.Data.JSON.Converter

Associated Types

type Rep ConvertError :: Type -> Type #

NFData ConvertError Source # 
Instance details

Defined in Z.Data.JSON.Converter

Methods

rnf :: ConvertError -> () #

Print ConvertError Source # 
Instance details

Defined in Z.Data.JSON.Converter

type Rep ConvertError Source # 
Instance details

Defined in Z.Data.JSON.Converter

type Rep ConvertError = D1 ('MetaData "ConvertError" "Z.Data.JSON.Converter" "Z-Data-0.9.0.0-9CZLncR3XfBEnMnIuwgUs7" 'False) (C1 ('MetaCons "ConvertError" 'PrefixI 'True) (S1 ('MetaSel ('Just "errPath") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [PathElement]) :*: S1 ('MetaSel ('Just "errMsg") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text)))

newtype Converter a Source #

Converter provides a monadic interface to convert protocol IR (e.g.Value) to Haskell ADT.

Constructors

Converter 

Fields

Instances

Instances details
Monad Converter Source # 
Instance details

Defined in Z.Data.JSON.Converter

Methods

(>>=) :: Converter a -> (a -> Converter b) -> Converter b #

(>>) :: Converter a -> Converter b -> Converter b #

return :: a -> Converter a #

Functor Converter Source # 
Instance details

Defined in Z.Data.JSON.Converter

Methods

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

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

MonadFail Converter Source # 
Instance details

Defined in Z.Data.JSON.Converter

Methods

fail :: String -> Converter a #

Applicative Converter Source # 
Instance details

Defined in Z.Data.JSON.Converter

Methods

pure :: a -> Converter a #

(<*>) :: Converter (a -> b) -> Converter a -> Converter b #

liftA2 :: (a -> b -> c) -> Converter a -> Converter b -> Converter c #

(*>) :: Converter a -> Converter b -> Converter b #

(<*) :: Converter a -> Converter b -> Converter a #

Alternative Converter Source # 
Instance details

Defined in Z.Data.JSON.Converter

Methods

empty :: Converter a #

(<|>) :: Converter a -> Converter a -> Converter a #

some :: Converter a -> Converter [a] #

many :: Converter a -> Converter [a] #

MonadPlus Converter Source # 
Instance details

Defined in Z.Data.JSON.Converter

Methods

mzero :: Converter a #

mplus :: Converter a -> Converter a -> Converter a #

fail' :: Text -> Converter a Source #

Text version of fail.

(<?>) :: Converter a -> PathElement -> Converter a infixl 9 Source #

Add (JSON) Path context to a converter

When converting a complex structure, it helps to annotate (sub)converters with context, so that if an error occurs, you can find its location.

withFlatMapR "Person" $ \o ->
  Person
    <$> o .: "name" <?> Key "name"
    <*> o .: "age" <?> Key "age"

(Standard methods like (.:) already do this.)

With such annotations, if an error occurs, you will get a (JSON) Path location of that error.

prependContext :: Text -> Converter a -> Converter a Source #

Add context to a failure message, indicating the name of the structure being converted.

prependContext "MyType" (fail "[error message]")
-- Error: "converting MyType failed, [error message]"