-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Spec based JSON parsing/serialisation -- -- Low boilerplate, easy to use and very fast JSON serialisation and -- parsing without generics or TemplateHaskell @package highjson @version 0.3.0.0 module Data.HighJson data HighSpec a as HighSpec :: !Text -> !(Maybe Text) -> !(BodySpec a as) -> HighSpec a as [hs_name] :: HighSpec a as -> !Text [hs_description] :: HighSpec a as -> !(Maybe Text) [hs_bodySpec] :: HighSpec a as -> !(BodySpec a as) recSpec :: Text -> Maybe Text -> HVectElim flds t -> RecordFields t flds -> HighSpec t flds data RecordFields t fs [RFEmpty] :: RecordFields t '[] [:+:] :: RecordField t f -> RecordFields t fs -> RecordFields t (f : fs) 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 :: Text -> Maybe Text -> SumOptions t flds -> HighSpec t flds data SumOptions t os [SOEmpty] :: SumOptions t '[] [:|:] :: SumOption t o -> SumOptions t os -> SumOptions t (o : os) sumOpt :: Text -> Prism' t o -> SumOption t o (.->) :: Text -> Prism' t o -> SumOption t o jsonSerializer :: AllHave ToJSON as => HighSpec a as -> a -> Value jsonEncoder :: AllHave ToJSON as => HighSpec a as -> a -> Encoding jsonParser :: AllHave FromJSON as => HighSpec a as -> Value -> Parser a data BodySpec a as BodySpecRecord :: !(RecordSpec a as) -> BodySpec a as BodySpecSum :: !(SumSpec a as) -> BodySpec a as data RecordField t f RecordField :: !Text -> !Bool -> (Object -> Text -> Parser f) -> !(t -> f) -> RecordField t f [rf_jsonKey] :: RecordField t f -> !Text [rf_optional] :: RecordField t f -> !Bool [rf_jsonLoader] :: RecordField t f -> Object -> Text -> Parser f [rf_get] :: RecordField t f -> !(t -> f) data RecordSpec a fs RecordSpec :: (HVect fs -> a) -> RecordFields a fs -> RecordSpec a fs [rs_make] :: RecordSpec a fs -> HVect fs -> a [rs_fields] :: RecordSpec a fs -> RecordFields a fs data SumOption t o SumOption :: !Text -> !(Prism' t o) -> SumOption t o [so_jsonKey] :: SumOption t o -> !Text [so_prism] :: SumOption t o -> !(Prism' t o) data SumSpec a os SumSpec :: SumOptions a os -> SumSpec a os [ss_options] :: SumSpec a os -> SumOptions a os -- | A type that can be converted to JSON. -- -- An example type and instance: -- --
-- -- Allow ourselves to write Text literals.
-- {-# 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:
--
--
-- {-# 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 --class ToJSON a -- | Convert a Haskell value to a JSON-friendly intermediate type. toJSON :: ToJSON a => a -> Value -- | 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 --toEncoding :: ToJSON a => a -> Encoding toJSONList :: ToJSON a => [a] -> Value toEncodingList :: ToJSON a => [a] -> Encoding -- | 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: -- --
-- -- Allow ourselves to write Text literals.
-- {-# LANGUAGE OverloadedStrings #-}
--
-- data Coord = Coord { x :: Double, y :: Double }
--
-- instance FromJSON Coord where
-- parseJSON (Object v) = Coord <$>
-- v .: "x" <*>
-- v .: "y"
--
-- -- We do not expect a non-Object value here.
-- -- We could use mzero to fail, but typeMismatch
-- -- 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:
--
--
-- {-# 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 --class FromJSON a parseJSON :: FromJSON a => Value -> Parser a parseJSONList :: FromJSON a => Value -> Parser [a]