Last active
August 4, 2019 14:54
-
-
Save simg/abcb19b2864167a3452cc23a92dc5eec to your computer and use it in GitHub Desktop.
Aeson Encode List of Tuples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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