on master help sign in
Address PR feedback.
a09eceeJesse Kempf
Data/Aeson.hs+1−13
@@ -1,5 +1,3 @@
1−{-# LANGUAGE CPP #-}
2−
3-- |
1-- |
4-- Module: Data.Aeson
2-- Module: Data.Aeson
5-- Copyright: (c) 2011-2016 Bryan O'Sullivan
3-- Copyright: (c) 2011-2016 Bryan O'Sullivan
@@ -113,6 +111,7 @@ module Data.Aeson
113 , withNumber
111 , withNumber
114 , withScientific
112 , withScientific
115 , withBool
113 , withBool
114+ , withEmbeddedJSON
116 -- * Constructors and accessors
115 -- * Constructors and accessors
117 , Series
116 , Series
118 , pairs
117 , pairs
@@ -122,7 +121,6 @@ module Data.Aeson
122 , (.:!)
121 , (.:!)
123 , (.!=)
122 , (.!=)
124 , object
123 , object
125− , embeddedJSON
126 -- * Parsing
124 -- * Parsing
127 , json
125 , json
128 , json'
126 , json'
@@ -138,7 +136,6 @@ import Data.Aeson.Types
138import Data.Aeson.Types.Internal (JSONPath, formatError)
136import Data.Aeson.Types.Internal (JSONPath, formatError)
139import qualified Data.ByteString as B
137import qualified Data.ByteString as B
140import qualified Data.ByteString.Lazy as L
138import qualified Data.ByteString.Lazy as L
141−import qualified Data.Text.Encoding as T
142
139
143-- | Efficiently serialize a JSON value as a lazy 'L.ByteString'.
140-- | Efficiently serialize a JSON value as a lazy 'L.ByteString'.
144--
141--
@@ -224,15 +221,6 @@ eitherDecodeStrict' =
224 eitherFormatError . eitherDecodeStrictWith jsonEOF' ifromJSON
221 eitherFormatError . eitherDecodeStrictWith jsonEOF' ifromJSON
225{-# INLINE eitherDecodeStrict' #-}
222{-# INLINE eitherDecodeStrict' #-}
226
223
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
236-- $use
224-- $use
237--
225--
238-- This section contains basic information on the different ways to
226-- This section contains basic information on the different ways to
Data/Aeson/Compat.hsnew file+16
@@ -0,0 +1,16 @@
1+{-# LANGUAGE CPP #-}
2+
3+module Data.Aeson.Compat
4+ (
5+ fromStrict
6+ ) where
7+
8+import qualified Data.ByteString as S
9+import qualified Data.ByteString.Lazy as L
10+
11+fromStrict :: S.ByteString -> L.ByteString
12+#if MIN_VERSION_bytestring(0, 9, 2)
13+fromStrict = L.fromChunks . (:[])
14+#else
15+fromStrict = L.fromStrict
16+#endif
Data/Aeson/Types.hs+1
@@ -84,6 +84,7 @@ module Data.Aeson.Types
84 , withNumber
84 , withNumber
85 , withScientific
85 , withScientific
86 , withBool
86 , withBool
87+ , withEmbeddedJSON
87
88
88 , pairs
89 , pairs
89 , foldable
90 , foldable
Data/Aeson/Types/Class.hs+1
@@ -73,6 +73,7 @@ module Data.Aeson.Types.Class
73 , withNumber
73 , withNumber
74 , withScientific
74 , withScientific
75 , withBool
75 , withBool
76+ , withEmbeddedJSON
76
77
77 -- * Functions
78 -- * Functions
78 , fromJSON
79 , fromJSON
Data/Aeson/Types/FromJSON.hs+13
@@ -55,6 +55,7 @@ module Data.Aeson.Types.FromJSON
55 , withNumber
55 , withNumber
56 , withScientific
56 , withScientific
57 , withBool
57 , withBool
58+ , withEmbeddedJSON
58
59
59 -- * Functions
60 -- * Functions
60 , fromJSON
61 , fromJSON
@@ -82,6 +83,7 @@ import Prelude.Compat
82import Control.Applicative ((<|>), Const(..))
83import Control.Applicative ((<|>), Const(..))
83import Control.Monad ((<=<), zipWithM)
84import Control.Monad ((<=<), zipWithM)
84import Data.Aeson.Internal.Functions (mapKey)
85import Data.Aeson.Internal.Functions (mapKey)
86+import Data.Aeson.Parser.Internal (eitherDecodeWith, jsonEOF)
85import Data.Aeson.Types.Generic
87import Data.Aeson.Types.Generic
86import Data.Aeson.Types.Internal
88import Data.Aeson.Types.Internal
87import Data.Attoparsec.Number (Number(..))
89import Data.Attoparsec.Number (Number(..))
@@ -114,6 +116,7 @@ import GHC.Generics
114import Numeric.Natural (Natural)
116import Numeric.Natural (Natural)
115import Text.ParserCombinators.ReadP (readP_to_S)
117import Text.ParserCombinators.ReadP (readP_to_S)
116import Unsafe.Coerce (unsafeCoerce)
118import Unsafe.Coerce (unsafeCoerce)
119+import qualified Data.Aeson.Compat as Compat
117import qualified Data.Aeson.Parser.Time as Time
120import qualified Data.Aeson.Parser.Time as Time
118import qualified Data.Attoparsec.ByteString.Char8 as A (endOfInput, parseOnly, scientific)
121import qualified Data.Attoparsec.ByteString.Char8 as A (endOfInput, parseOnly, scientific)
119import qualified Data.DList as DList
122import qualified Data.DList as DList
@@ -665,6 +668,16 @@ withBool _ f (Bool arr) = f arr
665withBool expected _ v = typeMismatch expected v
668withBool expected _ v = typeMismatch expected v
666{-# INLINE withBool #-}
669{-# INLINE withBool #-}
667
670
671+-- | Decode a nested JSON-encoded string.
672+withEmbeddedJSON :: (FromJSON a) => String -> (Value -> Parser a) -> Value -> Parser a
673+withEmbeddedJSON _ innerParser (String txt) =
674+ either fail innerParser $ eitherDecode (Compat.fromStrict $ T.encodeUtf8 txt)
675+ where
676+ eitherDecode = eitherFormatError . eitherDecodeWith jsonEOF ifromJSON
677+ eitherFormatError = either (Left . uncurry formatError) Right
678+withEmbeddedJSON name _ v = typeMismatch name v
679+{-# INLINE withEmbeddedJSON #-}
680+
668-- | Convert a value from JSON, failing if the types do not match.
681-- | Convert a value from JSON, failing if the types do not match.
669fromJSON :: (FromJSON a) => Value -> Result a
682fromJSON :: (FromJSON a) => Value -> Result a
670fromJSON = parse parseJSON
683fromJSON = parse parseJSON
aeson.cabal+1
@@ -100,6 +100,7 @@ library
100 Data.Aeson.Encode
100 Data.Aeson.Encode
101
101
102 other-modules:
102 other-modules:
103+ Data.Aeson.Compat
103 Data.Aeson.Encoding.Builder
104 Data.Aeson.Encoding.Builder
104 Data.Aeson.Internal.Functions
105 Data.Aeson.Internal.Functions
105 Data.Aeson.Parser.Unescape
106 Data.Aeson.Parser.Unescape