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

Adds a pretty decoder that removes Aeson portion of decoding error #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion json-fleece-aeson/json-fleece-aeson.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.35.2.
-- This file has been generated from package.yaml by hpack version 0.36.0.
--
-- see: https://github.com/sol/hpack

Expand Down Expand Up @@ -41,6 +41,7 @@ library
, base >=4.7 && <5
, bytestring ==0.11.*
, containers ==0.6.*
, either ==5.0.2
, json-fleece-core ==0.5.*
, shrubbery >=0.1.2 && <0.2
, text >=1.2 && <2.1
Expand Down
1 change: 1 addition & 0 deletions json-fleece-aeson/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ library:
- Fleece.Aeson.Decoder
- Fleece.Aeson.Encoder
dependencies:
- either == 5.0.2
- shrubbery >= 0.1.2 && < 0.2

tests:
Expand Down
17 changes: 12 additions & 5 deletions json-fleece-aeson/src/Fleece/Aeson/Decoder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Fleece.Aeson.Decoder
( Decoder (..)
, decode
, decodePretty
, decodeStrict
, fromValue
, toParser
Expand All @@ -16,6 +17,7 @@ import qualified Data.Aeson.KeyMap as AesonKeyMap
import qualified Data.Aeson.Types as AesonTypes
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
import Data.Either.Combinators (mapLeft)
import qualified Data.Map.Strict as Map
import qualified Data.Text.Encoding as Enc
import qualified Shrubbery
Expand All @@ -25,21 +27,26 @@ import qualified Fleece.Core as FC
data Decoder a
= Decoder FC.Name (Aeson.Value -> AesonTypes.Parser a)

fromValue :: Decoder a -> Aeson.Value -> Either String a
fromValue =
AesonTypes.parseEither . toParser
fromValue :: Decoder a -> Aeson.Value -> Either (String, String) a
fromValue decoder value =
mapLeft (\(path, err) -> (AesonTypes.formatError path err, err)) $
AesonTypes.iparseEither (toParser decoder) value

toParser :: Decoder a -> Aeson.Value -> AesonTypes.Parser a
toParser (Decoder _name f) =
f

decode :: Decoder a -> LBS.ByteString -> Either String a
decode decoder =
fromValue decoder <=< Aeson.eitherDecode
mapLeft fst . fromValue decoder <=< Aeson.eitherDecode

decodePretty :: Decoder a -> LBS.ByteString -> Either String a
decodePretty decoder =
mapLeft snd . fromValue decoder <=< Aeson.eitherDecode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather that offer a separate decodePretty function it would be better to make the decode function return a new error type other than String that the caller could then decide how to format.


decodeStrict :: Decoder a -> BS.ByteString -> Either String a
decodeStrict decoder =
fromValue decoder <=< Aeson.eitherDecodeStrict
mapLeft fst . fromValue decoder <=< Aeson.eitherDecodeStrict

instance FC.Fleece Decoder where
data Object Decoder _object a = Object
Expand Down