on master help sign in
Quote keys as necessary (fixes #137)
5648c22Michael Snoyman
ChangeLog.md+2−1
@@ -1,7 +1,8 @@
1# ChangeLog for yaml
1# ChangeLog for yaml
2
2
3−## Unreleased changes
3+## 0.8.32
4
4
5+* Escape keys as necessary [#137](https://github.com/snoyberg/yaml/issues/137)
5* hpackify
6* hpackify
6* src subdir
7* src subdir
7
8
package.yaml+1−1
@@ -1,5 +1,5 @@
1name: yaml
1name: yaml
2−version: 0.8.31.1
2+version: 0.8.32
3synopsis: Support for parsing and rendering YAML documents.
3synopsis: Support for parsing and rendering YAML documents.
4description: README and API documentation are available at <https://www.stackage.org/package/yaml>
4description: README and API documentation are available at <https://www.stackage.org/package/yaml>
5category: Data
5category: Data
src/Data/Yaml.hs+1−3
@@ -157,9 +157,7 @@ objToEvents' (Bool False) rest = EventScalar "false" BoolTag PlainNoTag Nothing
157objToEvents' n@Number{} rest = EventScalar (TE.encodeUtf8 $ TL.toStrict $ toLazyText $ encodeToTextBuilder n) IntTag PlainNoTag Nothing : rest
157objToEvents' n@Number{} rest = EventScalar (TE.encodeUtf8 $ TL.toStrict $ toLazyText $ encodeToTextBuilder n) IntTag PlainNoTag Nothing : rest
158
158
159pairToEvents :: Pair -> [Y.Event] -> [Y.Event]
159pairToEvents :: Pair -> [Y.Event] -> [Y.Event]
160−pairToEvents (k, v) rest =
160+pairToEvents (k, v) = objToEvents' (String k) . objToEvents' v
161− EventScalar (encodeUtf8 k) StrTag PlainNoTag Nothing
162− : objToEvents' v rest
163
161
164decode :: FromJSON a
162decode :: FromJSON a
165 => ByteString
163 => ByteString
test/Data/YamlSpec.hs+15
@@ -3,6 +3,7 @@
3{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE ScopedTypeVariables #-}
4{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE OverloadedStrings #-}
5{-# LANGUAGE TemplateHaskell #-}
5{-# LANGUAGE TemplateHaskell #-}
6+{-# LANGUAGE TupleSections #-}
6
7
7module Data.YamlSpec (main, spec) where
8module Data.YamlSpec (main, spec) where
8
9
@@ -29,6 +30,7 @@ import qualified Data.HashMap.Strict as M
29import qualified Data.Text as T
30import qualified Data.Text as T
30import Data.Aeson.TH
31import Data.Aeson.TH
31import Data.Text (Text)
32import Data.Text (Text)
33+import Data.Text.Encoding (decodeUtf8)
32import Data.Vector (Vector)
34import Data.Vector (Vector)
33import qualified Data.Vector as V
35import qualified Data.Vector as V
34import Data.HashMap.Strict (HashMap)
36import Data.HashMap.Strict (HashMap)
@@ -166,6 +168,19 @@ spec = do
166
168
167 it "truncates files" caseTruncatesFiles
169 it "truncates files" caseTruncatesFiles
168
170
171+ it "quoting keys #137" $ do
172+ let keys = T.words "true false NO YES 1.2 1e5 null"
173+ bs = D.encode $ M.fromList $ map (, ()) keys
174+ text = decodeUtf8 bs
175+ forM_ keys $ \key -> do
176+ let quoted = T.concat ["'", key, "'"]
177+ unless (quoted `T.isInfixOf` text) $ error $ concat
178+ [ "Could not find quoted key: "
179+ , T.unpack quoted
180+ , "\n\n"
181+ , T.unpack text
182+ ] :: IO ()
183+
169
184
170specialStrings :: [T.Text]
185specialStrings :: [T.Text]
171specialStrings =
186specialStrings =