3

I want to use automated DeriveGeneric for my parameterized type. I get error. I want to decode a yaml file ino FromJSON type.

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeFamilies  #-}

import Web.Scotty
import Data.ByteString.Char8 (pack, unpack)
import Data.ByteString.Lazy (toStrict, fromStrict)
import Data.List
import Data.Yaml
import GHC.Generics

data EPSG a = EPSG { epsg3857 :: a }

data Resolution = Resolution { max :: Int, items :: [Double]}

data Config = Config { minX :: EPSG Double, minY :: EPSG Double, maxX :: EPSG Double, maxY :: EPSG Double
                   , resolution :: EPSG Resolution
                   , metersPerUnit :: EPSG Double
                   , pixelSize :: EPSG Double
                   , scaleNames :: EPSG [String]
                   , tileWidth :: EPSG Double
                   , tileHeight :: EPSG Double
                   , subdirBit :: EPSG [Int]
                   , subdirShiftBit :: EPSG [Int]
                   , subdirNumSize :: EPSG [Int]
                   , fileNameNumSize :: EPSG [Int] } deriving Generic

instance FromJSON EPSG *
instance FromJSON Resolution
instance FromJSON Config

Line EPSG * raises error. How should I fix it?

Kamyar
  • 2,494
  • 2
  • 22
  • 33

1 Answers1

6

Your definition of EPSG needs to derive generic as well, and then you need to constrain your instance to have a FromJSON instance for a as well.

data EPSG a = EPSG { epsg3857 :: a } deriving Generic

...

instance FromJSON a => FromJSON (EPSG a)
jkeuhlen
  • 4,401
  • 23
  • 36
  • 2
    If you have `DeriveAnyClass` on too, you can jump straight to `data EPSG a = EPSG { epsg3857 :: a } deriving (Generic, FromJSON)`. – Alec Dec 19 '17 at 16:08
  • @Alec Thanks for the tip! Does that replace `DeriveGeneric`? Or in addition to it? – jkeuhlen Dec 19 '17 at 16:13
  • Nope, you still need ‘DeriveGeneric’ too. The features are sort of orthogonal. – Alec Dec 19 '17 at 16:14