Safe Haskell | None |
---|---|
Language | Haskell2010 |
Base module!
Has all functionality for parsing and serializing JSON.
Synopsis
- parseViaMegaparsec :: forall val. FromJSON val => Text -> Either String val
- parseViaAttoparsec :: FromJSON val => ByteString -> Either String val
- runParserViaAttoparsec :: (forall parser. JSONParser parser => parser a) -> ByteString -> Either String a
- runParserViaMegaparsec :: (forall parser. JSONParser parser => parser a) -> Text -> Either String a
- attoparsecParser :: FromJSON val => Parser val
- megaparsecParser :: FromJSON val => Parser val
- class FromJSON value where
- fromJSON :: JSONParser f => f value
- 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 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
- gFromJSON :: (GFromJSON v, JSONParser f) => FromJSONOptions -> f (v a)
- data FromJSONOptions = FromJSONOptions {}
- toJSONAsBuilder :: ToJSON a => a -> Builder
- toJSONViaBuilder :: ToJSON a => a -> ByteString
- toJSONText :: ToJSON a => a -> Text
- class ToJSON v where
- toJSON :: JSONSerializer f => f v
- class Selectable f => JSONSerializer f where
- serializeObject :: Text -> (forall objSerializer. JSONObjectSerializer objSerializer => objSerializer a) -> f a
- serializeDictionary :: Foldable t => (forall jsonSerializer. JSONSerializer jsonSerializer => jsonSerializer a) -> f (t (Text, a))
- serializeText :: f Text
- serializeTextConstant :: Text -> f a
- serializeNull :: f any
- serializeNumber :: f Scientific
- serializeBool :: f Bool
- serializeTuple :: (forall tupleSerializer. JSONTupleSerializer tupleSerializer => tupleSerializer a) -> f a
- serializeArray :: ToJSON a => f [a]
- class Divisible f => JSONObjectSerializer f where
- writeField :: Text -> (forall jsonSerializer. JSONSerializer jsonSerializer => jsonSerializer a) -> f a
- gToJSON :: (GToJSON v, JSONSerializer s) => ToJSONOptions -> s (v a)
- data ToJSONOptions = ToJSONOptions {}
JSON Parsing
Concretely
parseViaMegaparsec :: forall val. FromJSON val => Text -> Either String val Source #
Parse an object for which FromJSON
is defined via Megaparsec.
parseViaAttoparsec :: FromJSON val => ByteString -> Either String val Source #
Parse a ByteString via an Attoparsec Parser.
runParserViaAttoparsec :: (forall parser. JSONParser parser => parser a) -> ByteString -> Either String a Source #
runParserViaMegaparsec :: (forall parser. JSONParser parser => parser a) -> Text -> Either String a Source #
Run an abstract JSONParser via Megaparsec.
attoparsecParser :: FromJSON val => Parser val Source #
Get an Attoparsec parser for a particular JSON-parsable value.
megaparsecParser :: FromJSON val => Parser val Source #
Get a megaparsec parser for your JSON value. This parser will not construct any intermediate maps or other structures - your object will be parsed directly!
Note: this parser, until the ones that are built into the class, can consume whitespace at the start of the JSON.
Abstractly
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
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 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 # |
Generically
gFromJSON :: (GFromJSON v, JSONParser f) => FromJSONOptions -> f (v a) Source #
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))))) |
JSON Serialization
Concretely
toJSONAsBuilder :: ToJSON a => a -> Builder Source #
Serialize a Haskell datatype to a Builder
.
This is available for performance reasons: you may wish to use hPutBuilder in order to (more or less) directly serialize some JSON object to a file handle.
toJSONViaBuilder :: ToJSON a => a -> ByteString Source #
Serialize a Haskell datatype to a lazy ByteString.
toJSONText :: ToJSON a => a -> Text Source #
Abstractly
A class to provide the canonical way to encode a JSON.
This class uses finally tagless style to keep the instructions for serializing abstract. This allows us to automatically generate documentation, and to generate serializers that always avoid the need for intermediate structures.
This class is derivable generically, and will generate a "nice" format. In my opinion, at least.
Nothing
toJSON :: JSONSerializer f => f v Source #
Instances
class Selectable f => JSONSerializer f where Source #
An abstract representation of how to serialize a Haskell value into JSON.
:: Text | A name for the object. Should be "globally unique" as much as possible. |
-> (forall objSerializer. JSONObjectSerializer objSerializer => objSerializer a) | How to serialize the object.
The forall here keeps things abstract: you are only allowed to use the methods of |
-> f a |
serializeDictionary :: Foldable t => (forall jsonSerializer. JSONSerializer jsonSerializer => jsonSerializer a) -> f (t (Text, a)) Source #
serializeText :: f Text Source #
serializeTextConstant :: Text -> f a Source #
Serialize some text constant. Note that this returns a serializer of anything: if you are always going to serialize out the same string, we don't need to even look at the thing we're serializing!
serializeNull :: f any Source #
serializeNumber :: f Scientific Source #
serializeBool :: f Bool Source #
serializeTuple :: (forall tupleSerializer. JSONTupleSerializer tupleSerializer => tupleSerializer a) -> f a Source #
serializeArray :: ToJSON a => f [a] Source #
Instances
class Divisible f => JSONObjectSerializer f where Source #
An abstract representation of how to serialize a JSON object.
Since serializing is the exact opposite of parsing, we have to be
Decidable
instead of Alternative
.
That is, if we are serializing a JSON object, we need to be able to break things apart.
Unfortunately the combinators for breaking things apart are more annoying to use than the combinators for putting things together, and involve a lot of tuples everywhere.
Thankfully we provide a good interface to derive these classes generically!
:: Text | Label for the field to write |
-> (forall jsonSerializer. JSONSerializer jsonSerializer => jsonSerializer a) | How to write the field.
The forall ensures that JSON serialization is kept completely abstract.
You can only use the methods of |
-> f a |
Instances
JSONObjectSerializer CommaBuilder Source # | |
Defined in Jordan.ToJSON.Text writeField :: Text -> (forall (jsonSerializer :: Type -> Type). JSONSerializer jsonSerializer => jsonSerializer a) -> CommaBuilder a Source # |
Generically
gToJSON :: (GToJSON v, JSONSerializer s) => ToJSONOptions -> s (v a) Source #