@@ -106,6 +106,7 @@ import qualified Data.Conduit.List as CL
106import qualified Data.HashMap.Strict as M
106import qualified Data.HashMap.Strict as M
107import qualified Data.HashSet as HashSet
107import qualified Data.HashSet as HashSet
108import Data.Text.Encoding (encodeUtf8)
108import Data.Text.Encoding (encodeUtf8)
109+import qualified Data.Text as T
109import qualified Data.Text.Encoding as TE
110import qualified Data.Text.Encoding as TE
110import qualified Data.Text.Lazy as TL
111import qualified Data.Text.Lazy as TL
111import Data.Text.Lazy.Builder (toLazyText)
112import Data.Text.Lazy.Builder (toLazyText)
@@ -130,7 +131,13 @@ data EncodeOptions = EncodeOptions
130-- __WARNING__: You must ensure that special strings (like @"yes"@\/@"no"@\/@"null"@\/@"1234"@) are not encoded with the 'Plain' style, because
131-- __WARNING__: You must ensure that special strings (like @"yes"@\/@"no"@\/@"null"@\/@"1234"@) are not encoded with the 'Plain' style, because
131-- then they will be decoded as boolean, null or numeric values. You can use 'isSpecialString' to detect them.
132-- then they will be decoded as boolean, null or numeric values. You can use 'isSpecialString' to detect them.
133−-- By default, strings are encoded with the `Plain` style, except special strings, which are encoded with `SingleQuoted`.
134+-- By default, strings are encoded as follows:
136+-- * Any string containing a newline character uses the 'Literal' style
138+-- * Otherwise, any special string (see 'isSpecialString') uses 'SingleQuoted'
140+-- * Otherwise, use 'Plain'
135-- @since 0.10.2.0
142-- @since 0.10.2.0
136setStringStyle :: (Text -> ( Tag, Style )) -> EncodeOptions -> EncodeOptions
143setStringStyle :: (Text -> ( Tag, Style )) -> EncodeOptions -> EncodeOptions
@@ -156,9 +163,11 @@ defaultEncodeOptions = EncodeOptions
156 { encodeOptionsStringStyle = \s ->
163 { encodeOptionsStringStyle = \s ->
157 -- Empty strings need special handling to ensure they get quoted. This avoids:
164 -- Empty strings need special handling to ensure they get quoted. This avoids:
158 -- https://github.com/snoyberg/yaml/issues/24
165 -- https://github.com/snoyberg/yaml/issues/24
168+ | "\n" `T.isInfixOf` s -> ( NoTag, Literal )
159− if isSpecialString s
169+ | isSpecialString s -> ( NoTag, SingleQuoted )
160− then ( NoTag, SingleQuoted )
161− else ( StrTag, PlainNoTag )
170+ | otherwise -> ( StrTag, PlainNoTag )
162 , encodeOptionsFormat = defaultFormatOptions
171 , encodeOptionsFormat = defaultFormatOptions