Home > Software design >  How to indent output json files in haskell
How to indent output json files in haskell

Time:06-18

  • Can Haskell indent output json files so it is human friendly?
  • This option exists in other programming languages (like Python)
  • Listed below is a working example for:
    • reading an input json file
    • manipulating it somehow
    • storing back the results to another json file
  • Only trouble is, I can not humanly inspect it, because it is a huge one-liner
{-# LANGUAGE DeriveGeneric #-}

-- ***************
-- *             *
-- * module main *
-- *             *
-- ***************
module Main (main) where

-- *******************
-- *                 *
-- * general imports *
-- *                 *
-- *******************
import System.IO  
import Data.Aeson
import GHC.Generics
import Control.Monad
import System.Environment

-- *****************************
-- *                           *
-- * general qualified imports *
-- *                           *
-- *****************************
import qualified Data.ByteString.Lazy as B

-- ****************************
-- *                          *
-- * read json file to memory *
-- *                          *
-- ****************************
getJSON :: IO B.ByteString
getJSON = B.readFile "input.json"

-- *********************
-- *                   *
-- * data type: Person *
-- *                   *
-- *********************
data Person =
  Person
  {
    idnum  :: Integer,
    height :: Integer,
    weight :: Integer
  }
  deriving (Show, Generic)

-- *********************
-- *                   *
-- * data type: Person *
-- *                   *
-- *********************
instance ToJSON Person
instance FromJSON Person

-- **************************
-- *                        *
-- * data type: AfterPerson *
-- *                        *
-- **************************
data AfterMarathonPerson =
  AfterMarathonPerson
  {
    person     :: Person,
    weightLoss :: Integer
  }
  deriving (Show, Generic)

-- **************************
-- *                        *
-- * data type: AfterPerson *
-- *                        *
-- **************************
instance ToJSON AfterMarathonPerson

-- *******************
-- *                 *
-- * marathon effect *
-- *                 *
-- *******************
marathonEffect :: Person -> AfterMarathonPerson
marathonEffect (Person i h w) = AfterMarathonPerson (Person i h w) (w-23)

-- ********************
-- *                  *
-- * main entry point *
-- *                  *
-- ********************
main = do
  marathon <- (eitherDecode <$> getJSON) :: IO (Either String [Person])
  case marathon of
    Left jsonReadError -> putStrLn jsonReadError
    Right runners -> encodeFile "output.json"
      $ toJSON
      $ map marathonEffect
      $ runners

CodePudding user response:

Use encodePretty function from the aeson-pretty package to produce a ByteString of pretty-printed JSON and then write it to file with ByteString.writeFile instead of using encodeFile.

  • Related