@@ -1,5 +1,4 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2−{-# LANGUAGE PatternGuards #-}
3{-# LANGUAGE ScopedTypeVariables #-}
2{-# LANGUAGE ScopedTypeVariables #-}
4{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE OverloadedStrings #-}
5-- | Provides a high-level interface for processing YAML files.
4-- | Provides a high-level interface for processing YAML files.
@@ -53,35 +52,33 @@ module Data.Yaml
53 -- ** More control over decoding
52 -- ** More control over decoding
54 , decodeHelper
53 , decodeHelper
56−
55+#if !MIN_VERSION_base(4,8,0)
57−import qualified Text.Libyaml as Y
56+import Control.Applicative((<$>))
58+import Control.Exception
59+import Control.Monad.Trans.Resource (runResourceT)
58import Data.Aeson
60import Data.Aeson
59 ( Value (..), ToJSON (..), FromJSON (..), object
61 ( Value (..), ToJSON (..), FromJSON (..), object
60 , (.=) , (.:) , (.:?) , (.!=)
62 , (.=) , (.:) , (.:?) , (.!=)
61 , Object, Array
63 , Object, Array
65+import Data.Aeson.Encode (encodeToTextBuilder)
63import Data.Aeson.Types (Pair, parseMaybe, parseEither, Parser)
66import Data.Aeson.Types (Pair, parseMaybe, parseEither, Parser)
64−import Text.Libyaml hiding (encode, decode, encodeFile, decodeFile)
65import Data.ByteString (ByteString)
67import Data.ByteString (ByteString)
66−import System.IO.Unsafe (unsafePerformIO)
67−import Control.Exception
68import qualified Data.Conduit as C
68import qualified Data.Conduit as C
69import qualified Data.Conduit.List as CL
69import qualified Data.Conduit.List as CL
70−import qualified Data.Vector as V
71−import Data.Text.Encoding (encodeUtf8)
72import qualified Data.HashMap.Strict as M
70import qualified Data.HashMap.Strict as M
73import qualified Data.HashSet as HashSet
71import qualified Data.HashSet as HashSet
74−#if MIN_VERSION_aeson(0, 7, 0)
72+import Data.Text.Encoding (encodeUtf8)
75import qualified Data.Text.Encoding as TE
73import qualified Data.Text.Encoding as TE
76import qualified Data.Text.Lazy as TL
74import qualified Data.Text.Lazy as TL
77import Data.Text.Lazy.Builder (toLazyText)
75import Data.Text.Lazy.Builder (toLazyText)
78−import Data.Aeson.Encode (encodeToTextBuilder)
80−import qualified Data.ByteString.Char8 as S8
76+import qualified Data.Vector as V
81−#endif
77+import System.IO.Unsafe (unsafePerformIO)
82−import Control.Monad.Trans.Resource (runResourceT)
84import Data.Yaml.Internal
79import Data.Yaml.Internal
80+import Text.Libyaml hiding (encode, decode, encodeFile, decodeFile)
81+import qualified Text.Libyaml as Y
86encode :: ToJSON a => a -> ByteString
83encode :: ToJSON a => a -> ByteString
87encode obj = unsafePerformIO $
84encode obj = unsafePerformIO $
@@ -110,10 +107,10 @@ objToEvents' :: Value -> [Y.Event] -> [Y.Event]
110--objToEvents' (Scalar s) rest = scalarToEvent s : rest
107--objToEvents' (Scalar s) rest = scalarToEvent s : rest
111objToEvents' (Array list) rest =
108objToEvents' (Array list) rest =
112 EventSequenceStart Nothing
109 EventSequenceStart Nothing
113− : foldr ($) (EventSequenceEnd : rest) (map objToEvents' $ V.toList list)
110+ : foldr objToEvents' (EventSequenceEnd : rest) (V.toList list)
114objToEvents' (Object pairs) rest =
111objToEvents' (Object pairs) rest =
115 EventMappingStart Nothing
112 EventMappingStart Nothing
116− : foldr ($) (EventMappingEnd : rest) (map pairToEvents $ M.toList pairs)
113+ : foldr pairToEvents (EventMappingEnd : rest) (M.toList pairs)
118-- Empty strings need special handling to ensure they get quoted. This avoids:
115-- Empty strings need special handling to ensure they get quoted. This avoids:
119-- https://github.com/snoyberg/yaml/issues/24
116-- https://github.com/snoyberg/yaml/issues/24
@@ -130,12 +127,8 @@ objToEvents' (String s) rest =
130objToEvents' Null rest = EventScalar "null" NullTag PlainNoTag Nothing : rest
127objToEvents' Null rest = EventScalar "null" NullTag PlainNoTag Nothing : rest
131objToEvents' (Bool True) rest = EventScalar "true" BoolTag PlainNoTag Nothing : rest
128objToEvents' (Bool True) rest = EventScalar "true" BoolTag PlainNoTag Nothing : rest
132objToEvents' (Bool False) rest = EventScalar "false" BoolTag PlainNoTag Nothing : rest
129objToEvents' (Bool False) rest = EventScalar "false" BoolTag PlainNoTag Nothing : rest
133−#if MIN_VERSION_aeson(0,7,0)
134-- Use aeson's implementation which gets rid of annoying decimal points
130-- Use aeson's implementation which gets rid of annoying decimal points
135objToEvents' n@Number{} rest = EventScalar (TE.encodeUtf8 $ TL.toStrict $ toLazyText $ encodeToTextBuilder n) IntTag PlainNoTag Nothing : rest
131objToEvents' n@Number{} rest = EventScalar (TE.encodeUtf8 $ TL.toStrict $ toLazyText $ encodeToTextBuilder n) IntTag PlainNoTag Nothing : rest
137−objToEvents' (Number n) rest = EventScalar (S8.pack $ show n) IntTag PlainNoTag Nothing : rest
140pairToEvents :: Pair -> [Y.Event] -> [Y.Event]
133pairToEvents :: Pair -> [Y.Event] -> [Y.Event]
141pairToEvents (k, v) rest =
134pairToEvents (k, v) rest =
@@ -146,8 +139,8 @@ decode :: FromJSON a
146 => ByteString
139 => ByteString
147 -> Maybe a
140 -> Maybe a
148decode bs = unsafePerformIO
141decode bs = unsafePerformIO
149− $ fmap (either (const Nothing) id)
142+ $ either (const Nothing) id
150− $ decodeHelper_ (Y.decode bs)
143+ <$> decodeHelper_ (Y.decode bs)
152decodeFile :: FromJSON a
145decodeFile :: FromJSON a
153 => FilePath
146 => FilePath
@@ -165,8 +158,8 @@ decodeFileEither = decodeHelper_ . Y.decodeFile
166decodeEither :: FromJSON a => ByteString -> Either String a
159decodeEither :: FromJSON a => ByteString -> Either String a
167decodeEither bs = unsafePerformIO
160decodeEither bs = unsafePerformIO
168− $ fmap (either (Left . prettyPrintParseException) id)
161+ $ either (Left . prettyPrintParseException) id
169− $ decodeHelper (Y.decode bs)
162+ <$> decodeHelper (Y.decode bs)
171-- | More helpful version of 'decodeEither' which returns the 'YamlException'.
164-- | More helpful version of 'decodeEither' which returns the 'YamlException'.