on master help sign in
Improve docs, and use <> instead of mappend.
commitd4760e48b52e95996912b86d48986ddc47c6cc7d authorBryan O'Sullivan <bos@serpentine.com> dateFri 23 Dec 2011, 13:48 (15 years ago) parent95d6a6a
1 file changed+25−17
Data/Aeson/Encode.hs+25−17
Data/Aeson/Encode.hs+25−17
@@ -8,8 +8,13 @@
88-- Stability: experimental
99-- Portability: portable
1010--
11−-- Efficiently serialize a JSON value as a lazy 'L.ByteString',
12−-- encoded as UTF-8.
11+-- Efficiently serialize a JSON value.
12+--
13+-- Most frequently, you'll probably want to encode straight to UTF-8
14+-- (the standard JSON encoding) using 'encode'.
15+--
16+-- You can convert a 'Builder' (as returned by 'fromValue') to a
17+-- string using e.g. 'toLazyText'.
1318
1419module Data.Aeson.Encode
1520 (
@@ -30,36 +35,35 @@ import qualified Data.HashMap.Strict as H
3035import qualified Data.Text as T
3136import qualified Data.Vector as V
3237
33−-- | Encode a JSON value to a 'Builder'.
38+-- | Encode a JSON value to a 'Builder'. You can convert this to a
39+-- string using e.g. 'toLazyText', or encode straight to UTF-8 (the
40+-- standard JSON encoding) using 'encode'.
3441fromValue :: Value -> Builder
3542fromValue Null = {-# SCC "fromValue/Null" #-} "null"
3643fromValue (Bool b) = {-# SCC "fromValue/Bool" #-}
37− if b then "true"
38− else "false"
44+ if b then "true" else "false"
3945fromValue (Number n) = {-# SCC "fromValue/Number" #-} fromNumber n
4046fromValue (String s) = {-# SCC "fromValue/String" #-} string s
4147fromValue (Array v)
4248 | V.null v = {-# SCC "fromValue/Array" #-} "[]"
4349 | otherwise = {-# SCC "fromValue/Array" #-}
44− singleton '[' `mappend`
45− fromValue (V.unsafeHead v) `mappend`
50+ singleton '[' <>
51+ fromValue (V.unsafeHead v) <>
4652 V.foldr f (singleton ']') (V.unsafeTail v)
47− where f a z = singleton ',' `mappend` fromValue a `mappend` z
53+ where f a z = singleton ',' <> fromValue a <> z
4854fromValue (Object m) = {-# SCC "fromValue/Object" #-}
4955 case H.toList m of
50− (x:xs) -> singleton '{' `mappend`
51− one x `mappend` foldr f (singleton '}') xs
56+ (x:xs) -> singleton '{' <> one x <> foldr f (singleton '}') xs
5257 _ -> "{}"
53− where f a z = singleton ',' `mappend` one a `mappend` z
54− one (k,v) = string k `mappend` singleton ':' `mappend` fromValue v
58+ where f a z = singleton ',' <> one a <> z
59+ one (k,v) = string k <> singleton ':' <> fromValue v
5560
5661string :: T.Text -> Builder
57−string s = {-# SCC "string" #-}
58− singleton '"' `mappend` quote s `mappend` singleton '"'
62+string s = {-# SCC "string" #-} singleton '"' <> quote s <> singleton '"'
5963 where
6064 quote q = case T.uncons t of
61− Just (c,t') -> fromText h `mappend` escape c `mappend` quote t'
6265 Nothing -> fromText h
66+ Just (c,t') -> fromText h <> escape c <> quote t'
6367 where (h,t) = {-# SCC "break" #-} T.break isEscape q
6468 isEscape c = c == '\"' || c == '\\' || c < '\x20'
6569 escape '\"' = "\\\""
@@ -68,8 +72,7 @@ string s = {-# SCC "string" #-}
6872 escape '\r' = "\\r"
6973 escape '\t' = "\\t"
7074 escape c
71− | c < '\x20' = fromString $
72− "\\u" ++ replicate (4 - length h) '0' ++ h
75+ | c < '\x20' = fromString $ "\\u" ++ replicate (4 - length h) '0' ++ h
7376 | otherwise = singleton c
7477 where h = showHex (fromEnum c) ""
7578
@@ -84,3 +87,8 @@ encode :: ToJSON a => a -> L.ByteString
8487encode = {-# SCC "encode" #-} encodeUtf8 . toLazyText . fromValue .
8588 {-# SCC "toJSON" #-} toJSON
8689{-# INLINE encode #-}
90+
91+(<>) :: Builder -> Builder -> Builder
92+(<>) = mappend
93+{-# INLINE (<>) #-}
94+infixr 6 <>