--------------------------------------------------------- -- -- Module : Data.Object.Instances -- Copyright : Michael Snoyman -- License : BSD3 -- -- Maintainer : Michael Snoyman -- Stability : Stable -- Portability : portable -- -- Instances for converting various types of data into Data.Object.Object. -- --------------------------------------------------------- module Data.Object.Instances ( Json (..) , Yaml (..) , Html (..) ) where import Data.Object import qualified Data.ByteString as B import Data.ByteString.Class import Web.Encodings (encodeJson) import qualified Text.Yaml as Y newtype Json = Json B.ByteString instance FromObject Json where fromObject = return . Json . helper where helper :: Object -> B.ByteString helper (Scalar s) = B.concat [ toStrictByteString "\"" , encodeJson $ fromStrictByteString s , toStrictByteString "\"" ] helper (Sequence s) = B.concat [ toStrictByteString "[" , B.intercalate (toStrictByteString ",") $ map helper s , toStrictByteString "]" ] helper (Mapping m) = B.concat [ toStrictByteString "{" , B.intercalate (toStrictByteString ",") $ map helper2 m , toStrictByteString "}" ] helper2 :: (B.ByteString, Object) -> B.ByteString helper2 (k, v) = B.concat [ toStrictByteString "\"" , encodeJson $ fromStrictByteString k , toStrictByteString "\":" , helper v ] newtype Yaml = Yaml B.ByteString instance FromObject Yaml where fromObject = return . Yaml . Y.encode -- | Represents as an entire HTML 5 document by using the following: -- -- * A scalar is a paragraph. -- * A sequence is an unordered list. -- * A mapping is a definition list. newtype Html = Html B.ByteString instance FromObject Html where fromObject o = return $ Html $ B.concat [ toStrictByteString "\n" , helper o , toStrictByteString "" ] where helper :: Object -> B.ByteString helper (Scalar s) = B.concat [ toStrictByteString "

" , s , toStrictByteString "

" ] helper (Sequence []) = toStrictByteString "" helper (Sequence s) = B.concat [ toStrictByteString "" ] helper (Mapping m) = B.concat $ toStrictByteString "
" : map helper2 m ++ [ toStrictByteString "
" ] helper2 :: (B.ByteString, Object) -> B.ByteString helper2 (k, v) = B.concat $ [ toStrictByteString "
" , k , toStrictByteString "
" , helper v , toStrictByteString "
" ]