-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Very fast JSON serialisation and parsing library -- @package highjson @version 0.2.0.1 module Data.Json.Serialiser -- | Serialise json to a strict ByteString serialiseJsonBs :: ToJson a => a -> ByteString -- | Serialise json to a lazy ByteString serialiseJsonBsl :: ToJson a => a -> ByteString -- | Serialise json to a strict Text serialiseJsonT :: ToJson a => a -> Text -- | The class of types that can be converted to JSON values. See -- ObjectBuilder for an example of writing a ToJson -- instance for a custom data type. -- -- ToJson instances are provided for many common types. For -- example, to create a JSON array, call toJson on a list or -- Vector. To create a JSON object, call toJson on a -- HashMap. class ToJson a toJson :: ToJson a => a -> Value runSerSpec :: SerSpec k -> k -> Value -- | Parser specification. Use OnlyConstr for normal types and -- 'FirstConstr'/'NextConstr' for sum types data SerSpec k SingleConstr :: SerObjSpec k ts -> SerSpec k MultiConstr :: (k -> KeyedSerialiser k) -> SerSpec k -- | Associate a JSON key with a serialiser (.<-) :: ToJson a => Text -> a -> KeyedSerialiser k data KeyedSerialiser k -- | List of SpecKeys defining the serialisation of values to json data SerObjSpec k (ts :: [*]) SerObjSpecNil :: SerObjSpec k [] (:&&&:) :: !(SpecKey k t) -> !(SerObjSpec k ts) -> SerObjSpec k (t : ts) -- | A json key and a getter data SpecKey k t -- | Construct a SpecKey mapping a json key to a getter function (.:) :: (ToJson t, Typeable t) => Text -> (k -> t) -> SpecKey k t -- | Construct a SpecKey mapping a json key to a getter function of -- a Maybe type. This allows to omit the key when generating json -- instead of setting it to null. (.:?) :: (ToJson t, Typeable t) => Text -> (k -> Maybe t) -> SpecKey k (Maybe t) -- | Builds a JSON object. -- -- An ObjectBuilder builds one or more key-value pairs of a JSON -- object. They are constructed with the .= operator and combined -- with <>. -- -- To turn an ObjectBuilder into a Value, use its -- ToJson class instance. -- --
-- data Friend = Friend
-- { fId :: !Int
-- , fName :: !Text
-- } deriving (Eq, Show)
--
-- instance ToJson Friend where
-- toJson friend = toJson $
-- "id" .= fId friend
-- <> "name" .= fName friend
--
--
-- WARNING: ObjectBuilder does not check uniqueness of
-- object keys. If two keys with the same value are inserted, then the
-- resulting JSON document will be illegal.
data ObjectBuilder :: *
-- | A Value that produces the empty object.
emptyObject :: Value
-- | Represents a JSON value.
--
-- Values are built up from either ToJson instances or from
-- primitives like emptyObject, array, and null.
--
-- In special cases, or when performance is of utmost importance, the
-- unsafe functions unsafeAppendUtf8Builder are available.
--
-- Internally, Value encodes an action or sequence of actions that append
-- JSON-encoded text to the underlying Utf8Builder.
data Value :: *
-- | Create an ObjectBuilder from a key and a value.
(.=) :: ToJson a => Text -> a -> ObjectBuilder
-- | Create an ObjectBuilder from a key and a value. The key is an
-- ASCII-7, unescaped, zero-terminated Addr#.
--
-- WARNING: This function is unsafe. If the key is NOT
-- zero-terminated, then an access violation might result. If the key is
-- not a sequence of unescaped ASCII characters, the resulting JSON
-- document will be illegal.
--
-- This function is provided for maximum performance in the common case
-- that object keys are ASCII-7. It achieves performance by avoiding the
-- CAF for a Text literal and avoiding the need to transcode UTF-16 to
-- UTF-8 and escape.
--
-- To use this function, the calling source file must have the MagicHash
-- extension enabled.
--
--
-- data Friend = Friend
-- { fId :: !Int
-- , fName :: !Text
-- } deriving (Eq, Show)
--
-- instance ToJson Friend where
-- toJson friend = toJson $
-- "id"# .=# fId friend
-- <> "name"# .=# fName friend
--
(.=#) :: ToJson a => Addr# -> a -> ObjectBuilder
-- | Create an ObjectBuilder from an arbitrary key and value. The key can
-- be any type with a ToJsonString instance.
row :: (ToJsonString k, ToJson v) => k -> v -> ObjectBuilder
-- | Serialize any Foldable as a JSON array. This is generally
-- slower than directly calling toJson on a list or Vector,
-- but it will convert any Foldable type into an array.
array :: (Foldable t, ToJson a) => t a -> Value
-- | Represents a JSON "null".
nullValue :: Value
instance ToJson Int64
instance (ToJson a, ToJson b) => ToJson (a, b)
instance (ToJson a, ToJson b) => ToJson (Either a b)
module Data.Json.Parser
-- | Parse json from a strict ByteString
parseJsonBs :: JsonReadable t => ByteString -> Either String t
-- | Parse json from a lazy ByteString
parseJsonBsl :: JsonReadable t => ByteString -> Either String t
-- | Parse json from a strict Text
parseJsonT :: JsonReadable t => Text -> Either String t
-- | Typeclass defining an attoparsec Parser how Haskell types
-- should be parsed from JSON. Use predifined instances (with
-- readJson) and runSpec (on ObjSpec) to define
-- instances for custom types
class JsonReadable t
readJson :: JsonReadable t => Parser t
-- | Convert a ParseSpec into a Parser
runParseSpec :: ParseSpec k -> Parser k
-- | List of TypedKeys, should be in the same order as your
-- constructor in runSpec will expect them
data ObjSpec (ts :: [*])
ObjSpecNil :: ObjSpec []
(:&&:) :: !(TypedKey t) -> !(ObjSpec ts) -> ObjSpec (t : ts)
-- | Parser specification. Use :$: for normal types and
-- FirstConstr / :|: for sum types
data ParseSpec k
(:$:) :: HVectElim ts k -> ObjSpec ts -> ParseSpec k
FirstConstr :: KeyedConstr k -> ParseSpec k
(:|:) :: KeyedConstr k -> ParseSpec k -> ParseSpec k
-- | Associates a json key with a parser
data KeyedConstr k
-- | Choice between multiple constructors
(<||>) :: KeyedConstr k -> ParseSpec k -> ParseSpec k
class ConstrTagger r where type family ResultType r :: *
(.->) :: ConstrTagger r => Text -> Parser (ResultType r) -> r
-- | Json object key to a value t
data TypedKey t
-- | Required json object key. Use IsString instance for automatic
-- choice
reqKey :: Typeable t => Text -> TypedKey t
-- | Optional json object key. Use IsString instance for automatic
-- choice
optKey :: Typeable t => Text -> TypedKey (Maybe t)
-- | Get the textual key of a TypedKey
typedKeyKey :: TypedKey t -> Text
-- | Parse a json object given a value parser for each key
readObject :: (Text -> Maybe (Parser a)) -> Parser (HashMap Text a)
type Parser = Parser ByteString
-- | A value that is Typeable and JsonReadable
data WrappedValue
WrappedValue :: !t -> WrappedValue
-- | Get a value out of the map returned by readObject
getValueByKey :: (Monad m, Typeable t) => Text -> HashMap Text WrappedValue -> m t
-- | Optionally get a value out of the map returned by readObject
getOptValueByKey :: (Monad m, Typeable t) => Text -> HashMap Text WrappedValue -> m (Maybe t)
instance [overlap ok] ConstrTagger (ParseSpec k)
instance [overlap ok] ConstrTagger (KeyedConstr k)
instance [overlap ok] Typeable t => IsString (TypedKey t)
instance [overlap ok] Typeable t => IsString (TypedKey (Maybe t))
instance [overlap ok] JsonReadable a => JsonReadable (HVect '[a])
instance [overlap ok] (JsonReadable a, JsonReadable b) => JsonReadable (Either a b)
instance [overlap ok] JsonReadable t => JsonReadable (Maybe t)
instance [overlap ok] JsonReadable Text
instance [overlap ok] JsonReadable Word64
instance [overlap ok] JsonReadable Word32
instance [overlap ok] JsonReadable Word16
instance [overlap ok] JsonReadable Word8
instance [overlap ok] JsonReadable Word
instance [overlap ok] JsonReadable Int64
instance [overlap ok] JsonReadable Int32
instance [overlap ok] JsonReadable Int16
instance [overlap ok] JsonReadable Int8
instance [overlap ok] JsonReadable Int
instance [overlap ok] JsonReadable Double
instance [overlap ok] JsonReadable Scientific
instance [overlap ok] JsonReadable Bool
instance [overlap ok] JsonReadable t => JsonReadable (t, t)
instance [overlap ok] JsonReadable t => JsonReadable (Vector t)
instance [overlap ok] JsonReadable t => JsonReadable [t]
module Data.Json
-- | Describes JSON parsing and serialisation of a Haskell type
data JsonSpec k (ts :: [*])
JsonSpec :: !(HVectElim ts k) -> !(FieldSpec k ts) -> JsonSpec k
j_constr :: JsonSpec k -> !(HVectElim ts k)
j_fields :: JsonSpec k -> !(FieldSpec k ts)
-- | Describes JSON parsing and serialisation of a list of fields
data FieldSpec k (ts :: [*])
EmptySpec :: FieldSpec k []
(:+:) :: !(FieldKey k t) -> !(FieldSpec k ts) -> FieldSpec k (t : ts)
-- | Describes a json key
data FieldKey k t
-- | Required json object key. Use IsString instance for automatic
-- choice
reqKey :: Typeable t => Text -> TypedKey t
-- | Optional json object key. Use IsString instance for automatic
-- choice
optKey :: Typeable t => Text -> TypedKey (Maybe t)
-- | Json object key to a value t
data TypedKey t
-- | Construct a FieldKey mapping a json key to a getter function
(.=) :: (ToJson t, JsonReadable t, Typeable t) => TypedKey t -> (k -> t) -> FieldKey k t
-- | Construct a FieldKey mapping a json key to a getter function of
-- a Maybe type. This allows to omit the key when generating json
-- instead of setting it to null.
(.=?) :: (ToJson t, JsonReadable t, Typeable t) => TypedKey (Maybe t) -> (k -> Maybe t) -> FieldKey k (Maybe t)
-- | Describes JSON parsing and serialisation of a Haskell sum type.
-- Currently the library can only guarantee matching parsers/serialisers
-- for non-sum types using JsonSpec.
data JsonSumSpec k
JsonSumSpec :: !(ParseSpec k) -> !(k -> KeyedSerialiser k) -> JsonSumSpec k
js_parser :: JsonSumSpec k -> !(ParseSpec k)
js_serialiser :: JsonSumSpec k -> !(k -> KeyedSerialiser k)
-- | Associate a json key with a parser
(.->) :: ConstrTagger r => Text -> Parser (ResultType r) -> r
-- | Choice between multiple constructors
(<||>) :: KeyedConstr k -> ParseSpec k -> ParseSpec k
-- | Associate a JSON key with a serialiser
(.<-) :: ToJson a => Text -> a -> KeyedSerialiser k
-- | Construct a Parser from JsonSpec to implement
-- JsonReadable instances
makeParser :: JsonSpec k ts -> Parser k
-- | Construct a function from JsonSpec to implement ToJson
-- instances
makeSerialiser :: JsonSpec k ts -> k -> Value
-- | Construct a Parser from JsonSumSpec to implement
-- JsonReadable instances
makeSumParser :: JsonSumSpec k -> Parser k
-- | Construct a function from JsonSumSpec to implement
-- ToJson instances
makeSumSerialiser :: JsonSumSpec k -> k -> Value
-- | The class of types that can be converted to JSON values. See
-- ObjectBuilder for an example of writing a ToJson
-- instance for a custom data type.
--
-- ToJson instances are provided for many common types. For
-- example, to create a JSON array, call toJson on a list or
-- Vector. To create a JSON object, call toJson on a
-- HashMap.
class ToJson a
toJson :: ToJson a => a -> Value
-- | Typeclass defining an attoparsec Parser how Haskell types
-- should be parsed from JSON. Use predifined instances (with
-- readJson) and runSpec (on ObjSpec) to define
-- instances for custom types
class JsonReadable t
readJson :: JsonReadable t => Parser t
-- | Parse json from a strict ByteString
parseJsonBs :: JsonReadable t => ByteString -> Either String t
-- | Parse json from a lazy ByteString
parseJsonBsl :: JsonReadable t => ByteString -> Either String t
-- | Parse json from a strict Text
parseJsonT :: JsonReadable t => Text -> Either String t
-- | Serialise json to a strict ByteString
serialiseJsonBs :: ToJson a => a -> ByteString
-- | Serialise json to a lazy ByteString
serialiseJsonBsl :: ToJson a => a -> ByteString
-- | Serialise json to a strict Text
serialiseJsonT :: ToJson a => a -> Text