Skip to content

Instantly share code, notes, and snippets.

@kowey
Created November 6, 2009 17:16
Show Gist options
  • Save kowey/228117 to your computer and use it in GitHub Desktop.
Save kowey/228117 to your computer and use it in GitHub Desktop.
{-# LANGUAGE DeriveDataTypeable #-}
data HillKey = HkId
| HkMainNameInfo
-- and many more
| HkLatitude
| HkLongitude
deriving (Data, Typeable)
showKey :: HillKey -> String
showKey = drop 4 -- "-hk-"
. concatMap twiggle . showConstr . toConstr
where
twiggle '_' = "-"
twiggle x = if isUpper x then [ '-', toLower x ] else [x]
data HillEntry = HillEntry { hiId :: Int
, hiMainNameInfo :: HillNameInfo
-- and many more
, hiLatitude :: Float
, hiLongitude :: Float
}
instance JSON HillEntry where
readJSON j =
do jo <- fromJSObject `fmap` readJSON j
let field x = maybe (fail $ "Could not find: " ++ x) readJSON
$ lookup x jo
HillEntry <$> field (showKey HkId)
<*> field (showKey HkMainNameInfo)
<*> field (showKey HkLatitude)
<*> field (showKey HkLongitude)
showJSON (HillEntry x0 x1 {- and many more -} x27 x28 ) =
JSObject . toJSObject $
[ ( showKey HkId , showJSON x0 )
, ( showKey HkMainNameInfo , showJSON x1 )
-- and many more
, ( showKey HkLatitude, showJSON x27 )
, ( showKey HkLongitude , showJSON x28 )
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment