jsonifier-0.2.1.2: Fast and simple JSON encoding toolkit
Safe HaskellSafe-Inferred
LanguageHaskell2010

Jsonifier

Description

Simple DSL for mapping Haskell values into JSON representation and rendering it into ByteString.

Synopsis

Execution

toByteString :: Json -> ByteString Source #

Render a JSON value into strict bytestring.

toWrite :: Json -> Write Source #

Render a JSON value into Write.

Json

data Json Source #

Specification of how to render a JSON value to ByteString. Sort of a JSON-specialized ByteString builder.

You can construct it from Haskell types using the specialized conversion functions like intNumber, textString or object. After constructing, you can convert to strict ByteString using the toByteString function.

Primitives

null :: Json Source #

JSON Null literal.

true :: Json Source #

JSON true Boolean literal.

false :: Json Source #

JSON false Boolean literal.

bool :: Bool -> Json Source #

JSON Boolean literal.

Numbers

intNumber :: Int -> Json Source #

JSON Number literal from Int.

wordNumber :: Word -> Json Source #

JSON Number literal from Word.

doubleNumber :: Double -> Json Source #

JSON Number literal from Double.

Since JSON doesn't have support for them, non-real values like NaN, Infinity, -Infinity get rendered as 0.

scientificNumber :: Scientific -> Json Source #

JSON Number literal from Scientific.

Strings

textString :: Text -> Json Source #

JSON String literal from Text.

scientificString :: Scientific -> Json Source #

JSON String literal from Scientific.

You may need this when the reader of your JSON cannot handle large number literals.

Composites

array :: Foldable f => f Json -> Json Source #

JSON Array literal from a foldable over element literals.

object :: Foldable f => f (Text, Json) -> Json Source #

JSON Object literal from a foldable over pairs of key to value literal.

Low-level

fromByteString :: ByteString -> Json Source #

Any JSON literal manually rendered as ByteString.

This is a low-level function allowing to avoid unnecessary processing in cases where you already have a rendered JSON at hand.

Warning:

It is your responsibility to ensure that the content is correct JSON.

fromWrite :: Write -> Json Source #

Any JSON literal manually rendered as Write.

This is a low-level function allowing to avoid unnecessary processing in cases where you already have a rendered JSON at hand.

You can think of Write as a specialized version of ByteString builder. You can efficiently convert a ByteString to Write using byteString, making it possible to have parts of the JSON value tree rendered using other libraries. You can as well manually implement encoders for your custom types.

Warning:

It is your responsibility to ensure that the content is correct, otherwise you may produce invalid JSON.