git.iko.soy/typeable/postgresql-simple
on master help sign in
Merge pull request #151 from srijs/fix/fromfield-value

fix FromField Value so it accepts not only top-level json values

commit2e09b1d8d823b3d3c427067094a5e156f87407c5 authorLeon P Smith <leon.p.smith+github@gmail.com> dateTue 23 Jun 2015, 12:15 (11 years ago) parent32f1aae6cb4083
2 files changed+5−8
src/Database/PostgreSQL/Simple/FromField.hs+3−8 test/Main.hs+2
src/Database/PostgreSQL/Simple/FromField.hs+3−8
@@ -112,10 +112,11 @@ module Database.PostgreSQL.Simple.FromField
112112
113113#include "MachDeps.h"
114114
115−import Control.Applicative ( (<|>), (<$>), pure, (*>) )
115+import Control.Applicative ( (<|>), (<$>), pure, (*>), (<*) )
116116import Control.Concurrent.MVar (MVar, newMVar)
117117import Control.Exception (Exception)
118118import qualified Data.Aeson as JSON
119+import qualified Data.Aeson.Parser as JSON (value')
119120import Data.Attoparsec.ByteString.Char8 hiding (Result)
120121import Data.ByteString (ByteString)
121122import qualified Data.ByteString.Char8 as B
@@ -545,13 +546,7 @@ instance FromField JSON.Value where
545546 else case mbs of
546547 Nothing -> returnError UnexpectedNull f ""
547548 Just bs ->
548−#if MIN_VERSION_aeson(0,6,3)
549− case JSON.eitherDecodeStrict' bs of
550−#elif MIN_VERSION_bytestring(0,10,0)
551− case JSON.eitherDecode' $ LB.fromStrict bs of
552−#else
553− case JSON.eitherDecode' $ LB.fromChunks [bs] of
554−#endif
549+ case parseOnly (JSON.value' <* endOfInput) bs of
555550 Left err -> returnError ConversionFailed f err
556551 Right val -> pure val
557552
test/Main.hs+2
@@ -184,6 +184,8 @@ testJSON TestEnv{..} = TestCase $ do
184184 roundTrip (Map.fromList [("foo","bar"),("bar","baz"),("baz","hello")] :: Map Text Text)
185185 roundTrip (Map.fromList [("fo\"o","bar"),("b\\ar","baz"),("baz","\"value\\with\"escapes")] :: Map Text Text)
186186 roundTrip (V.fromList [1,2,3,4,5::Int])
187+ roundTrip ("foo" :: Text)
188+ roundTrip (42 :: Int)
187189 where
188190 roundTrip :: ToJSON a => a -> Assertion
189191 roundTrip a = do