Skip to content

Instantly share code, notes, and snippets.

@raimohanska
Created December 2, 2011 20:53
Show Gist options
  • Save raimohanska/1424794 to your computer and use it in GitHub Desktop.
Save raimohanska/1424794 to your computer and use it in GitHub Desktop.
Data.Aeson.Generic examples with Strings
{-# LANGUAGE OverloadedStrings, DeriveDataTypeable, NoMonomorphismRestriction #-}
module AesonTest where
import qualified Data.Aeson.Generic as A
import qualified Data.ByteString.Lazy as L8
import Data.Data
import Data.Typeable
import Codec.Binary.UTF8.String as U8
import Data.Maybe
data Event = Event { kind :: String } deriving (Data, Typeable, Show)
asJsonString = U8.decode . L8.unpack . A.encode
fromJsonString = fromJust . A.decode . L8.pack . U8.encode
-------------------------------------------------
asJsonStringExample = asJsonString $ Event "lol"
fromJsonStringExample = fromJsonString "{\"kind\" : \"wut\"}" :: Event
@raimohanska
Copy link
Author

It's a (good) design choice that Aeson works with ByteStrings instead of Strings. However, Strings are a lot easier to experiment with in GHCI, so I wrote some example code for encoding/decoding POHO's (Plain Old Haskell Objects, LOL!) to JSON using the Aeson library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment