From 0d35b328d2eb6d5180fc4358392aba2fbc2cc6d6 Mon Sep 17 00:00:00 2001 From: Tom Ellis Date: Wed, 23 Aug 2017 12:33:02 +0100 Subject: [PATCH] Add fromFieldJSONByteString --- src/Database/PostgreSQL/Simple/FromField.hs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Database/PostgreSQL/Simple/FromField.hs b/src/Database/PostgreSQL/Simple/FromField.hs index 2868ce33..bce3ba3b 100644 --- a/src/Database/PostgreSQL/Simple/FromField.hs +++ b/src/Database/PostgreSQL/Simple/FromField.hs @@ -552,15 +552,19 @@ instance FromField UUID where -- | json instance FromField JSON.Value where - fromField f mbs = + fromField f mbs = parse =<< fromFieldJSONByteString f mbs + where parse bs = case parseOnly (JSON.value' <* endOfInput) bs of + Left err -> returnError ConversionFailed f err + Right val -> pure val + +-- | Return the JSON ByteString directly +fromFieldJSONByteString :: Field -> Maybe ByteString -> Conversion ByteString +fromFieldJSONByteString f mbs = if typeOid f /= $(inlineTypoid TI.json) && typeOid f /= $(inlineTypoid TI.jsonb) then returnError Incompatible f "" else case mbs of Nothing -> returnError UnexpectedNull f "" - Just bs -> - case parseOnly (JSON.value' <* endOfInput) bs of - Left err -> returnError ConversionFailed f err - Right val -> pure val + Just bs -> pure bs -- | Parse a field to a JSON 'JSON.Value' and convert that into a -- Haskell value using 'JSON.fromJSON'.