Skip to content

Instantly share code, notes, and snippets.

@ssanj
Created October 20, 2019 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssanj/a5948d442bfc6c61b4b16e1c160edee2 to your computer and use it in GitHub Desktop.
Save ssanj/a5948d442bfc6c61b4b16e1c160edee2 to your computer and use it in GitHub Desktop.
Pretty Print Json in Haskell
{-# LANGUAGE OverloadedStrings #-}
module Milo.Blog where
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import qualified Data.Text.Encoding as E
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString.Lazy.Char8 as LBS8
import qualified Data.Aeson as A (Value(String))
import Data.Aeson.Types (parseEither)
import Data.Aeson ((.=), Value, object, encode, FromJSON, fromJSON)
import Data.Aeson.Encode.Pretty (encodePretty)
tweetJson :: Value
tweetJson =
object [
"created_at" .= A.String "Wed Sep 18 01:28:16 +0000 2019",
"user" .= object [
"screen_name" .= A.String "tweetbot",
"name" .= A.String "The Twittebot"
],
"full_text" .= A.String "this is a strange tweet",
"lang" .= A.String "en"
]
lsbToText :: LBS.ByteString -> T.Text
lsbToText = E.decodeUtf8 . LBS.toStrict
jsonToText :: Value -> T.Text
jsonToText = lsbToText . encodePretty
prettyPrint :: Value -> IO ()
prettyPrint = TIO.putStrLn . jsonToText
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment