So, to make out everything (since most of the work has already been completed). You actually have two problems:
- You mix string types
- You do not have an instance of
ToJSON declared for Cat
Here is a working example based on the latest versions of aeson and text (for me it is aeson-1.0.0.0 and text-1.2.2.1 .
{-# LANGUAGE OverloadedStrings, DeriveGeneric, DeriveAnyClass #-} import GHC.Generics import Data.Text.Lazy (Text) import Data.Text.Lazy.IO as I import Data.Aeson.Text (encodeToLazyText) import Data.Aeson (ToJSON) data Cat = Cat { name :: Text, age :: Int } deriving (Show, Generic, ToJSON) meowmers = Cat { name = "meowmers", age = 1 } main = I.writeFile "myfile.json" (encodeToLazyText meowmers)
As you can probably tell from the import, I rely on aeson to convert between string types via encodeToLazyText . This concerns issue number 1.
Then I use the DeriveGeneric language DeriveGeneric to get a Generic instance for Cat , and use it in conjunction with the DeriveAnyClass extension to get a ToJSON instance for Cat . The magic of this instance is again part of aeson .
By running this, I get a new myfile.json file containing {"age":1,"name":"meowmers"} in it.
source share