Skip to content

Commit e8d263d

Browse files
committed
Add a helper for decoding embedded json strings
1 parent a3f3260 commit e8d263d

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Data/Aeson.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE CPP #-}
2+
13
-- |
24
-- Module: Data.Aeson
35
-- Copyright: (c) 2011-2016 Bryan O'Sullivan
@@ -120,6 +122,7 @@ module Data.Aeson
120122
, (.:!)
121123
, (.!=)
122124
, object
125+
, embeddedJSON
123126
-- * Parsing
124127
, json
125128
, json'
@@ -135,6 +138,7 @@ import Data.Aeson.Types
135138
import Data.Aeson.Types.Internal (JSONPath, formatError)
136139
import qualified Data.ByteString as B
137140
import qualified Data.ByteString.Lazy as L
141+
import qualified Data.Text.Encoding as T
138142

139143
-- | Efficiently serialize a JSON value as a lazy 'L.ByteString'.
140144
--
@@ -220,6 +224,15 @@ eitherDecodeStrict' =
220224
eitherFormatError . eitherDecodeStrictWith jsonEOF' ifromJSON
221225
{-# INLINE eitherDecodeStrict' #-}
222226

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
223236
-- $use
224237
--
225238
-- This section contains basic information on the different ways to

0 commit comments

Comments
 (0)