You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.3 KiB
53 lines
1.3 KiB
7 years ago
|
{-# LANGUAGE OverloadedStrings #-}
|
||
|
{-# LANGUAGE QuasiQuotes #-}
|
||
|
|
||
|
module Marshalling where
|
||
|
|
||
|
import Control.Applicative
|
||
|
import Data.Aeson
|
||
|
import Data.ByteString.Lazy (ByteString)
|
||
|
import qualified Data.Text as T
|
||
|
import Data.Text (Text)
|
||
|
import Text.RawString.QQ
|
||
|
|
||
|
sectionJson :: ByteString
|
||
|
sectionJson = [r|
|
||
|
{ "section": {"host": "wikipedia.org"},
|
||
|
"whatisit": {"red": "intoothandclaw"}
|
||
|
}
|
||
|
|]
|
||
|
|
||
|
data TestData = TestData { section :: Host, what :: Color } deriving (Eq, Show)
|
||
|
|
||
|
instance FromJSON TestData where
|
||
|
parseJSON (Object v) =
|
||
|
TestData <$> v .: "section"
|
||
|
<*> v .: "whatisit"
|
||
|
parseJSON _ = fail "Expected an object for TestData"
|
||
|
|
||
|
newtype Host = Host String deriving (Eq, Show)
|
||
|
|
||
|
instance FromJSON Host where
|
||
|
parseJSON (Object v) =
|
||
|
Host <$> v .: "host"
|
||
|
parseJSON _ = fail "Expected an object for Host"
|
||
|
|
||
|
type Annotation = String
|
||
|
|
||
|
data Color = Red Annotation
|
||
|
| Blue Annotation
|
||
|
| Yellow Annotation
|
||
|
deriving (Eq, Show)
|
||
|
|
||
|
instance FromJSON Color where
|
||
|
parseJSON (Object v) =
|
||
|
(Red <$> v .: "red") <|>
|
||
|
(Blue <$> v .: "blue") <|>
|
||
|
(Yellow <$> v .: "yellow")
|
||
|
parseJSON _ = fail "Expected an object for Color"
|
||
|
|
||
|
main :: IO ()
|
||
|
main = do
|
||
|
let d :: Maybe TestData
|
||
|
d = decode sectionJson
|
||
|
print d
|