clplug-1.0.0.0: src/Plugin/Generic.hs
{-# LANGUAGE FlexibleContexts #-}
module Plugin.Generic (defaultParse, singleField) where
import GHC.Generics
import Data.Aeson.Types
-- | helper for typical aeson instances
defaultParse :: (Generic a, GFromJSON Zero (Rep a)) => Value -> Parser a
defaultParse = genericParseJSON def
where
def = defaultOptions{
fieldLabelModifier = dropWhile (=='_') . map hyphen
, omitNothingFields = True
}
hyphen '5' = '-'
hyphen o = o
-- | many responses from node are objects under a single key, helper for this case
singleField :: (Generic a, GFromJSON Zero (Rep a)) => Key -> Value -> Parser a
singleField k1 (Object v) = v .: k1 >>= defaultParse
singleField _ _ = parseFail "Object is expected"