Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Parse JSON using finally-tagless style.
This provides JSON parsing as an abstract interface. This interface provides a way to parse JSON that is *inspectable* and has some nice properties: for example, we can use it to build a parser that directly parses your data structure, without building some intermediate value type!
Synopsis
- class Applicative f => JSONObjectParser f where
- parseFieldWith :: Text -> (forall valueParser. JSONParser valueParser => valueParser a) -> f a
- parseField :: FromJSON v => Text -> f v
- class Applicative f => JSONTupleParser f where
- consumeItemWith :: (forall valueParser. JSONParser valueParser => valueParser a) -> f a
- consumeItem :: FromJSON v => f v
- class (Functor f, forall a. Monoid (f a)) => JSONParser f where
- parseObject :: Text -> (forall objectParser. JSONObjectParser objectParser => objectParser a) -> f a
- parseObjectStrict :: Text -> (forall objectParser. JSONObjectParser objectParser => objectParser a) -> f a
- parseDictionary :: (forall jsonParser. JSONParser jsonParser => jsonParser a) -> f [(Text, a)]
- parseText :: f Text
- parseTextConstant :: Text -> f ()
- parseTuple :: (forall arrayParser. JSONTupleParser arrayParser => arrayParser o) -> f o
- parseArray :: FromJSON a => f [a]
- parseArrayWith :: (forall jsonParser. JSONParser jsonParser => jsonParser a) -> f [a]
- parseNumber :: f Scientific
- parseNull :: f ()
- parseBool :: f Bool
- validateJSON :: f (Either Text a) -> f a
- class FromJSON value where
- fromJSON :: JSONParser f => f value
- data FromJSONOptions = FromJSONOptions {}
- defaultOptions :: FromJSONOptions
- addName :: String -> FromJSONOptions -> FromJSONOptions
- class GFromJSON v where
- gFromJSON :: JSONParser f => FromJSONOptions -> f (v a)
- class GFromJSONObject v where
- gFromJSONObject :: JSONObjectParser f => FromJSONOptions -> f (v a)
Documentation
class Applicative f => JSONObjectParser f where Source #
A class for parsing JSON objects.
:: Text | Label of the field. Will be parsed into escaped text, if need be. |
-> (forall valueParser. JSONParser valueParser => valueParser a) | How to parse the field. Note the forall in this type signature: you cannot have this be specific to any particular implementation of parsing, to keep the parsing of a JSON abstract. |
-> f a |
Parse an object field with a given label, using a parser.
Note: in order to enable the generation of better documentation, use parseField
instead if at all possible!
parseField :: FromJSON v => Text -> f v Source #
Instances
JSONObjectParser ObjectParser Source # | |
Defined in Jordan.FromJSON.Megaparsec parseFieldWith :: Text -> (forall (valueParser :: Type -> Type). JSONParser valueParser => valueParser a) -> ObjectParser a Source # parseField :: FromJSON v => Text -> ObjectParser v Source # |
class Applicative f => JSONTupleParser f where Source #
A class for parsing JSON arrays.
consumeItemWith :: (forall valueParser. JSONParser valueParser => valueParser a) -> f a Source #
Use a JSON parser to consume a single item of an array, then move onto the next one.
Note: you should prefer consumeItem
as it enables better documentation generation.
consumeItem :: FromJSON v => f v Source #
Consume a single array item.
Instances
JSONTupleParser ArrayParser Source # | |
Defined in Jordan.FromJSON.Megaparsec consumeItemWith :: (forall (valueParser :: Type -> Type). JSONParser valueParser => valueParser a) -> ArrayParser a Source # consumeItem :: FromJSON v => ArrayParser v Source # |
class (Functor f, forall a. Monoid (f a)) => JSONParser f where Source #
Abstract class representing various parsers.
All parsers must have a Monoid instance that represents choice with failure as the identity.
parseObject, parseDictionary, parseText, parseTuple, parseArrayWith, parseNumber, parseNull, parseBool, validateJSON
:: Text | A label for the object. This label should, as much as possible, be "globally unique" in some way. This will enable better generation of documentation. |
-> (forall objectParser. JSONObjectParser objectParser => objectParser a) | Instructions on how to parse the object. Note that the actual implementation is kept abstract: you can only use methods found in JSONObjectParser, or combinators of those methods. This ensures that we can generate the proper parser in all cases. |
-> f a |
parseObjectStrict :: Text -> (forall objectParser. JSONObjectParser objectParser => objectParser a) -> f a Source #
Parse an object where you are okay if we parse strictly, IE, do not allow extra fields. This sometimes enables us to generate parsers that run faster.
parseDictionary :: (forall jsonParser. JSONParser jsonParser => jsonParser a) -> f [(Text, a)] Source #
Parse a dictionary of key-value pairs.
Parse a text field.
parseTextConstant :: Text -> f () Source #
parseTuple :: (forall arrayParser. JSONTupleParser arrayParser => arrayParser o) -> f o Source #
Use a tuple parser to parse an array.
parseArray :: FromJSON a => f [a] Source #
parseArrayWith :: (forall jsonParser. JSONParser jsonParser => jsonParser a) -> f [a] Source #
parseNumber :: f Scientific Source #
validateJSON :: f (Either Text a) -> f a Source #
Instances
class FromJSON value where Source #
A class to provide the canonical way to parse a JSON. This class uses finally tagless tyle to keep the instructions for parsing abstract. This allows us to automatically generate documentation, and to generate parsers that do not use intermediate structures.
This class is derivable generically, and will generate a "nice" format. In my opinion, at least.
Nothing
fromJSON :: JSONParser f => f value Source #
Instances
data FromJSONOptions Source #
Instances
Generic FromJSONOptions Source # | |
Defined in Jordan.FromJSON.Class type Rep FromJSONOptions :: Type -> Type # from :: FromJSONOptions -> Rep FromJSONOptions x # to :: Rep FromJSONOptions x -> FromJSONOptions # | |
type Rep FromJSONOptions Source # | |
Defined in Jordan.FromJSON.Class type Rep FromJSONOptions = D1 ('MetaData "FromJSONOptions" "Jordan.FromJSON.Class" "jordan-0.1.0.0-inplace" 'False) (C1 ('MetaCons "FromJSONOptions" 'PrefixI 'True) (S1 ('MetaSel ('Just "fromJSONEncodeSums") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 SumTypeEncoding) :*: (S1 ('MetaSel ('Just "fromJSONBaseName") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 String) :*: S1 ('MetaSel ('Just "convertEnum") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (String -> String))))) |
addName :: String -> FromJSONOptions -> FromJSONOptions Source #
class GFromJSON v where Source #
gFromJSON :: JSONParser f => FromJSONOptions -> f (v a) Source #
Instances
class GFromJSONObject v where Source #
gFromJSONObject :: JSONObjectParser f => FromJSONOptions -> f (v a) Source #
Instances
GFromJSONObject (U1 :: Type -> Type) Source # | |
Defined in Jordan.FromJSON.Class gFromJSONObject :: JSONObjectParser f => FromJSONOptions -> f (U1 a) Source # | |
(GFromJSONObject lhs, GFromJSONObject rhs) => GFromJSONObject (lhs :*: rhs) Source # | |
Defined in Jordan.FromJSON.Class gFromJSONObject :: JSONObjectParser f => FromJSONOptions -> f ((lhs :*: rhs) a) Source # | |
(FromJSON c, Selector t) => GFromJSONObject (S1 t (K1 v c :: Type -> Type)) Source # | |
Defined in Jordan.FromJSON.Class gFromJSONObject :: JSONObjectParser f => FromJSONOptions -> f (S1 t (K1 v c) a) Source # |