@@ -16,14 +16,18 @@
17module Data.Aeson.Generic
17module Data.Aeson.Generic
19+ -- * Decoding and encoding
22+ -- * Lower-level conversion functions
19− fromJSON
23+ , fromJSON
23import Control.Applicative ((<$>))
27import Control.Applicative ((<$>))
24import Control.Arrow (first)
28import Control.Arrow (first)
25import Control.Monad.State.Strict
29import Control.Monad.State.Strict
26−import Data.Aeson.Functions
30+import Data.Aeson.Functions hiding (decode)
27import Data.Aeson.Types hiding (FromJSON(..), ToJSON(..), fromJSON)
31import Data.Aeson.Types hiding (FromJSON(..), ToJSON(..), fromJSON)
28import Data.Attoparsec.Number (Number)
32import Data.Attoparsec.Number (Number)
29import Data.Generics
33import Data.Generics
@@ -35,7 +39,11 @@ import Data.Text (Text, pack, unpack)
35import Data.Text.Encoding (encodeUtf8)
39import Data.Text.Encoding (encodeUtf8)
36import Data.Time.Clock (UTCTime)
40import Data.Time.Clock (UTCTime)
37import Data.Word (Word, Word8, Word16, Word32, Word64)
41import Data.Word (Word, Word8, Word16, Word32, Word64)
42+import Data.Aeson.Parser (json)
43+import qualified Data.Aeson.Encode as E
44+import qualified Data.Aeson.Functions as F
38import qualified Data.Aeson.Types as T
45import qualified Data.Aeson.Types as T
46+import qualified Data.Attoparsec.Lazy as L
39import qualified Data.ByteString as B
47import qualified Data.ByteString as B
40import qualified Data.ByteString.Lazy as L
48import qualified Data.ByteString.Lazy as L
41import qualified Data.HashMap.Strict as H
49import qualified Data.HashMap.Strict as H
@@ -46,6 +54,21 @@ import qualified Data.Text.Lazy as LT
46import qualified Data.Traversable as T
54import qualified Data.Traversable as T
47import qualified Data.Vector as V
55import qualified Data.Vector as V
57+-- | Efficiently serialize a JSON value as a lazy 'L.ByteString'.
58+encode :: (Data a) => a -> L.ByteString
59+encode = E.encode . toJSON
62+-- | Efficiently deserialize a JSON value from a lazy 'L.ByteString'.
63+-- If this fails due to incomplete or invalid input, 'Nothing' is
65+decode :: (Data a) => L.ByteString -> Maybe a
66+decode s = case L.parse json s of
67+ L.Done _ v -> case fromJSON v of
49type T a = a -> Value
72type T a = a -> Value
51toJSON :: (Data a) => a -> Value
74toJSON :: (Data a) => a -> Value
@@ -93,7 +116,7 @@ toJSON = toJSON_generic
93 | tyrep == typeOf DT.empty = remap id
116 | tyrep == typeOf DT.empty = remap id
94 | tyrep == typeOf LT.empty = remap LT.toStrict
117 | tyrep == typeOf LT.empty = remap LT.toStrict
95 | tyrep == typeOf "" = remap pack
118 | tyrep == typeOf "" = remap pack
96− | tyrep == typeOf B.empty = remap decode
119+ | tyrep == typeOf B.empty = remap F.decode
97 | tyrep == typeOf L.empty = remap strict
120 | tyrep == typeOf L.empty = remap strict
98 | otherwise = modError "toJSON" $
121 | otherwise = modError "toJSON" $
99 "cannot convert map keyed by type " ++ show tyrep
122 "cannot convert map keyed by type " ++ show tyrep
@@ -104,7 +127,7 @@ toJSON = toJSON_generic
104 | tyrep == typeOf DT.empty = remap id
127 | tyrep == typeOf DT.empty = remap id
105 | tyrep == typeOf LT.empty = remap LT.toStrict
128 | tyrep == typeOf LT.empty = remap LT.toStrict
106 | tyrep == typeOf "" = remap pack
129 | tyrep == typeOf "" = remap pack
107− | tyrep == typeOf B.empty = remap decode
130+ | tyrep == typeOf B.empty = remap F.decode
108 | tyrep == typeOf L.empty = remap strict
131 | tyrep == typeOf L.empty = remap strict
109 | otherwise = modError "toJSON" $
132 | otherwise = modError "toJSON" $
110 "cannot convert map keyed by type " ++ show tyrep
133 "cannot convert map keyed by type " ++ show tyrep