Kevin
Kevin

Reputation: 1657

Create ToJSON instance for persistent Entity

I'm trying to use the new selectRep function from Yesod 1.2, but I'm having trouble getting json responses to work.

instance ToJSON (Entity Feed) where
    toJSON (Entity uid (Feed url lastUpdated)) = object 
        [ "id" .= uid
        , "url" .= url
        , "lastUpdated" .= lastUpdated
        ]

getFeedByIdR :: FeedId -> Handler TypedContent
getFeedByIdR feedId = do
    feed <- runDB $ get404 feedId
    selectRep $ do
        provideRep $ return $ toJSON (Entity feedId feed)

The error I get from the above code is

Handler/Feed.hs:23:31:
    Overlapping instances for ToJSON (Entity Feed)
      arising from a use of `toJSON'
    Matching instances:
      instance ToJSON e => ToJSON (Entity e)
        -- Defined in `persistent-1.2.0.1:Database.Persist.Class.PersistEntity'
      instance ToJSON (Entity Feed) -- Defined at Handler/Feed.hs:5:10
    In the second argument of `($)', namely
      `toJSON (Entity feedId feed)'
    In the second argument of `($)', namely
      `return $ toJSON (Entity feedId feed)'
    In a stmt of a 'do' block:
      provideRep $ return $ toJSON (Entity feedId feed)

It seems that persistent does indeed provide an instance for ToJSON (Entity e) here, but can I use my ToJSON (Entity Feed) somehow?

Upvotes: 3

Views: 1125

Answers (3)

Michael Snoyman
Michael Snoyman

Reputation: 31315

If you provide an instance for Feed, then you can use the built-in Entity e instance. Adding json to your entity line will create that instance automatically, see: https://github.com/yesodweb/persistent/wiki/Persistent-entity-syntax#json-instances

Upvotes: 5

tomferon
tomferon

Reputation: 4991

As tel said, you could use newtype to define a different type and circumvent the restriction. However, if using a new type is too inconvenient, you could also use the language extension OverlappingInstances.

This extension allows you to have overlapping instances as long as there is always one more specific then the other (which will be chosen if it matches).

(See http://hackage.haskell.org/trac/haskell-prime/wiki/OverlappingInstances)

Upvotes: 0

J. Abrahamson
J. Abrahamson

Reputation: 74354

The usual trick for overriding default instances (which is what Yesod's ToJSON e => ToJSON (Entity e) instance is) is to use a newtype.

newtype EntityFeed = EF (Entity Feed)
instance ToJSON EntityFeed where ...

though that might be a little inconvenient.

Upvotes: 2

Related Questions