Skip to content

Add encode/decode instances for Identity a #54

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

Merged
merged 1 commit into from
May 27, 2019
Merged
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 bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"purescript-foreign-object": "^2.0.0",
"purescript-record": "^2.0.0",
"purescript-nonempty": "^5.0.0",
"purescript-arrays": "^5.1.0"
"purescript-arrays": "^5.1.0",
"purescript-identity": "^4.1.0"
},
"devDependencies": {
"purescript-assert": "^4.1.0",
Expand Down
4 changes: 4 additions & 0 deletions src/Data/Argonaut/Decode/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Data.Argonaut.Core (Json, isNull, caseJsonNull, caseJsonBoolean, caseJson
import Data.Array as Arr
import Data.Bifunctor (lmap, rmap)
import Data.Either (Either(..), note)
import Data.Identity (Identity(..))
import Data.Int (fromNumber)
import Data.List (List(..), (:), fromFoldable)
import Data.List as L
Expand All @@ -27,6 +28,9 @@ import Type.Data.RowList (RLProxy(..))
class DecodeJson a where
decodeJson :: Json -> Either String a

instance decodeIdentity :: DecodeJson a => DecodeJson (Identity a) where
decodeJson j = Identity <$> decodeJson j

instance decodeJsonMaybe :: DecodeJson a => DecodeJson (Maybe a) where
decodeJson j
| isNull j = pure Nothing
Expand Down
4 changes: 4 additions & 0 deletions src/Data/Argonaut/Encode/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Prelude
import Data.Argonaut.Core (Json, fromArray, fromBoolean, fromNumber, fromObject, fromString, jsonNull)
import Data.Array as Arr
import Data.Either (Either, either)
import Data.Identity (Identity(..))
import Data.Int (toNumber)
import Data.List (List(..), (:), toUnfoldable)
import Data.List as L
Expand All @@ -26,6 +27,9 @@ import Type.Data.RowList (RLProxy(..))
class EncodeJson a where
encodeJson :: a -> Json

instance encodeIdentity :: EncodeJson a => EncodeJson (Identity a) where
encodeJson (Identity a) = encodeJson a

instance encodeJsonMaybe :: EncodeJson a => EncodeJson (Maybe a) where
encodeJson Nothing = jsonNull
encodeJson (Just a) = encodeJson a
Expand Down