Created
October 20, 2019 12:46
-
-
Save ssanj/a5948d442bfc6c61b4b16e1c160edee2 to your computer and use it in GitHub Desktop.
Pretty Print Json in Haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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