module Data.Aeson.OneLine
( renderValue
, renderObject
, renderArray
) where
import qualified Data.Aeson.Text as Aeson
import qualified Data.Aeson.Types as Aeson
import qualified Data.Foldable as Foldable
import Prelude hiding ((+))
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Lazy as LText
import qualified Data.Text.Lazy.Builder as LText
import qualified Data.HashMap.Lazy as HashMap
(+) :: Text -> Text -> Text
(+) = Text.append
commaSeparate :: [Text] -> Text
commaSeparate = Text.intercalate (Text.pack ", ")
renderValue :: Aeson.Value -> Text
renderValue val =
case val of
Aeson.Object x -> renderObject x
Aeson.Array x -> renderArray x
x -> renderTerse x
renderTerse :: Aeson.Value -> Text
renderTerse =
LText.toStrict . LText.toLazyText . Aeson.encodeToTextBuilder
renderObject :: Aeson.Object -> Text
renderObject obj =
Text.pack "{" + x + Text.pack "}"
where
x = commaSeparate (f <$> HashMap.toList obj)
f (k, v) = renderTerse (Aeson.String k) +
Text.pack ": " + renderValue v
renderArray :: Aeson.Array -> Text
renderArray arr =
Text.pack "[" + x + Text.pack "]"
where
x = commaSeparate (renderValue <$> Foldable.toList arr)