title: Can I reuse ToJSON and FromJSON instances between Data.Aeson and Data.Yaml?
body: <p>Data.Aeson and Data.Yaml are libraries for working with JSON and YAML, respectively, which have nearly identical interfaces. I have written “Aeson-based” <code>FromJSON</code> and <code>ToJSON</code> instances for some of my types:</p>
  
  <pre><code>import Data.Aeson
  
  data PropertyValue = Nested Microformat | Flat Text
      deriving (Generic, Show)
  
  instance ToJSON PropertyValue where
      toEncoding = genericToEncoding defaultOptions { sumEncoding = UntaggedValue }
  
  instance FromJSON PropertyValue where
      parseJSON = genericParseJSON defaultOptions { sumEncoding = UntaggedValue }
  </code></pre>
  
  <p>and while Aeson uses these instances, Yaml seems to ignore them. (Specifically, I believe that Yaml is automatically generating encoders and decoders thanks to the fact that my type derives <code>Generic</code>.) I tried to make the instances work with Yaml as well by changing my code to</p>
  
  <pre><code>import Data.Aeson
  import qualified Data.Yaml as Y
  
  data PropertyValue = Nested Microformat | Flat Text
      deriving (Generic, Show)
  
  instance ToJSON PropertyValue where
      toEncoding = genericToEncoding defaultOptions { sumEncoding = UntaggedValue }
  
  instance FromJSON PropertyValue where
      parseJSON = genericParseJSON defaultOptions { sumEncoding = UntaggedValue }
  
  instance Y.ToJSON PropertyValue where
      toEncoding = genericToEncoding defaultOptions { sumEncoding = UntaggedValue }
  
  instance Y.FromJSON PropertyValue where
      parseJSON = genericParseJSON defaultOptions { sumEncoding = UntaggedValue }
  </code></pre>
  
  <p>but GHC complained:</p>
  
  <pre><code>Duplicate instance declarations:
    instance ToJSON PropertyValue
      -- Defined at src/Microformats.hs:54:10
    instance ToJSON PropertyValue
      -- Defined at src/Microformats.hs:60:10
  </code></pre>
  
  <p>Is it not possible to define instances for two different classes that happen to have the same name? Or is Yaml’s <code>ToJSON</code> actually the <em>same</em> class as Aeson’s somehow?</p>
  
  <p>More to the point, is it possible to reuse my <code>ToJSON</code> and <code>FromJSON</code> instances to avoid having to write the same code twice? Failing that, is it at least possible to write instances of both libraries’ <code>ToJSON</code> and <code>FromJSON</code> without confusing the compiler?</p>