git.iko.soy/typeable/postgresql-simple
on master help sign in
Add ToField and FromField instances for aeson's JSON Value
63a00b2Bas van Dijk
postgresql-simple.cabal+2−1
@@ -52,11 +52,12 @@ Library
52 Database.PostgreSQL.Simple.TypeInfo.Types
52 Database.PostgreSQL.Simple.TypeInfo.Types
53
53
54 Build-depends:
54 Build-depends:
55+ aeson >= 0.6,
55 attoparsec >= 0.10.3,
56 attoparsec >= 0.10.3,
56 base < 5,
57 base < 5,
57 blaze-builder,
58 blaze-builder,
58 blaze-textual,
59 blaze-textual,
59− bytestring >= 0.9,
60+ bytestring >= 0.10,
60 containers,
61 containers,
61 postgresql-libpq >= 0.6.2,
62 postgresql-libpq >= 0.6.2,
62 template-haskell,
63 template-haskell,
src/Database/PostgreSQL/Simple/FromField.hs+12
@@ -94,6 +94,7 @@ module Database.PostgreSQL.Simple.FromField
94import Control.Applicative
94import Control.Applicative
95 ( Applicative, (<|>), (<$>), pure )
95 ( Applicative, (<|>), (<$>), pure )
96import Control.Exception (Exception)
96import Control.Exception (Exception)
97+import qualified Data.Aeson as JSON
97import Data.Attoparsec.Char8 hiding (Result)
98import Data.Attoparsec.Char8 hiding (Result)
98import Data.ByteString (ByteString)
99import Data.ByteString (ByteString)
99import qualified Data.ByteString.Char8 as B
100import qualified Data.ByteString.Char8 as B
@@ -441,6 +442,17 @@ fromArray typeInfo f = sequence . (parseIt <$>) <$> array delim
441 where f' | Arrays.Array _ <- item = f
442 where f' | Arrays.Array _ <- item = f
442 | otherwise = fElem
443 | otherwise = fElem
443
444
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+
444type Compat = PQ.Oid -> Bool
456type Compat = PQ.Oid -> Bool
445
457
446okText, okText', okBinary, ok16, ok32, ok64, okInt :: Compat
458okText, okText', okBinary, ok16, ok32, ok64, okInt :: Compat
src/Database/PostgreSQL/Simple/ToField.hs+4
@@ -24,6 +24,7 @@ module Database.PostgreSQL.Simple.ToField
24import Blaze.ByteString.Builder (Builder, fromByteString, toByteString)
24import Blaze.ByteString.Builder (Builder, fromByteString, toByteString)
25import Blaze.ByteString.Builder.Char8 (fromChar)
25import Blaze.ByteString.Builder.Char8 (fromChar)
26import Blaze.Text (integral, double, float)
26import Blaze.Text (integral, double, float)
27+import qualified Data.Aeson as JSON
27import Data.ByteString (ByteString)
28import Data.ByteString (ByteString)
28import Data.Int (Int8, Int16, Int32, Int64)
29import Data.Int (Int8, Int16, Int32, Int64)
29import Data.List (intersperse)
30import Data.List (intersperse)
@@ -230,6 +231,9 @@ instance (ToField a) => ToField (Vector a) where
230 -- Because the ARRAY[...] input syntax is being used, it is possible
231 -- Because the ARRAY[...] input syntax is being used, it is possible
231 -- that the use of type-specific separator characters is unnecessary.
232 -- that the use of type-specific separator characters is unnecessary.
232
233
234+instance ToField JSON.Value where
235+ toField = toField . JSON.encode
236+
233-- | Surround a string with single-quote characters: \"@'@\"
237-- | Surround a string with single-quote characters: \"@'@\"
234--
238--
235-- This function /does not/ perform any other escaping.
239-- This function /does not/ perform any other escaping.