@@ -94,6 +94,7 @@ module Database.PostgreSQL.Simple.FromField
9494import Control.Applicative
9595 ( Applicative, (<|>), (<$>), pure )
9696import Control.Exception (Exception)
97+import qualified Data.Aeson as JSON
9798import Data.Attoparsec.Char8 hiding (Result)
9899import Data.ByteString (ByteString)
99100import qualified Data.ByteString.Char8 as B
@@ -441,6 +442,17 @@ fromArray typeInfo f = sequence . (parseIt <$>) <$> array delim
441442 where f' | Arrays.Array _ <- item = f
442443 | otherwise = fElem
443444
445+-- | json
446+instance FromField JSON.Value where
447+ fromField f Nothing = returnError UnexpectedNull f ""
448+ fromField f (Just bs)
449+ | typeOid f /= $(inlineTypoid TI.json) =
450+ returnError Incompatible f ""
451+ | otherwise =
452+ case JSON.eitherDecode' $ LB.fromStrict bs of
453+ Left err -> returnError ConversionFailed f err
454+ Right val -> pure val
455+
444456type Compat = PQ.Oid -> Bool
445457
446458okText, okText', okBinary, ok16, ok32, ok64, okInt :: Compat