Automatically deriving instances recursively

I want to derive ToJSON and FromJSON for a type from an external module.

Specifically, I’m using the url package and would like to serialize its URL type to JSON with Aeson. Here’s my current solution:

{-# LANGUAGE  DeriveGeneric #-}
{-# LANGUAGE  StandaloneDeriving #-}

import qualified Network.URL as URL
import qualified GHC.Generics as Generics
import qualified Data.Aeson as Aeson

deriving instance Generics.Generic URL.Protocol
instance Aeson.ToJSON URL.Protocol
instance Aeson.FromJSON URL.Protocol
deriving instance Generics.Generic URL.Host
instance Aeson.ToJSON URL.Host
instance Aeson.FromJSON URL.Host
deriving instance Generics.Generic URL.URLType
instance Aeson.ToJSON URL.URLType
instance Aeson.FromJSON URL.URLType
deriving instance Generics.Generic URL.URL
instance Aeson.ToJSON URL.URL
instance Aeson.FromJSON URL.URL

Am I missing something? Is there a way to derive for URL and have the compiler derive instances for all the types used by URL? The process is very mechanical - I add those three lines for whichever type the compiler is complaining about.

2 Likes