{-# LANGUAGE OverloadedStrings #-}
module Saison.Decoding.Examples (
SomeValue (..),
countSomeValues,
Laureates (..),
Laureate (..),
) where
import Prelude ()
import Prelude.Compat
import Control.DeepSeq (NFData (..))
import Data.Text (Text)
import qualified Data.Aeson as Aeson
import Saison
import Saison.Decoding.Result
data SomeValue = SomeValue
deriving (Eq, Ord, Show)
instance NFData SomeValue where
rnf SomeValue = ()
instance Aeson.FromJSON SomeValue where
parseJSON _ = return SomeValue
instance Aeson.ToJSON SomeValue where
toJSON _ = Aeson.Null
instance FromTokens SomeValue where
fromTokens = \toks -> SomeValue <$ skipValue toks
countSomeValues :: Either e (Laureates SomeValue) -> Int
countSomeValues = either (const (-1)) (length . unLaureates)
newtype Laureates a = Laureates { unLaureates :: [a] }
deriving (Eq, Show)
instance NFData a => NFData (Laureates a) where
rnf (Laureates xs) = rnf xs
instance Aeson.FromJSON a => Aeson.FromJSON (Laureates a) where
parseJSON = Aeson.withObject "Laureates" $ \obj ->
Laureates <$> obj Aeson..: "laureates"
instance FromTokens a => FromTokens (Laureates a) where
fromTokens (TkRecordOpen toks0) = go toks0 where
go (TkPair t toks1)
| t == "laureates" =
Result $ \g f ->
unResult (fromTokens toks1) g $ \xs k -> case k of
TkRecordEnd k' -> f (Laureates xs) k'
_ -> g "Expecting record with exactly one key"
| otherwise = failResult $ "Expecting laureates key, got " ++ show t
go (TkRecordErr e) = failResult e
go (TkRecordEnd _) = failResult "Expecting record with exactly one key"
fromTokens (TkErr e) = failResult e
fromTokens _ = failResult "Expecting Record, got ????"
data Laureate = Laureate
{ lBorn :: !Text
, lBornCity :: !(Maybe Text)
, lBornCountry :: !(Maybe Text)
, lBornCountryCode :: !(Maybe Text)
, lDead :: !Text
, lDeadCity :: !(Maybe Text)
, lDeadCountry :: !(Maybe Text)
, lDeadCountryCode :: !(Maybe Text)
, lFirstName :: !(Maybe Text)
, lSurname :: !(Maybe Text)
, lId :: !Text
, lGender :: !Text
}
deriving (Eq, Show)
instance NFData Laureate where
rnf (Laureate a b c d e f g h i j k l) =
rnf a `seq` rnf b `seq` rnf c `seq` rnf d `seq`
rnf e `seq` rnf f `seq` rnf g `seq` rnf h `seq`
rnf i `seq` rnf j `seq` rnf k `seq` rnf l
instance Aeson.FromJSON Laureate where
parseJSON = Aeson.withObject "Laureate" $ \obj -> Laureate
<$> obj Aeson..: "born"
<*> obj Aeson..:? "bornCity"
<*> obj Aeson..:? "bornCountry"
<*> obj Aeson..:? "bornCountryCode"
<*> obj Aeson..: "died"
<*> obj Aeson..:? "diedCity"
<*> obj Aeson..:? "diedCountry"
<*> obj Aeson..:? "diedCountryCode"
<*> obj Aeson..:? "firstname"
<*> obj Aeson..:? "surname"
<*> obj Aeson..: "id"
<*> obj Aeson..: "gender"
instance FromTokens Laureate where
fromTokens = runRecordParser $ pure Laureate
<.:> "born"
<.:?> "bornCity"
<.:?> "bornCountry"
<.:?> "bornCountryCode"
<.:> "died"
<.:?> "diedCity"
<.:?> "diedCountry"
<.:?> "diedCountryCode"
<.:?> "firstname"
<.:?> "surname"
<.:> "id"
<.:> "gender"