on master help sign in
Tag numerics as strings #39
d9b4850Michael Snoyman
Data/Yaml.hs+11−1
@@ -134,7 +134,7 @@ objToEvents' (String s) rest =
134 event
134 event
135 -- Make sure that special strings are encoded as strings properly.
135 -- Make sure that special strings are encoded as strings properly.
136 -- See: https://github.com/snoyberg/yaml/issues/31
136 -- See: https://github.com/snoyberg/yaml/issues/31
137− | s `HashSet.member` specialStrings = EventScalar (encodeUtf8 s) NoTag SingleQuoted Nothing
137+ | s `HashSet.member` specialStrings || isNumeric s = EventScalar (encodeUtf8 s) NoTag SingleQuoted Nothing
138 | otherwise = EventScalar (encodeUtf8 s) StrTag PlainNoTag Nothing
138 | otherwise = EventScalar (encodeUtf8 s) StrTag PlainNoTag Nothing
139objToEvents' Null rest = EventScalar "null" NullTag PlainNoTag Nothing : rest
139objToEvents' Null rest = EventScalar "null" NullTag PlainNoTag Nothing : rest
140objToEvents' (Bool True) rest = EventScalar "true" BoolTag PlainNoTag Nothing : rest
140objToEvents' (Bool True) rest = EventScalar "true" BoolTag PlainNoTag Nothing : rest
@@ -151,6 +151,16 @@ specialStrings :: HashSet.HashSet Text
151specialStrings = HashSet.fromList $ T.words
151specialStrings = HashSet.fromList $ T.words
152 "y Y yes Yes YES n N no No NO true True TRUE false False FALSE on On ON off Off OFF null Null NULL ~"
152 "y Y yes Yes YES n N no No NO true True TRUE false False FALSE on On ON off Off OFF null Null NULL ~"
153
153
154+isNumeric :: Text -> Bool
155+isNumeric =
156+ T.all isNumeric'
157+ where
158+ isNumeric' c = ('0' <= c && c <= '9')
159+ || c == 'e'
160+ || c == 'E'
161+ || c == '.'
162+ || c == '-'
163+
154-- Parsing
164-- Parsing
155
165
156data ParseException = NonScalarKey
166data ParseException = NonScalarKey
test/main.hs+1−1
@@ -124,7 +124,7 @@ main = hspec $ do
124 }
124 }
125
125
126 describe "round-tripping of special scalars" $ do
126 describe "round-tripping of special scalars" $ do
127− let special = words "y Y On ON false"
127+ let special = words "y Y On ON false 12345 12345.0 12345a 12e3"
128 forM_ special $ \w -> it w $
128 forM_ special $ \w -> it w $
129 let v = object ["word" .= w]
129 let v = object ["word" .= w]
130 in D.decode (D.encode v) `shouldBe` Just v
130 in D.decode (D.encode v) `shouldBe` Just v
yaml.cabal+1−1
@@ -1,5 +1,5 @@
1name: yaml
1name: yaml
2−version: 0.8.6
2+version: 0.8.6.1
3license: BSD3
3license: BSD3
4license-file: LICENSE
4license-file: LICENSE
5author: Michael Snoyman <michael@snoyman.com>, Anton Ageev <antage@gmail.com>,Kirill Simonov
5author: Michael Snoyman <michael@snoyman.com>, Anton Ageev <antage@gmail.com>,Kirill Simonov