Skip to content

Commit 20a0343

Browse files
committed
Merge pull request #11 from hdgarrood/fix-either
Fix either
2 parents 95b4e0e + 9a075bc commit 20a0343

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

src/Data/Argonaut/Decode.purs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,23 @@ instance decodeJsonTuple :: (DecodeJson a, DecodeJson b) => DecodeJson (Tuple a
7777
f _ = Left "Couldn't decode Tuple"
7878

7979
instance decodeJsonEither :: (DecodeJson a, DecodeJson b) => DecodeJson (Either a b) where
80-
decodeJson j = (Left <$> decodeJson j) <|> (Right <$> decodeJson j)
80+
decodeJson j =
81+
case toObject j of
82+
Just obj -> do
83+
tag <- just (M.lookup "tag" obj)
84+
val <- just (M.lookup "value" obj)
85+
case toString tag of
86+
Just "Right" ->
87+
Right <$> decodeJson val
88+
Just "Left" ->
89+
Left <$> decodeJson val
90+
_ ->
91+
Left "Couldn't decode Either"
92+
_ ->
93+
Left "Couldn't decode Either"
94+
where
95+
just (Just x) = Right x
96+
just Nothing = Left "Couldn't decode Either"
8197

8298
instance decodeJsonNull :: DecodeJson Unit where
8399
decodeJson = foldJsonNull (Left "Not null") (const $ Right unit)

src/Data/Argonaut/Encode.purs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ module Data.Argonaut.Encode
88
import Prelude
99

1010
import Data.Argonaut.Core (Json(), jsonNull, fromBoolean, fromNumber, fromString, fromArray, fromObject)
11-
import Data.Either (Either(..))
11+
import Data.Either (Either(..), either)
1212
import Data.Foldable (foldr)
1313
import Data.Generic (Generic, GenericSpine(..), toSpine)
1414
import Data.Int (toNumber)
15-
import Data.List (List(), fromList)
15+
import Data.List (List(..), fromList)
1616
import Data.Map as M
1717
import Data.Maybe (Maybe(..))
1818
import Data.String (fromChar)
@@ -50,8 +50,12 @@ instance encodeJsonTuple :: (EncodeJson a, EncodeJson b) => EncodeJson (Tuple a
5050
encodeJson (Tuple a b) = encodeJson [encodeJson a, encodeJson b]
5151

5252
instance encodeJsonEither :: (EncodeJson a, EncodeJson b) => EncodeJson (Either a b) where
53-
encodeJson (Left a) = encodeJson a
54-
encodeJson (Right b) = encodeJson b
53+
encodeJson = either (obj "Left") (obj "Right")
54+
where
55+
obj :: forall c. (EncodeJson c) => String -> c -> Json
56+
obj tag x =
57+
fromObject $ SM.fromList $
58+
Cons (Tuple "tag" (fromString tag)) (Cons (Tuple "value" (encodeJson x)) Nil)
5559

5660
instance encodeJsonUnit :: EncodeJson Unit where
5761
encodeJson = const jsonNull

test/Test/Main.purs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,18 @@ genericsCheck = do
183183
}]}
184184

185185

186+
eitherCheck = do
187+
log "Test EncodeJson/DecodeJson Either instance"
188+
quickCheck \(x :: Either String String) ->
189+
case decodeJson (encodeJson x) of
190+
Right decoded ->
191+
decoded == x
192+
<?> ("x = " <> show x <> ", decoded = " <> show decoded)
193+
Left err ->
194+
false <?> err
195+
186196
main = do
197+
eitherCheck
187198
encodeDecodeCheck
188199
combinatorsCheck
189200
genericsCheck

0 commit comments

Comments
 (0)