@@ -87,6 +87,8 @@ module Database.PostgreSQL.Simple.FromField
88 , PQ.Oid(..)
88 , PQ.Oid(..)
89 , PQ.Format(..)
89 , PQ.Format(..)
92#include "MachDeps.h"
94#include "MachDeps.h"
@@ -453,6 +455,23 @@ instance FromField JSON.Value where
453 Left err -> returnError ConversionFailed f err
455 Left err -> returnError ConversionFailed f err
454 Right val -> pure val
456 Right val -> pure val
458+-- | Parse a field to a JSON 'JSON.Value' and convert that into a
459+-- Haskell value using 'JSON.fromJSON'.
461+-- This can be used as the default implementation for the 'fromField'
462+-- method for Haskell types that have a JSON representation in
465+-- The 'Typeable' constraint is required to show more informative
466+-- error messages when parsing fails.
467+fromJSONField :: (JSON.FromJSON a, Typeable a) => FieldParser a
468+fromJSONField f mbBs = do
469+ value <- fromField f mbBs
470+ case JSON.fromJSON value of
471+ JSON.Error err -> returnError ConversionFailed f $
472+ "JSON decoding error: " ++ err
473+ JSON.Success x -> pure x
456type Compat = PQ.Oid -> Bool
475type Compat = PQ.Oid -> Bool
458okText, okText', okBinary, ok16, ok32, ok64, okInt :: Compat
477okText, okText', okBinary, ok16, ok32, ok64, okInt :: Compat