{-# LANGUAGE FlexibleInstances #-} --------------------------------------------------------- -- -- 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 (..) , SafeFromObject (..) ) where import Data.Object import qualified Data.ByteString as B import Data.ByteString.Class import Web.Encodings (encodeJson) import qualified Text.Yaml as Y class SafeFromObject a where safeFromObject :: Object -> a newtype Json = Json { unJson :: B.ByteString } instance SafeFromObject Json where safeFromObject = 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 { unYaml :: B.ByteString } instance SafeFromObject Yaml where safeFromObject = 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 { unHtml :: B.ByteString } instance SafeFromObject Html where safeFromObject o = 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 "
" ]