Skip to content

Instantly share code, notes, and snippets.

@simg
Last active August 4, 2019 14:54
Show Gist options
  • Save simg/abcb19b2864167a3452cc23a92dc5eec to your computer and use it in GitHub Desktop.
Save simg/abcb19b2864167a3452cc23a92dc5eec to your computer and use it in GitHub Desktop.
Aeson Encode List of Tuples
data Summary = Summary {
count :: Int
, list1 :: Map Text Int
} deriving (Generic, Show)
instance ToJSON Summary where
toJSON (Summary {..}) = object [
"count" .= count
, "list1" .= sortBy (comparing snd) $ toList names :: [(Text, Int)]
]
{-
app/Main.hs:9:7: error:
• Couldn't match type ‘[(Text, Int)]’ with ‘(Text, Value)’
Expected type: aeson-1.4.2.0:Data.Aeson.Types.Internal.Pair
Actual type: [(Text, Int)]
-}
{-
The following worked
-}
instance ToJSON Summary where
toJSON (Summary {..}) = object [
"count" .= count
, "list1" .= toJSON (sortBy (comparing snd) $ toList names :: [(Text, Int)])
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment