Skip to content

Commit 0b812f1

Browse files
authored
Beginner-friendly syntax in decodeJsonTuple (#76)
* Decode Tuple as Array instead of List internally
1 parent 1e528c9 commit 0b812f1

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/Data/Argonaut/Decode/Class.purs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Data.Argonaut.Decode.Class where
22

33
import Prelude
44

5+
import Control.Apply (lift2)
56
import Data.Argonaut.Core (Json, isNull, caseJsonNull, caseJsonBoolean, caseJsonNumber, caseJsonString, toArray, toObject, toString, stringify)
67
import Data.Array as Arr
78
import Data.Array.NonEmpty (NonEmptyArray)
@@ -10,7 +11,7 @@ import Data.Bifunctor (lmap, rmap)
1011
import Data.Either (Either(..), note)
1112
import Data.Identity (Identity(..))
1213
import Data.Int (fromNumber)
13-
import Data.List (List(..), (:), fromFoldable)
14+
import Data.List (List, fromFoldable)
1415
import Data.List as L
1516
import Data.List.NonEmpty (NonEmptyList)
1617
import Data.List.NonEmpty as NEL
@@ -41,10 +42,11 @@ instance decodeJsonMaybe :: DecodeJson a => DecodeJson (Maybe a) where
4142
| otherwise = Just <$> decodeJson j
4243

4344
instance decodeJsonTuple :: (DecodeJson a, DecodeJson b) => DecodeJson (Tuple a b) where
44-
decodeJson j = decodeJson j >>= f
45-
where
46-
f (a : b : Nil) = Tuple <$> decodeJson a <*> decodeJson b
47-
f _ = Left "Couldn't decode Tuple"
45+
decodeJson j = do
46+
decoded <- decodeJson j
47+
case decoded of
48+
[a, b] -> lift2 Tuple (decodeJson a) (decodeJson b)
49+
_ -> Left "Couldn't decode Tuple"
4850

4951
instance decodeJsonEither :: (DecodeJson a, DecodeJson b) => DecodeJson (Either a b) where
5052
decodeJson json =

0 commit comments

Comments
 (0)