extensible/JSON
aesonのFromJSON/ToJSONインスタンスがあらかじめ定義されているため、そのままJSONと相互変換できる。 Main.hs
code: (hs)
{- stack script
--resolver nightly-2018-05-09
--package extensible
--package aeson
--package bytestring
-}
{-# LANGUAGE DataKinds #-} {-# LANGUAGE TypeOperators #-} module Main where
import Data.Extensible
import Data.Aeson (eitherDecode)
import qualified Data.ByteString.Lazy as BL (readFile)
import System.Environment (getArgs)
type Config = Record
'[ "name" >: String
, "age" >: Int
]
main :: IO ()
main = do
content <- BL.readFile path
case eitherDecode content of
Left e -> putStrLn e
Right json -> print (json :: Config)
config.json
code:_
{
"name": "guchi",
"age": 0
}
実行結果
code:_
$ ./Main.hs config.json
name @= "guchi" <: age @= 0 <: nil