module JSONBytesBuilder.Builder
(
JSON,
null,
boolean,
number_int,
number_integer,
number_double,
number_scientific,
string,
object,
array,
Object,
row,
Array,
element,
)
where
import JSONBytesBuilder.Prelude hiding (null)
import qualified Data.ByteString.Builder as A
import qualified JSONBytesBuilder.ByteString.Builders as E
newtype JSON =
JSON A.Builder
newtype Object =
Object (Maybe A.Builder)
instance Monoid Object where
mempty =
Object Nothing
mappend =
\case
Object (Just left) ->
\case
Object (Just right) ->
Object (Just (left <> A.char8 ',' <> right))
_ ->
Object (Just left)
Object Nothing ->
id
newtype Array =
Array (Maybe A.Builder)
instance Monoid Array where
mempty =
Array Nothing
mappend =
\case
Array (Just left) ->
\case
Array (Just right) ->
Array (Just (left <> A.char8 ',' <> right))
_ ->
Array (Just left)
Array Nothing ->
id
null :: JSON
null =
JSON (inline E.null)
boolean :: Bool -> JSON
boolean =
JSON . inline E.boolean
number_int :: Int -> JSON
number_int =
JSON . inline A.intDec
number_integer :: Integer -> JSON
number_integer =
JSON . inline A.integerDec
number_double :: Double -> JSON
number_double =
JSON . inline A.doubleDec
number_scientific :: Scientific -> JSON
number_scientific =
JSON . inline E.scientific
string :: Text -> JSON
string =
JSON . inline E.string
object :: Object -> JSON
object (Object x) =
JSON (maybe E.emptyObject (inline E.inCurlies) x)
array :: Array -> JSON
array (Array x) =
JSON (maybe E.emptyArray (inline E.inSquarelies) x)
row :: Text -> JSON -> Object
row key (JSON value) =
Object (Just (inline E.row key value))
element :: JSON -> Array
element (JSON value) =
Array (Just value)