Skip to content

Commit ed75728

Browse files
committed
Resolve #852: Export ifromJSON and iparse from Data.Aeson.Types. Add iparseEither.
1 parent 10821e5 commit ed75728

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ For the latest version of this document, please see [https://github.com/haskell/
66
- Change `KeyValue` instances to be more general (and use equality to constraint them) instead of being more lax flexible instances.
77
- Export `Key` type also from `Data.Aeson.KeyMap` module.
88
- Export `mapWithKey` from `Data.Aeson.KeyMap` module.
9+
- Export `ifromJSON` and `iparse` from `Data.Aeson.Types`. Add `iparseEither`.
910

1011
### 2.0.3.0
1112

src/Data/Aeson/Types.hs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module Data.Aeson.Types
2929
, typeMismatch
3030
, unexpected
3131
-- * Type conversion
32+
-- ** Parsing
3233
, Parser
3334
, Result(..)
3435
, FromJSON(..)
@@ -37,12 +38,19 @@ module Data.Aeson.Types
3738
, parseEither
3839
, parseMaybe
3940
, parseFail
40-
, ToJSON(..)
41-
, KeyValue(..)
41+
-- ** Parser error handling
4242
, modifyFailure
4343
, prependFailure
4444
, parserThrowError
4545
, parserCatchError
46+
-- ** Parsing with paths
47+
, IResult (..)
48+
, ifromJSON
49+
, iparse
50+
, iparseEither
51+
-- ** Encoding
52+
, ToJSON(..)
53+
, KeyValue(..)
4654

4755
-- ** Keys for maps
4856
, ToJSONKey(..)

src/Data/Aeson/Types/Internal.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ module Data.Aeson.Types.Internal
4343
, JSONPathElement(..)
4444
, JSONPath
4545
, iparse
46+
, iparseEither
4647
, parse
4748
, parseEither
4849
, parseMaybe
@@ -580,6 +581,14 @@ parseEither m v = runParser (m v) [] onError Right
580581
where onError path msg = Left (formatError path msg)
581582
{-# INLINE parseEither #-}
582583

584+
-- | Run a 'Parser' with an 'Either' result type.
585+
-- If the parse fails, the 'Left' payload will contain an error message and a json path to failed element.
586+
--
587+
-- @since 2.1.0.0
588+
iparseEither :: (a -> Parser b) -> a -> Either (JSONPath, String) b
589+
iparseEither m v = runParser (m v) [] (\path msg -> Left (path, msg)) Right
590+
{-# INLINE iparseEither #-}
591+
583592
-- | Annotate an error message with a
584593
-- <http://goessner.net/articles/JsonPath/ JSONPath> error location.
585594
formatError :: JSONPath -> String -> String

0 commit comments

Comments
 (0)