10

I'm trying to parse the following JSON with aeson.

{
    "data": [
        {
            "id": "34",
            "type": "link",
            "story": "foo"
        },
        {
            "id": "35",
            "type": "link",
            "story": "bar"
        }
    ]
}

Since there are a lot of field I'd like to ignore, it seems I should use GHC generics. But how to write a data type definition that uses Haskell keywords like data and type? The following of course gives: parse error on input `data'

data Feed = Feed {data :: [Post]}
    deriving (Show, Generic)

data Post = Post {
        id :: String,
        type :: String,
        story :: String
    }
    deriving (Show, Generic)
Community
  • 1
  • 1
mb21
  • 34,845
  • 8
  • 116
  • 142

2 Answers2

13

You can write your own FromJSON and ToJSON instances without relying on GHC.Generics. This also means that you can use different field names for the data representation and the JSON representation.

Example instances for Post:

{-# LANGUAGE OverloadedStrings #-}
import Control.Applicative
import Data.Aeson
import qualified Data.ByteString.Lazy as LBS

data Post = Post {
        postId :: String,
        typ :: String,
        story :: String
  }
  deriving (Show)

instance FromJSON Post where
  parseJSON (Object x) = Post <$> x .: "id" <*> x.: "type" <*> x .: "story"
  parseJSON _ = fail "Expected an Object"

instance ToJSON Post where
  toJSON post = object 
    [ "id" .= postId post
    , "type" .= typ post
    , "story" .= story post
    ]

main :: IO ()
main = do
  print $ (decode $ Post "{\"type\": \"myType\", \"story\": \"Really interresting story\", \"id\" : \"SomeId\"}" :: Maybe Post)
  LBS.putStrLn $ encode $ Post "myId" "myType" "Some other story"

The same can be done for Feed. If you didn't have to ignore fields, you could also use deriveJSON from Data.Aeson.TH, which takes a function to modify field names as it's first argument.

bennofs
  • 11,873
  • 1
  • 38
  • 62
  • Thanks, works nicely! Would it also be possible to combine this approach with `Generic`? Say, the `Post` type didn't have the `type` attribute, it seems I can't have `Post deriving(Generic)` while implementing `Feed`'s `parseJSON` manually and then combine them as in the question. – mb21 Aug 25 '13 at 09:50
  • Finally, I was wondering whether I really need the `Feed` data type, just to get rid of the `data` attribute in the JSON or whether I can somehow get at the Posts directly. – mb21 Aug 25 '13 at 11:22
  • 1
    @mb21 [a] has FromJSON/ToJSON instances, so if all you want to do is serialize a list of Posts, just do it directly with `encode listOfPosts`. And you can combine the approach with Generic, just derive the instance for Post like any other instance using Generic, and write the instance for Feed manually. It just works. – bennofs Aug 25 '13 at 12:14
0

To combine @bennofs approach with generic you can follow the example from Aeson: the tutorial:

{-# LANGUAGE DeriveGeneric #-}

import GHC.Generics
import Data.Aeson
import Data.Aeson.Types

data Person = Person {
  _name :: String,
  _age  :: Int }
  deriving (Generic)

instance ToJSON Person where
  toJSON = genericToJSON defaultOptions {
             fieldLabelModifier = drop 1 }

instance FromJSON Person where
  parseJSON = genericParseJSON defaultOptions {
                fieldLabelModifier = drop 1 }

while setting the fieldLabelModifier to alternate only the desired labels:

keywordFieldLabelModifier "_id" = "id"
keywordFieldLabelModifier "_type" = "type"
keywordFieldLabelModifier = id
majkrzak
  • 1,332
  • 3
  • 14
  • 30