module JSONBytesBuilder.Private.Builder
where
import JSONBytesBuilder.Private.Prelude hiding (null)
import qualified Data.ByteString.Builder as A
import qualified Data.Text.Lazy as C
import qualified Data.ByteString.Lazy as D
import qualified JSONBytesBuilder.Private.ByteString.Builder as E
newtype Literal =
Literal A.Builder
newtype Rows =
Rows (Maybe A.Builder)
instance Semigroup Rows where
(<>) =
\case
Rows (Just left) ->
\case
Rows (Just right) ->
Rows (Just (left <> A.char8 ',' <> right))
_ ->
Rows (Just left)
Rows Nothing ->
id
instance Monoid Rows where
{-# INLINE mempty #-}
mempty =
Rows Nothing
{-# INLINE mappend #-}
mappend =
(<>)
newtype Elements =
Elements (Maybe A.Builder)
instance Semigroup Elements where
(<>) =
\case
Elements (Just left) ->
\case
Elements (Just right) ->
Elements (Just (left <> A.char8 ',' <> right))
_ ->
Elements (Just left)
Elements Nothing ->
id
instance Monoid Elements where
{-# INLINE mempty #-}
mempty =
Elements Nothing
{-# INLINE mappend #-}
mappend =
(<>)
{-# INLINE null #-}
null :: Literal
null =
Literal (inline E.null)
{-# INLINE boolean #-}
boolean :: Bool -> Literal
boolean =
Literal . inline E.boolean
{-# INLINE numberFromInt #-}
numberFromInt :: Int -> Literal
numberFromInt =
Literal . inline A.intDec
{-# INLINE numberFromInteger #-}
numberFromInteger :: Integer -> Literal
numberFromInteger =
Literal . inline A.integerDec
{-# INLINE numberFromDouble #-}
numberFromDouble :: Double -> Literal
numberFromDouble =
Literal . inline A.doubleDec
{-# INLINE numberFromScientific #-}
numberFromScientific :: Scientific -> Literal
numberFromScientific =
Literal . inline E.scientific
{-# INLINE stringFromText #-}
stringFromText :: Text -> Literal
stringFromText =
Literal . inline E.stringFromText
{-# INLINE stringFromLazyText #-}
stringFromLazyText :: C.Text -> Literal
stringFromLazyText =
Literal . inline E.stringFromLazyText
{-# INLINE stringFromBytes #-}
stringFromBytes :: ByteString -> Literal
stringFromBytes =
Literal . inline E.stringFromBytes
{-# INLINE stringFromLazyBytes #-}
stringFromLazyBytes :: D.ByteString -> Literal
stringFromLazyBytes =
Literal . inline E.stringFromLazyBytes
{-# INLINE object #-}
object :: Rows -> Literal
object (Rows x) =
Literal (maybe E.emptyObject (inline E.inCurlies) x)
{-# INLINE objectFromRows #-}
objectFromRows :: [Rows] -> Literal
objectFromRows =
object . mconcat
{-# INLINE array #-}
array :: Elements -> Literal
array (Elements x) =
Literal (maybe E.emptyArray (inline E.inSquarelies) x)
arrayFromLiterals :: [Literal] -> Literal
arrayFromLiterals =
array . foldMap element
{-# INLINE row #-}
row :: Text -> Literal -> Rows
row key (Literal literal) =
Rows (Just (inline E.row key literal))
{-# INLINE element #-}
element :: Literal -> Elements
element (Literal literal) =
Elements (Just literal)