@@ -1,5 +1,3 @@
1−{-# LANGUAGE BangPatterns, OverloadedStrings #-}
4-- Module: Data.Aeson.Encode
2-- Module: Data.Aeson.Encode
5-- Copyright: (c) 2012-2016 Bryan O'Sullivan
3-- Copyright: (c) 2012-2016 Bryan O'Sullivan
@@ -9,102 +7,22 @@
9-- Stability: experimental
7-- Stability: experimental
10-- Portability: portable
8-- Portability: portable
12−-- Efficiently serialize a JSON value.
10+-- This module is left to supply limited backwards-compatibility.
14−-- Most frequently, you'll probably want to encode straight to UTF-8
15−-- (the standard JSON encoding) using 'encode'.
17−-- You can use the conversions to 'Builder's when embedding JSON messages as
18−-- parts of a protocol.
19−module Data.Aeson.Encode
11+module Data.Aeson.Encode {-# DEPRECATED "Use Data.Aeson or Data.Aeson.Text instead" #-}
22− -- * Encoding to Builders
24 , encodeToTextBuilder
13 , encodeToTextBuilder
30−import Data.Aeson.Encode.Builder (encodeToBuilder)
16+import Data.ByteString.Lazy (ByteString)
31−import Data.Aeson.Types (Value(..), ToJSON (..))
32−import Data.Aeson.Encoding (fromEncoding)
33−import Data.Monoid ((<>))
34−import Data.Scientific (FPFormat(..), Scientific, base10Exponent)
35−import Data.Text.Lazy.Builder
17+import Data.Text.Lazy.Builder (Builder)
36−import Data.Text.Lazy.Builder.Scientific (formatScientificBuilder)
38−import Numeric (showHex)
39−import qualified Data.ByteString.Builder as B
40−import qualified Data.HashMap.Strict as H
41−import qualified Data.Text as T
42−import qualified Data.Vector as V
43−import qualified Data.ByteString.Lazy as L
45−-- | Efficiently serialize a JSON value as a lazy 'L.ByteString'.
47−-- This is implemented in terms of the 'ToJSON' class's 'toEncoding' method.
48−encode :: ToJSON a => a -> L.ByteString
49−encode = B.toLazyByteString . fromEncoding . toEncoding
52−-- | Encode a JSON 'Value' to a "Data.Text" 'Builder', which can be
53−-- embedded efficiently in a text-based protocol.
55−-- If you are going to immediately encode straight to a
56−-- 'L.ByteString', it is more efficient to use 'encodeToBuilder'
58−encodeToTextBuilder :: Value -> Builder
62− go Null = {-# SCC "go/Null" #-} "null"
63− go (Bool b) = {-# SCC "go/Bool" #-} if b then "true" else "false"
64− go (Number s) = {-# SCC "go/Number" #-} fromScientific s
65− go (String s) = {-# SCC "go/String" #-} string s
67− | V.null v = {-# SCC "go/Array" #-} "[]"
68− | otherwise = {-# SCC "go/Array" #-}
70− go (V.unsafeHead v) <>
71− V.foldr f (singleton ']') (V.unsafeTail v)
72− where f a z = singleton ',' <> go a <> z
73− go (Object m) = {-# SCC "go/Object" #-}
75− (x:xs) -> singleton '{' <> one x <> foldr f (singleton '}') xs
77− where f a z = singleton ',' <> one a <> z
78− one (k,v) = string k <> singleton ':' <> go v
80−{-# DEPRECATED fromValue "Use 'encodeToTextBuilder' instead" #-}
81−fromValue :: Value -> Builder
82−fromValue = encodeToTextBuilder
84−string :: T.Text -> Builder
19+import qualified Data.Aeson as A
85−string s = {-# SCC "string" #-} singleton '"' <> quote s <> singleton '"'
20+import qualified Data.Aeson.Text as A
87− quote q = case T.uncons t of
88− Nothing -> fromText h
89− Just (!c,t') -> fromText h <> escape c <> quote t'
90− where (h,t) = {-# SCC "break" #-} T.break isEscape q
91− isEscape c = c == '\"' ||
100− escape c
22+encode :: A.ToJSON a => a -> ByteString
101− | c < '\x20' = fromString $ "\\u" ++ replicate (4 - length h) '0' ++ h
23+encode = A.encode
102− | otherwise = singleton c
24+{-# DEPRECATED encode "Use encode from Data.Aeson" #-}
103− where h = showHex (fromEnum c) ""
105−fromScientific :: Scientific -> Builder
26+encodeToTextBuilder :: A.Value -> Builder
106−fromScientific s = formatScientificBuilder format prec s
27+encodeToTextBuilder = A.encodeToTextBuilder
107− where
28+{-# DEPRECATED encodeToTextBuilder "Use encodeTotextBuilder from Data.Aeson.Text" #-}
109− | base10Exponent s < 0 = (Generic, Nothing)
110− | otherwise = (Fixed, Just 0)