Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fromFieldJSONByteString #222

Merged
merged 1 commit into from
Nov 26, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Database/PostgreSQL/Simple/FromField.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand Down