简体   繁体   English

如何在haskell中缩进输出json文件

[英]How to indent output json files in haskell

  • Can Haskell indent output json files so it is human friendly ? Haskell 是否可以缩进输出 json 文件以使其对人类友好
  • This option exists in other programming languages (like Python)此选项存在于其他编程语言(如 Python)中
  • Listed below is a working example for:下面列出的是一个工作示例:
    • reading an input json file读取输入的 json 文件
    • manipulating it somehow以某种方式操纵它
    • storing back the results to another json file将结果存储回另一个 json 文件
  • 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

使用 aeson aeson-pretty包中的encodePretty函数生成漂亮打印的 JSON 的ByteString ,然后使用ByteString.writeFile而不是使用encodeFile将其写入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM