| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Data.HighJson
Contents
- A json specification for any type
- Construct specifications for records
- Construct specifications for sum types
- Construct specifications for enum types
- Shared between specifications for simplicity
- Generate json serializers/encoders and parsers from specs
- Specification structures
- Aeson reexports
- Implementation detail structures
- data HighSpec a ty as = HighSpec {
- hs_name :: !Text
- hs_description :: !(Maybe Text)
- hs_bodySpec :: !(BodySpec ty a as)
- data SpecType
- recSpec :: (IsDataSpec q, DContainer q ~ RecordFields) => Text -> Maybe Text -> HVectElim (DFields q) (DType q) -> q -> RecordTypeSpec (DType q) (DFields q)
- type RecordTypeSpec t flds = HighSpec t SpecRecord flds
- reqField :: FromJSON f => Text -> (t -> f) -> RecordField t f
- (.=) :: FromJSON f => Text -> (t -> f) -> RecordField t f
- optField :: FromJSON f => Text -> (t -> Maybe f) -> RecordField t (Maybe f)
- (.=?) :: FromJSON f => Text -> (t -> Maybe f) -> RecordField t (Maybe f)
- sumSpec :: (IsDataSpec q, DContainer q ~ SumOptions) => Text -> Maybe Text -> q -> SumTypeSpec (DType q) (DFields q)
- type SumTypeSpec t flds = HighSpec t SpecSum flds
- sumOpt :: Text -> Prism' t o -> SumOption t o
- (.->) :: Text -> Prism' t o -> SumOption t o
- enumSpec :: (IsDataSpec q, DContainer q ~ PhantomEnumContainer) => Text -> Maybe Text -> q -> EnumTypeSpec (DType q) (DFields q)
- type EnumTypeSpec t flds = HighSpec t SpecEnum flds
- enumOpt :: Text -> Prism' t () -> EnumOption t
- (@->) :: Text -> Prism' t () -> EnumOption t
- class IsDataSpec t where
- data a :& b = a :& b
- jsonSerializer :: AllHave ToJSON as => HighSpec a ty as -> a -> Value
- jsonEncoder :: AllHave ToJSON as => HighSpec a ty as -> a -> Encoding
- jsonParser :: AllHave FromJSON as => HighSpec a ty as -> Value -> Parser a
- data BodySpec ty a as where
- BodySpecRecord :: !(RecordSpec a as) -> BodySpec SpecRecord a as
- BodySpecSum :: !(SumSpec a as) -> BodySpec SpecSum a as
- BodySpecEnum :: !(EnumSpec a) -> BodySpec SpecEnum a as
- data RecordField t f = RecordField {
- rf_jsonKey :: !Text
- rf_optional :: !Bool
- rf_jsonLoader :: Object -> Text -> Parser f
- rf_get :: !(t -> f)
- data RecordSpec a fs = RecordSpec {
- rs_make :: HVect fs -> a
- rs_fields :: RecordFields a fs
- data RecordFields t fs where
- RFEmpty :: RecordFields t '[]
- (:+:) :: RecordField t f -> RecordFields t fs -> RecordFields t (f ': fs)
- data SumOption t o = SumOption {
- so_jsonKey :: !Text
- so_prism :: !(Prism' t o)
- data SumSpec a os = SumSpec {
- ss_options :: SumOptions a os
- data SumOptions t os where
- SOEmpty :: SumOptions t '[]
- (:|:) :: SumOption t o -> SumOptions t os -> SumOptions t (o ': os)
- data EnumOption t = EnumOption {
- eo_jsonKey :: !Text
- eo_prism :: !(Prism' t ())
- data EnumSpec a = EnumSpec {
- es_options :: [EnumOption a]
- class ToJSON a where
- class FromJSON a where
- newtype PhantomEnumContainer t ts = PhantomEnumContainer {
- unPhantomEnumContainer :: [EnumOption t]
- class CombinableContainer t where
A json specification for any type
data HighSpec a ty as Source #
Constructors
| HighSpec | |
Fields
| |
Construct specifications for records
recSpec :: (IsDataSpec q, DContainer q ~ RecordFields) => Text -> Maybe Text -> HVectElim (DFields q) (DType q) -> q -> RecordTypeSpec (DType q) (DFields q) Source #
The specification for a record. Contains a name, an optional description, the constructor and a description how to parse and serialize fields respecting a given json key.
type RecordTypeSpec t flds = HighSpec t SpecRecord flds Source #
A specification for a record
reqField :: FromJSON f => Text -> (t -> f) -> RecordField t f Source #
A required json field. The key must be present in the json.
optField :: FromJSON f => Text -> (t -> Maybe f) -> RecordField t (Maybe f) Source #
An optional json field.
Construct specifications for sum types
sumSpec :: (IsDataSpec q, DContainer q ~ SumOptions) => Text -> Maybe Text -> q -> SumTypeSpec (DType q) (DFields q) Source #
The specification for a sum type. Contains a name, an optional description and a mapping from all constructor (prims) to their respective json fields
type SumTypeSpec t flds = HighSpec t SpecSum flds Source #
A specification for an arbitrary sum type
Construct specifications for enum types
enumSpec :: (IsDataSpec q, DContainer q ~ PhantomEnumContainer) => Text -> Maybe Text -> q -> EnumTypeSpec (DType q) (DFields q) Source #
The specification for a classic enum type. Contains a name, an optional description and a mapping from all constructors to ther counterpart json string names.
type EnumTypeSpec t flds = HighSpec t SpecEnum flds Source #
A specification for a classic enum
Shared between specifications for simplicity
class IsDataSpec t where Source #
A type class that allows a unified notation for records and sum types. Build specifications using '(:&)' and '(.=)', '(.=?)', '(.->)' or '(@->)'
Minimal complete definition
Methods
compileRec :: t -> DContainer t (DType t) (DFields t) Source #
Instances
| IsDataSpec (EnumOption t) Source # | |
| IsDataSpec (SumOption t f) Source # | |
| IsDataSpec (RecordField t f) Source # | |
| (IsDataSpec x, IsDataSpec y, (~) * (DType x) (DType y), (~) (* -> [*] -> *) (DContainer x) (DContainer y), CombinableContainer (DContainer x)) => IsDataSpec ((:&) x y) Source # | |
Combination of two local specifications. For records, these are fields, for sum types and enums these are the options.
Constructors
| a :& b infixr 8 |
Instances
| Functor ((:&) a) Source # | |
| Foldable ((:&) a) Source # | |
| Traversable ((:&) a) Source # | |
| (Bounded b, Bounded a) => Bounded ((:&) a b) Source # | |
| (Eq b, Eq a) => Eq ((:&) a b) Source # | |
| (Show b, Show a) => Show ((:&) a b) Source # | |
| (Monoid a, Monoid b) => Monoid ((:&) a b) Source # | |
| (IsDataSpec x, IsDataSpec y, (~) * (DType x) (DType y), (~) (* -> [*] -> *) (DContainer x) (DContainer y), CombinableContainer (DContainer x)) => IsDataSpec ((:&) x y) Source # | |
| type DFields ((:&) x y) Source # | |
| type DType ((:&) x y) Source # | |
| type DContainer ((:&) x y) Source # | |
Generate json serializers/encoders and parsers from specs
Specification structures
data BodySpec ty a as where Source #
Constructors
| BodySpecRecord :: !(RecordSpec a as) -> BodySpec SpecRecord a as | |
| BodySpecSum :: !(SumSpec a as) -> BodySpec SpecSum a as | |
| BodySpecEnum :: !(EnumSpec a) -> BodySpec SpecEnum a as |
data RecordField t f Source #
Constructors
| RecordField | |
Fields
| |
Instances
| IsDataSpec (RecordField t f) Source # | |
| type DFields (RecordField t f) Source # | |
| type DType (RecordField t f) Source # | |
| type DContainer (RecordField t f) Source # | |
data RecordSpec a fs Source #
Constructors
| RecordSpec | |
Fields
| |
data RecordFields t fs where Source #
Constructors
| RFEmpty :: RecordFields t '[] | |
| (:+:) :: RecordField t f -> RecordFields t fs -> RecordFields t (f ': fs) infixr 5 |
Instances
Constructors
| SumOption | |
Fields
| |
Constructors
| SumSpec | |
Fields
| |
data SumOptions t os where Source #
Constructors
| SOEmpty :: SumOptions t '[] | |
| (:|:) :: SumOption t o -> SumOptions t os -> SumOptions t (o ': os) infixr 5 |
Instances
data EnumOption t Source #
Constructors
| EnumOption | |
Fields
| |
Instances
| IsDataSpec (EnumOption t) Source # | |
| type DFields (EnumOption t) Source # | |
| type DType (EnumOption t) Source # | |
| type DContainer (EnumOption t) Source # | |
Constructors
| EnumSpec | |
Fields
| |
Aeson reexports
A type that can be converted to JSON.
An example type and instance:
-- Allow ourselves to writeTextliterals. {-# LANGUAGE OverloadedStrings #-} data Coord = Coord { x :: Double, y :: Double } instance ToJSON Coord where toJSON (Coord x y) =object["x".=x, "y".=y] toEncoding (Coord x y) =pairs("x".=x<>"y".=y)
Instead of manually writing your ToJSON instance, there are two options
to do it automatically:
- Data.Aeson.TH provides Template Haskell functions which will derive an instance at compile time. The generated instance is optimized for your type so will probably be more efficient than the following two options:
- The compiler can provide a default generic implementation for
toJSON.
To use the second, simply add a deriving clause to your
datatype and declare a GenericToJSON instance for your datatype without giving
definitions for toJSON or toEncoding.
For example, the previous example can be simplified to a more minimal instance:
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
data Coord = Coord { x :: Double, y :: Double } deriving Generic
instance ToJSON Coord where
toEncoding = genericToEncoding defaultOptions
Why do we provide an implementation for toEncoding here? The
toEncoding function is a relatively new addition to this class.
To allow users of older versions of this library to upgrade without
having to edit all of their instances or encounter surprising
incompatibilities, the default implementation of toEncoding uses
toJSON. This produces correct results, but since it performs an
intermediate conversion to a Value, it will be less efficient
than directly emitting an Encoding. Our one-liner definition of
toEncoding above bypasses the intermediate Value.
If DefaultSignatures doesn't give exactly the results you want,
you can customize the generic encoding with only a tiny amount of
effort, using genericToJSON and genericToEncoding with your
preferred Options:
instance ToJSON Coord where
toJSON = genericToJSON defaultOptions
toEncoding = genericToEncoding defaultOptions
Methods
Convert a Haskell value to a JSON-friendly intermediate type.
toEncoding :: a -> Encoding #
Encode a Haskell value as JSON.
The default implementation of this method creates an
intermediate Value using toJSON. This provides
source-level compatibility for people upgrading from older
versions of this library, but obviously offers no performance
advantage.
To benefit from direct encoding, you must provide an
implementation for this method. The easiest way to do so is by
having your types implement Generic using the DeriveGeneric
extension, and then have GHC generate a method body as follows.
instance ToJSON Coord where
toEncoding = genericToEncoding defaultOptions
toJSONList :: [a] -> Value #
toEncodingList :: [a] -> Encoding #
Instances
A type that can be converted from JSON, with the possibility of failure.
In many cases, you can get the compiler to generate parsing code for you (see below). To begin, let's cover writing an instance by hand.
There are various reasons a conversion could fail. For example, an
Object could be missing a required key, an Array could be of
the wrong size, or a value could be of an incompatible type.
The basic ways to signal a failed conversion are as follows:
emptyandmzerowork, but are terse and uninformativefailyields a custom error messagetypeMismatchproduces an informative message for cases when the value encountered is not of the expected type
An example type and instance:
-- Allow ourselves to writeTextliterals. {-# LANGUAGE OverloadedStrings #-} data Coord = Coord { x :: Double, y :: Double } instance FromJSON Coord where parseJSON (Objectv) = Coord<$>v.:"x"<*>v.:"y" -- We do not expect a non-Objectvalue here. -- We could usemzeroto fail, buttypeMismatch-- gives a much more informative error message. parseJSON invalid =typeMismatch"Coord" invalid
Instead of manually writing your FromJSON instance, there are two options
to do it automatically:
- Data.Aeson.TH provides Template Haskell functions which will derive an instance at compile time. The generated instance is optimized for your type so will probably be more efficient than the following two options:
- The compiler can provide a default generic implementation for
parseJSON.
To use the second, simply add a deriving clause to your
datatype and declare a GenericFromJSON instance for your datatype without giving
a definition for parseJSON.
For example, the previous example can be simplified to just:
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
data Coord = Coord { x :: Double, y :: Double } deriving Generic
instance FromJSON Coord
If DefaultSignatures doesn't give exactly the results you want,
you can customize the generic decoding with only a tiny amount of
effort, using genericParseJSON with your preferred Options:
instance FromJSON Coord where
parseJSON = genericParseJSON defaultOptions
Instances
Implementation detail structures
newtype PhantomEnumContainer t ts Source #
Constructors
| PhantomEnumContainer | |
Fields
| |
Instances
class CombinableContainer t where Source #
A monoidal type class that respects type level lists associated to the bodies
Minimal complete definition