highjson-0.2.0.1: Very fast JSON serialisation and parsing library

Safe HaskellNone
LanguageHaskell2010

Data.Json

Contents

Synopsis

DSL to define JSON structure

data JsonSpec k ts Source

Describes JSON parsing and serialisation of a Haskell type

Constructors

JsonSpec 

Fields

j_constr :: !(HVectElim ts k)
 
j_fields :: !(FieldSpec k ts)
 

data FieldSpec k ts where Source

Describes JSON parsing and serialisation of a list of fields

Constructors

EmptySpec :: FieldSpec k [] 
(:+:) :: (ToJson t, JsonReadable t, Typeable t) => !(FieldKey k t) -> !(FieldSpec k ts) -> FieldSpec k (t : ts) infixr 5 

data FieldKey k t Source

Describes a json key

reqKey :: Typeable t => Text -> TypedKey t Source

Required json object key. Use IsString instance for automatic choice

optKey :: Typeable t => Text -> TypedKey (Maybe t) Source

Optional json object key. Use IsString instance for automatic choice

data TypedKey t Source

Json object key to a value t

Instances

(.=) :: (ToJson t, JsonReadable t, Typeable t) => TypedKey t -> (k -> t) -> FieldKey k t Source

Construct a FieldKey mapping a json key to a getter function

(.=?) :: (ToJson t, JsonReadable t, Typeable t) => TypedKey (Maybe t) -> (k -> Maybe t) -> FieldKey k (Maybe t) Source

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.

DSL to define JSON structure for sum types

data JsonSumSpec k Source

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.

Constructors

JsonSumSpec 

Fields

js_parser :: !(ParseSpec k)
 
js_serialiser :: !(k -> KeyedSerialiser k)
 

(.->) :: ConstrTagger r => Text -> Parser (ResultType r) -> r Source

Associate a json key with a parser

(<||>) :: KeyedConstr k -> ParseSpec k -> ParseSpec k infixr 3 Source

Choice between multiple constructors

(.<-) :: ToJson a => Text -> a -> KeyedSerialiser k Source

Associate a JSON key with a serialiser

Make parsers and serialisers from spec

makeParser :: JsonSpec k ts -> Parser k Source

Construct a Parser from JsonSpec to implement JsonReadable instances

makeSerialiser :: JsonSpec k ts -> k -> Value Source

Construct a function from JsonSpec to implement ToJson instances

makeSumParser :: JsonSumSpec k -> Parser k Source

Construct a Parser from JsonSumSpec to implement JsonReadable instances

makeSumSerialiser :: JsonSumSpec k -> k -> Value Source

Construct a function from JsonSumSpec to implement ToJson instances

class ToJson a where

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.

Methods

toJson :: a -> Value

class JsonReadable t where Source

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

Methods

readJson :: Parser t Source

Run parsers / serialisers

parseJsonBs :: JsonReadable t => ByteString -> Either String t Source

Parse json from a strict ByteString

parseJsonT :: JsonReadable t => Text -> Either String t Source

Parse json from a strict Text

serialiseJsonBs :: ToJson a => a -> ByteString Source

Serialise json to a strict ByteString

serialiseJsonBsl :: ToJson a => a -> ByteString Source

Serialise json to a lazy ByteString

serialiseJsonT :: ToJson a => a -> Text Source

Serialise json to a strict Text