public
Last active

More fun with Aeson

  • Download Gist
JSON.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
{-# 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
TestData.json
JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
{
"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"
]
}
 
}
}
TestOutput.txt
1 2
$ ./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})]}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.