on master help sign in
Add a helper for decoding embedded json strings
e8d263dJesse Kempf
Data/Aeson.hs+13
@@ -1,3 +1,5 @@
1+{-# LANGUAGE CPP #-}
2+
1-- |
3-- |
2-- Module: Data.Aeson
4-- Module: Data.Aeson
3-- Copyright: (c) 2011-2016 Bryan O'Sullivan
5-- Copyright: (c) 2011-2016 Bryan O'Sullivan
@@ -120,6 +122,7 @@ module Data.Aeson
120 , (.:!)
122 , (.:!)
121 , (.!=)
123 , (.!=)
122 , object
124 , object
125+ , embeddedJSON
123 -- * Parsing
126 -- * Parsing
124 , json
127 , json
125 , json'
128 , json'
@@ -135,6 +138,7 @@ import Data.Aeson.Types
135import Data.Aeson.Types.Internal (JSONPath, formatError)
138import Data.Aeson.Types.Internal (JSONPath, formatError)
136import qualified Data.ByteString as B
139import qualified Data.ByteString as B
137import qualified Data.ByteString.Lazy as L
140import qualified Data.ByteString.Lazy as L
141+import qualified Data.Text.Encoding as T
138
142
139-- | Efficiently serialize a JSON value as a lazy 'L.ByteString'.
143-- | Efficiently serialize a JSON value as a lazy 'L.ByteString'.
140--
144--
@@ -220,6 +224,15 @@ eitherDecodeStrict' =
220 eitherFormatError . eitherDecodeStrictWith jsonEOF' ifromJSON
224 eitherFormatError . eitherDecodeStrictWith jsonEOF' ifromJSON
221{-# INLINE eitherDecodeStrict' #-}
225{-# INLINE eitherDecodeStrict' #-}
222
226
227+-- | Decode a nested JSON-encoded string.
228+embeddedJSON :: (FromJSON a) => String -> (Value -> Parser a)
229+embeddedJSON str = withText str (either fail return . eitherDecode . fromStrict . T.encodeUtf8)
230+ where
231+#if MIN_VERSION_bytestring(0, 9, 2)
232+ fromStrict = L.fromChunks . (:[])
233+#else
234+ fromStrict = L.fromStrict
235+#endif
223-- $use
236-- $use
224--
237--
225-- This section contains basic information on the different ways to
238-- This section contains basic information on the different ways to