{-# LANGUAGE CPP #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
#if (defined (ghcjs_HOST_OS))
module Data.Yaml {-# WARNING "GHCJS is not supported yet (will break at runtime once called)." #-}
#else
module Data.Yaml
#endif
(
encode
, encodeWith
, encodeFile
, encodeFileWith
, decodeEither'
, decodeFileEither
, decodeFileWithWarnings
, decodeThrow
, decodeFileThrow
, decodeAllEither'
, decodeAllFileEither
, decodeAllFileWithWarnings
, decodeAllThrow
, decodeAllFileThrow
, decodeHelper
, Value (..)
, Parser
, Object
, Array
, ParseException(..)
, prettyPrintParseException
, YamlException (..)
, YamlMark (..)
, object
, array
, (.=)
, (.:)
, (.:?)
, (.!=)
, withObject
, withText
, withArray
, withScientific
, withBool
, parseMonad
, parseEither
, parseMaybe
, ToJSON (..)
, FromJSON (..)
, isSpecialString
, EncodeOptions
, defaultEncodeOptions
, defaultStringStyle
, setStringStyle
, setFormat
, FormatOptions
, defaultFormatOptions
, setWidth
) where
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative((<$>))
#endif
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Aeson
( Value (..), ToJSON (..), FromJSON (..), object
, (.=) , (.:) , (.:?) , (.!=)
, Object, Array
, withObject, withText, withArray, withScientific, withBool
)
import Data.Aeson.Types (parseMaybe, parseEither, Parser)
import Data.ByteString (ByteString)
import qualified Data.Vector as V
import System.IO.Unsafe (unsafePerformIO)
import Data.Text (Text)
import Data.Yaml.Internal
import Text.Libyaml hiding (encode, decode, encodeFile, decodeFile, encodeWith, encodeFileWith)
import qualified Text.Libyaml as Y
import Control.Exception.Safe
import qualified Streamly.Prelude as S
setStringStyle :: (Text -> ( Tag, Style )) -> EncodeOptions -> EncodeOptions
setStringStyle :: (Text -> (Tag, Style)) -> EncodeOptions -> EncodeOptions
setStringStyle Text -> (Tag, Style)
s EncodeOptions
opts = EncodeOptions
opts { encodeOptionsStringStyle :: Text -> (Tag, Style)
encodeOptionsStringStyle = Text -> (Tag, Style)
s }
setFormat :: FormatOptions -> EncodeOptions -> EncodeOptions
setFormat :: FormatOptions -> EncodeOptions -> EncodeOptions
setFormat FormatOptions
f EncodeOptions
opts = EncodeOptions
opts { encodeOptionsFormat :: FormatOptions
encodeOptionsFormat = FormatOptions
f }
data EncodeOptions = EncodeOptions
{ EncodeOptions -> Text -> (Tag, Style)
encodeOptionsStringStyle :: Text -> ( Tag, Style )
, EncodeOptions -> FormatOptions
encodeOptionsFormat :: FormatOptions
}
defaultEncodeOptions :: EncodeOptions
defaultEncodeOptions :: EncodeOptions
defaultEncodeOptions = EncodeOptions
{ encodeOptionsStringStyle :: Text -> (Tag, Style)
encodeOptionsStringStyle = Text -> (Tag, Style)
defaultStringStyle
, encodeOptionsFormat :: FormatOptions
encodeOptionsFormat = FormatOptions
defaultFormatOptions
}
encode :: ToJSON a => a -> ByteString
encode :: forall a. ToJSON a => a -> ByteString
encode = forall a. ToJSON a => EncodeOptions -> a -> ByteString
encodeWith EncodeOptions
defaultEncodeOptions
encodeWith :: ToJSON a => EncodeOptions -> a -> ByteString
encodeWith :: forall a. ToJSON a => EncodeOptions -> a -> ByteString
encodeWith EncodeOptions
opts a
obj = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
(MonadCatch m, MonadAsync m, MonadMask m) =>
FormatOptions -> SerialT m Event -> m ByteString
Y.encodeWith (EncodeOptions -> FormatOptions
encodeOptionsFormat EncodeOptions
opts) (forall (m :: * -> *) (t :: (* -> *) -> * -> *) a.
(Monad m, IsStream t) =>
[a] -> t m a
S.fromList forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => (Text -> (Tag, Style)) -> a -> [Event]
objToStream (EncodeOptions -> Text -> (Tag, Style)
encodeOptionsStringStyle EncodeOptions
opts) forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => a -> Value
toJSON a
obj)
encodeFile :: ToJSON a => FilePath -> a -> IO ()
encodeFile :: forall a. ToJSON a => FilePath -> a -> IO ()
encodeFile = forall a. ToJSON a => EncodeOptions -> FilePath -> a -> IO ()
encodeFileWith EncodeOptions
defaultEncodeOptions
encodeFileWith :: ToJSON a => EncodeOptions -> FilePath -> a -> IO ()
encodeFileWith :: forall a. ToJSON a => EncodeOptions -> FilePath -> a -> IO ()
encodeFileWith EncodeOptions
opts FilePath
fp a
obj = forall (m :: * -> *).
(MonadCatch m, MonadAsync m, MonadMask m) =>
FormatOptions -> FilePath -> SerialT m Event -> m ()
Y.encodeFileWith (EncodeOptions -> FormatOptions
encodeOptionsFormat EncodeOptions
opts) FilePath
fp (forall (m :: * -> *) (t :: (* -> *) -> * -> *) a.
(Monad m, IsStream t) =>
[a] -> t m a
S.fromList forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => (Text -> (Tag, Style)) -> a -> [Event]
objToStream (EncodeOptions -> Text -> (Tag, Style)
encodeOptionsStringStyle EncodeOptions
opts) forall a b. (a -> b) -> a -> b
$ forall a. ToJSON a => a -> Value
toJSON a
obj)
decodeFileEither
:: FromJSON a
=> FilePath
-> IO (Either ParseException a)
decodeFileEither :: forall a. FromJSON a => FilePath -> IO (Either ParseException a)
decodeFileEither = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a.
FromJSON a =>
FilePath -> IO (Either ParseException ([Warning], a))
decodeFileWithWarnings
decodeAllFileEither
:: FromJSON a
=> FilePath
-> IO (Either ParseException [a])
decodeAllFileEither :: forall a. FromJSON a => FilePath -> IO (Either ParseException [a])
decodeAllFileEither = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a.
FromJSON a =>
FilePath -> IO (Either ParseException ([Warning], [a]))
decodeAllFileWithWarnings
decodeFileWithWarnings
:: FromJSON a
=> FilePath
-> IO (Either ParseException ([Warning], a))
decodeFileWithWarnings :: forall a.
FromJSON a =>
FilePath -> IO (Either ParseException ([Warning], a))
decodeFileWithWarnings = forall a.
FromJSON a =>
SerialT IO Event -> IO (Either ParseException ([Warning], a))
decodeHelper_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
(MonadCatch m, MonadAsync m, MonadMask m) =>
FilePath -> SerialT m Event
Y.decodeFile
decodeAllFileWithWarnings
:: FromJSON a
=> FilePath
-> IO (Either ParseException ([Warning], [a]))
decodeAllFileWithWarnings :: forall a.
FromJSON a =>
FilePath -> IO (Either ParseException ([Warning], [a]))
decodeAllFileWithWarnings = forall a.
FromJSON a =>
SerialT IO Event -> IO (Either ParseException ([Warning], [a]))
decodeAllHelper_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
(MonadCatch m, MonadAsync m, MonadMask m) =>
FilePath -> SerialT m Event
Y.decodeFile
decodeEither' :: FromJSON a => ByteString -> Either ParseException a
decodeEither' :: forall a. FromJSON a => ByteString -> Either ParseException a
decodeEither' = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a b. a -> Either a b
Left (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> Either a b
Left forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> ParseException
AesonException) forall a b. b -> Either a b
Right)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IO a -> a
unsafePerformIO
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a.
FromJSON a =>
SerialT IO Event
-> IO (Either ParseException ([Warning], Either FilePath a))
decodeHelper
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
(MonadCatch m, MonadAsync m, MonadMask m) =>
ByteString -> SerialT m Event
Y.decode
decodeAllEither' :: FromJSON a => ByteString -> Either ParseException [a]
decodeAllEither' :: forall a. FromJSON a => ByteString -> Either ParseException [a]
decodeAllEither' = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a b. a -> Either a b
Left (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> Either a b
Left forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> ParseException
AesonException) forall a b. b -> Either a b
Right)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IO a -> a
unsafePerformIO
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a.
FromJSON a =>
SerialT IO Event
-> IO (Either ParseException ([Warning], Either FilePath [a]))
decodeAllHelper
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
(MonadCatch m, MonadAsync m, MonadMask m) =>
ByteString -> SerialT m Event
Y.decode
decodeThrow :: (MonadThrow m, FromJSON a) => ByteString -> m a
decodeThrow :: forall (m :: * -> *) a.
(MonadThrow m, FromJSON a) =>
ByteString -> m a
decodeThrow = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. FromJSON a => ByteString -> Either ParseException a
decodeEither'
decodeAllThrow :: (MonadThrow m, FromJSON a) => ByteString -> m [a]
decodeAllThrow :: forall (m :: * -> *) a.
(MonadThrow m, FromJSON a) =>
ByteString -> m [a]
decodeAllThrow = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. FromJSON a => ByteString -> Either ParseException [a]
decodeAllEither'
decodeFileThrow :: (MonadIO m, FromJSON a) => FilePath -> m a
decodeFileThrow :: forall (m :: * -> *) a. (MonadIO m, FromJSON a) => FilePath -> m a
decodeFileThrow FilePath
f = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. FromJSON a => FilePath -> IO (Either ParseException a)
decodeFileEither FilePath
f forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwIO forall (m :: * -> *) a. Monad m => a -> m a
return
decodeAllFileThrow :: (MonadIO m, FromJSON a) => FilePath -> m [a]
decodeAllFileThrow :: forall (m :: * -> *) a.
(MonadIO m, FromJSON a) =>
FilePath -> m [a]
decodeAllFileThrow FilePath
f = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. FromJSON a => FilePath -> IO (Either ParseException [a])
decodeAllFileEither FilePath
f forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwIO forall (m :: * -> *) a. Monad m => a -> m a
return
array :: [Value] -> Value
array :: [Value] -> Value
array = Array -> Value
Array forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> Vector a
V.fromList
#if MIN_VERSION_base(4, 13, 0)
parseMonad :: MonadFail m => (a -> Parser b) -> a -> m b
#else
parseMonad :: Monad m => (a -> Parser b) -> a -> m b
#endif
parseMonad :: forall (m :: * -> *) a b.
MonadFail m =>
(a -> Parser b) -> a -> m b
parseMonad a -> Parser b
p = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall (m :: * -> *) a. MonadFail m => FilePath -> m a
fail forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> Parser b) -> a -> Either FilePath b
parseEither a -> Parser b
p
{-# DEPRECATED parseMonad "With the MonadFail split, this function is going to be removed in the future. Please migrate to parseEither." #-}