Created
December 2, 2011 20:53
-
-
Save raimohanska/1424794 to your computer and use it in GitHub Desktop.
Data.Aeson.Generic examples with Strings
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
{-# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.