I am trying to encode a data type into JSON:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
import Data.Aeson
data Trend = Trend
{ period :: String
, africa :: String
, americas :: String
, asia :: String
} deriving Show
instance ToJSON Trend where
toJSON Trend{..} =
object [ "Period" .= period
, "Africa" .= africa
, "Americas" .= americas
, "Asia" .= asia
]
test = Trend {period = "2013", africa = "1", americas = "2", asia = "3"}
Which gives:
λ: encode test
λ: "{\"Asia\":\"3\",\"Period\":\"2013\",\"Africa\":\"1\",\"Americas\":\"2\"}"
I don't understand why the generated JSON does not have the fields in the same order as my data type.
I am expecting the output to be {period, africa, americas, asia} and I am getting {asia, period, africa, americas)
I understand that in passing information across, the order is not important but I am curious as to why this is happening.