Haskell, Aeson & JSON parsing into custom type

Following on from a [previous post][1], I've found I'm totally stuck. I'm trying to parse a JSON structure into my own type, and not only am I stuck on how to parse the Array, I'm not even sure if I'm using the Aeson library as intended. Any help would be greatly appreciated. The code: data Exif = Exif [(T.Text, ExifValue)] deriving (Show) data ExifValue = ExifText T.Text | ExifInt Integer | ExifDouble Double | ExifBool Bool | ExifArray [ExifValue] deriving (Show) instance FromJSON ExifValue where parseJSON (Number (I n)) = return $ ExifInt n parseJSON (Number (D n)) = return $ ExifDouble n parseJSON (String s) = return $ ExifText s parseJSON (Bool b) = return $ ExifBool b -- parseJSON (Array a) = ????? instance FromJSON Exif where parseJSON (Object o) = do x <- sequence $ map f (M.assocs o) return $ Exif x where f (t, x) = do y <- parseJSON x return ((t, y) :: (T.Text, ExifValue)) parseExifFile = fmap parseExifData . B.readFile parseExifData :: B.ByteString -> Data.Attoparsec.Result (Data.Aeson.Result [Exif]) parseExifData content = parse (fmap fromJSON json) content The test file: [{ "SourceFile": "test.jpg", "ExifTool:ExifToolVersion": 8.61, "File:FileName": "test.jpg", "File:FileSize": 2174179, "File:FileModifyDate": "2011:07:27 16:53:49-07:00", "File:FilePermissions": 644, "File:FileType": "JPEG", "File:MIMEType": "image/jpeg", "File:ExifByteOrder": "MM", "File:CurrentIPTCDigest": "32d6a77098a73aa816f2570c9472735a", "File:ImageWidth": 2592, "File:ImageHeight": 1936, "File:EncodingProcess": 0, "File:BitsPerSample": 8, "File:ColorComponents": 3, "File:YCbCrSubSampling": "2 2", "XMP:Subject": ["alpha","beta","gamma"] }] [1]: https://stackoverflow.com/questions/6566149/haskell-aeson-how-to-convert-value-into-custom-type

以上就是Haskell, Aeson & JSON parsing into custom type的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JSON 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏