You can reuse FromJSON
instance of Map String v
. Something like the next:
{-# LANGUAGE OverloadedStrings #-}
import Data.Functor
import Data.Monoid
import Data.Aeson
import Data.Map (Map)
import qualified Data.ByteString.Lazy as LBS
import System.Environment
newtype City = City (Map String String)
deriving Show
instance FromJSON City where
parseJSON val = City <$> parseJSON val
newtype Country = Country (Map String City)
deriving Show
instance FromJSON Country where
parseJSON val = Country <$> parseJSON val
newtype DB = DB (Map String Country)
deriving Show
instance FromJSON DB where
parseJSON val = DB <$> parseJSON val
main :: IO ()
main = do
file <- head <$> getArgs
str <- LBS.readFile file
print (decode str :: Maybe DB)
The output:
shum@shum-lt:/tmp/shum$ cat in.js
{
"A": {
"A1": {
"A11": "1111",
"A22": "2222"
}
},
"B": {
}
}
shum@shum-lt:/tmp/shum$ runhaskell test.hs in.js
Just (DB (fromList [("A",Country (fromList [("A1",City (fromList [("A11","1111"),("A22","2222")]))])),("B",Country (fromList []))]))
shum@shum-lt:/tmp/shum$
PS: You can do it without newtype
s, I used them only for clarity.