blaze-json-0.2.1: tiny library for encoding json

Safe HaskellSafe-Inferred
LanguageHaskell2010

Text.Blaze.JSON.Internal

Synopsis

Documentation

newtype JSON Source

JSON encoding data type

Constructors

JSON 

toBuilder :: EncodeConfig -> JSON -> Builder Source

convert JSON to bytestring Builder.

encodeWith :: EncodeConfig -> JSON -> ByteString Source

encode JSON using given config

encode :: JSON -> ByteString Source

encode = encodeWith def

newtype EncodeConfig Source

>>> def :: EncodeConfig
EncodeConfig {escapeHtml = False}

Constructors

EncodeConfig 

Fields

escapeHtml :: Bool

escape < and > to \uXXXX.

bool :: Bool -> JSON Source

json boolean value from Bool

>>> bool True
"true"

null :: JSON Source

json null value

>>> null
"null"

integral :: Integral i => i -> JSON Source

json number value from Integral

>>> integral 32
"32"

double :: Double -> JSON Source

json number value from double

>>> double 235.12
"235.12"

float :: Float -> JSON Source

json number value from float

>>> float 235.12
"235.12"

text :: Text -> JSON Source

json text value from Text

>>> print $ text "foo\n"
"\"foo\\n\""

lazyText :: Text -> JSON Source

json text value from LazyText

>>> print $ lazyText "bar\0"
"\"bar\\u0000\""

utf8 :: ByteString -> JSON Source

utf8 encoded bytestring to JSON. since v0.2.0.

>>> utf8 $ T.encodeUtf8 "\29483"
"\"\29483\""

lazyUtf8 :: ByteString -> JSON Source

utf8 encoded lazy bytestring to JSON. since v0.2.0.

>>> lazyUtf8 $ TL.encodeUtf8 "\29356"
"\"\29356\""

array' :: Foldable f => (a -> JSON) -> f a -> JSON Source

array :: Foldable f => f JSON -> JSON Source

convert to json array

>>> array [integral 4, bool True]
"[4,true]"

object' :: Foldable f => (k -> Text) -> (a -> JSON) -> f (k, a) -> JSON Source

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

O(nlogn) convert to object

prior value is prevailed.

You could use unsafeObject when could ensure unique key.

>>> object [("foo", integral 12), ("bar", bool True), ("foo", text "ignored")]
"{\"foo\":12,\"bar\":true}"

unsafeObject' :: Foldable f => (k -> Text) -> (a -> JSON) -> f (k, a) -> JSON Source

unsafeObject :: Foldable f => f (Text, JSON) -> JSON Source

O(n) unique key list to object

>>> unsafeObject [("foo", integral 12), ("bar", bool True), ("foo", text "INVALID")]
"{\"foo\":12,\"bar\":true,\"foo\":\"INVALID\"}"