Created
October 17, 2011 23:09
-
-
Save 23Skidoo/1294134 to your computer and use it in GitHub Desktop.
More fun with Aeson
This file contains 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 #-} | |
-- See | |
-- http://stackoverflow.com/questions/7800272/trying-to-parse-recursive-json-am-i-on-the-right-track/7800464 | |
import Control.Applicative | |
import Control.Monad | |
import Data.Aeson | |
import Data.Attoparsec | |
import qualified Data.ByteString as B | |
import Data.Map as M | |
import Data.Text | |
import qualified Data.Vector as V | |
data Outer = Outer { | |
oName :: Text, | |
oProducts :: M.Map Text Inner | |
} deriving Show | |
data Inner = Inner { | |
iQA :: Text, | |
iVM :: Text, | |
iAvailable :: V.Vector Text | |
} deriving Show | |
instance FromJSON Outer where | |
parseJSON (Object o) = Outer <$> o .: "name" <*> o .: "products" | |
parseJSON _ = mzero | |
instance FromJSON Inner where | |
parseJSON (Object o) = Inner <$> o .: "qa" <*> o .: "vm" <*> o .: "available" | |
parseJSON _ = mzero | |
parseAll :: B.ByteString -> Outer | |
parseAll s = case (parse (fromJSON <$> json) s) of | |
Done _ (Success e) -> e | |
_ -> error "Should not happen!" | |
main :: IO () | |
main = do p <- parseAll <$> B.readFile "json.txt" | |
print p |
This file contains 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
{ | |
"name": "versions", | |
"products": | |
{ | |
"IM": | |
{ | |
"qa":"5.0.01.004", | |
"vm":"5.0.01.003", | |
"available": | |
[ | |
"3.1.02.003", | |
"3.9.0", | |
"3.9.1", | |
"3.9.2", | |
"4.0.01.004", | |
"4.0.01.005", | |
"4.1.00.001", | |
"4.1.00.002", | |
"4.1.01.001", | |
"4.1.01.002", | |
"4.1.01.003", | |
"4.2.00.001", | |
"4.2.00.002", | |
"4.2.01.001", | |
"4.2.01.002", | |
"4.2.01.003", | |
"4.2.01.004", | |
"4.3.00.001", | |
"4.3.00.002", | |
"4.3.00.003", | |
"4.3.00.004", | |
"4.3.00.005", | |
"4.3.01.001", | |
"5.0.00.001", | |
"5.0.00.002", | |
"5.0.00.003", | |
"5.0.00.004", | |
"5.0.00.005", | |
"5.0.00.006", | |
"5.0.01.001", | |
"5.0.01.002", | |
"5.0.01.003", | |
"5.0.01.004" | |
] | |
}, | |
"IB": | |
{ | |
"qa":"5.0.01.004", | |
"vm":"5.0.01.003", | |
"available": | |
[ | |
"3.1.02.003", | |
"3.9.0", | |
"3.9.1", | |
"3.9.2", | |
"4.0.01.004", | |
"4.0.01.005", | |
"4.1.00.001", | |
"4.1.00.002", | |
"4.1.01.001", | |
"4.1.01.002", | |
"4.1.01.003", | |
"4.2.00.001", | |
"4.2.00.002", | |
"4.2.01.001", | |
"4.2.01.002", | |
"4.2.01.003", | |
"4.2.01.004", | |
"4.3.00.001", | |
"4.3.00.002", | |
"4.3.00.003", | |
"4.3.00.004", | |
"4.3.00.005", | |
"4.3.01.001", | |
"5.0.00.001", | |
"5.0.00.002", | |
"5.0.00.003", | |
"5.0.00.004", | |
"5.0.00.005", | |
"5.0.00.006", | |
"5.0.01.001", | |
"5.0.01.002", | |
"5.0.01.003", | |
"5.0.01.004" | |
] | |
} | |
} | |
} |
This file contains 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
$ ./Main json.txt | |
Outer {oName = "versions", oProducts = fromList [("IB",Inner {iQA = "5.0.01.004", iVM = "5.0.01.003", iAvailable = fromList ["3.1.02.003","3.9.0","3.9.1","3.9.2","4.0.01.004","4.0.01.005","4.1.00.001","4.1.00.002","4.1.01.001","4.1.01.002","4.1.01.003","4.2.00.001","4.2.00.002","4.2.01.001","4.2.01.002","4.2.01.003","4.2.01.004","4.3.00.001","4.3.00.002","4.3.00.003","4.3.00.004","4.3.00.005","4.3.01.001","5.0.00.001","5.0.00.002","5.0.00.003","5.0.00.004","5.0.00.005","5.0.00.006","5.0.01.001","5.0.01.002","5.0.01.003","5.0.01.004"] :: Data.Vector.Vector}),("IM",Inner {iQA = "5.0.01.004", iVM = "5.0.01.003", iAvailable = fromList ["3.1.02.003","3.9.0","3.9.1","3.9.2","4.0.01.004","4.0.01.005","4.1.00.001","4.1.00.002","4.1.01.001","4.1.01.002","4.1.01.003","4.2.00.001","4.2.00.002","4.2.01.001","4.2.01.002","4.2.01.003","4.2.01.004","4.3.00.001","4.3.00.002","4.3.00.003","4.3.00.004","4.3.00.005","4.3.01.001","5.0.00.001","5.0.00.002","5.0.00.003","5.0.00.004","5.0.00.005","5.0.00.006","5.0.01.001","5.0.01.002","5.0.01.003","5.0.01.004"] :: Data.Vector.Vector})]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment