I am able to json-encode my data
import Data.Aeson (ToJSON, toJSON, (.=), object)
import qualified Data.Text as T
import qualified Data.Text.Lazy as L
data ServiceResponse = ReadServiceResponse L.Text
| GenericServiceError Int L.Text
instance ToJSON ServiceResponse where
toJSON (ReadServiceResponse text) = object ["text" .= text]
toJSON (GenericServiceError code text) =
object ["code" .= code, "message" .= text]
For data having only one "scalar" value (e.g. String, Int, L.Text, ...) I would like to get a scalar representation instead of an object. For example the ReadServiceResponse should be encoded into a json string instead of an object like
{
text: "hi I'm some text."
}
I tried
instance ToJSON ServiceResponse where
toJSON (ReadServiceResponse text) = text
which does not compile
• Couldn't match expected type ‘aeson-1.3.1.1:Data.Aeson.Types.Internal.Value’ with actual type ‘L.Text’
Should I transform text into a Data.Aeson.Types.Internal.Value (How can i do that)? Thanks in advance for any help