Skip to content

Commit 9e0aa34

Browse files
committed
Changed API for decode combinators to match Aeson's
Added manual tests for decoding with different combinator combinations
1 parent 82efbd5 commit 9e0aa34

File tree

4 files changed

+192
-34
lines changed

4 files changed

+192
-34
lines changed

package-lock.json

Lines changed: 21 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Data/Argonaut/Decode.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ module Data.Argonaut.Decode
44
) where
55

66
import Data.Argonaut.Decode.Class (class DecodeJson, decodeJson)
7-
import Data.Argonaut.Decode.Combinators (getField, (.?), getFieldOptional, (.??), defaultField, (.?=))
7+
import Data.Argonaut.Decode.Combinators (getField, (.:), getFieldOptional, (.:!), getFieldOptional', (.:?), defaultField, (.!=))

src/Data/Argonaut/Decode/Combinators.purs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
module Data.Argonaut.Decode.Combinators
22
( getField
33
, getFieldOptional
4+
, getFieldOptional'
45
, defaultField
5-
, (.?)
6-
, (.??)
7-
, (.?=)
6+
, (.:)
7+
, (.:!)
8+
, (.:?)
9+
, (.!=)
810
) where
911

1012
import Prelude
1113

12-
import Data.Argonaut.Core (Json)
14+
import Data.Argonaut.Core (Json, isNull)
1315
import Data.Argonaut.Decode.Class (class DecodeJson, decodeJson)
1416
import Data.Bifunctor (lmap)
1517
import Data.Either (Either(..))
@@ -23,7 +25,7 @@ getField o s =
2325
(elaborateFailure s <<< decodeJson)
2426
(FO.lookup s o)
2527

26-
infix 7 getField as .?
28+
infix 7 getField as .:
2729

2830
getFieldOptional :: forall a. DecodeJson a => FO.Object Json -> String -> Either String (Maybe a)
2931
getFieldOptional o s =
@@ -34,12 +36,26 @@ getFieldOptional o s =
3436
where
3537
decode json = Just <$> (elaborateFailure s <<< decodeJson) json
3638

37-
infix 7 getFieldOptional as .??
39+
infix 7 getFieldOptional as .:!
40+
41+
getFieldOptional' :: forall a. DecodeJson a => FO.Object Json -> String -> Either String (Maybe a)
42+
getFieldOptional' o s =
43+
maybe
44+
(pure Nothing)
45+
decode
46+
(FO.lookup s o)
47+
where
48+
decode json =
49+
if isNull json
50+
then pure Nothing
51+
else Just <$> decodeJson json
52+
53+
infix 7 getFieldOptional' as .:?
3854

3955
defaultField :: forall a. Either String (Maybe a) -> a -> Either String a
4056
defaultField parser default = fromMaybe default <$> parser
4157

42-
infix 6 defaultField as .?=
58+
infix 6 defaultField as .!=
4359

4460
elaborateFailure :: a. String -> Either String a -> Either String a
4561
elaborateFailure s e =

0 commit comments

Comments
 (0)