@@ -5,17 +5,17 @@ module Data.Argonaut.Decode.Class
55
66import Prelude
77
8- import Data.Argonaut.Core (Json , JArray , JObject , isNull , foldJsonNull , foldJsonBoolean , foldJsonNumber , foldJsonString , toArray , toObject , toString )
8+ import Data.Argonaut.Core (Json , isNull , caseJsonNull , caseJsonBoolean , caseJsonNumber , caseJsonString , toArray , toObject , toString , stringify )
99import Data.Bifunctor (lmap )
1010import Data.Either (Either (..))
1111import Data.Int (fromNumber )
1212import Data.List (List (..), (:), fromFoldable )
1313import Data.Map as M
1414import Data.Maybe (maybe , Maybe (..))
15- import Data.String (charAt )
16- import Data.StrMap as SM
15+ import Data.String (CodePoint , codePointAt )
1716import Data.Traversable (traverse )
1817import Data.Tuple (Tuple (..))
18+ import Foreign.Object as FO
1919
2020class DecodeJson a where
2121 decodeJson :: Json -> Either String a
@@ -35,21 +35,21 @@ instance decodeJsonEither :: (DecodeJson a, DecodeJson b) => DecodeJson (Either
3535 decodeJson json =
3636 lmap (" Couldn't decode Either: " <> _) $
3737 decodeJObject json >>= \obj -> do
38- tag <- maybe (Left " Expected field 'tag'" ) Right $ SM .lookup " tag" obj
39- val <- maybe (Left " Expected field 'value'" ) Right $ SM .lookup " value" obj
38+ tag <- maybe (Left " Expected field 'tag'" ) Right $ FO .lookup " tag" obj
39+ val <- maybe (Left " Expected field 'value'" ) Right $ FO .lookup " value" obj
4040 case toString tag of
4141 Just " Right" -> Right <$> decodeJson val
4242 Just " Left" -> Left <$> decodeJson val
4343 _ -> Left " 'tag' field was not \" Left\" or \" Right\" "
4444
4545instance decodeJsonNull :: DecodeJson Unit where
46- decodeJson = foldJsonNull (Left " Value is not a null" ) (const $ Right unit)
46+ decodeJson = caseJsonNull (Left " Value is not a null" ) (const $ Right unit)
4747
4848instance decodeJsonBoolean :: DecodeJson Boolean where
49- decodeJson = foldJsonBoolean (Left " Value is not a Boolean" ) Right
49+ decodeJson = caseJsonBoolean (Left " Value is not a Boolean" ) Right
5050
5151instance decodeJsonNumber :: DecodeJson Number where
52- decodeJson = foldJsonNumber (Left " Value is not a Number" ) Right
52+ decodeJson = caseJsonNumber (Left " Value is not a Number" ) Right
5353
5454instance decodeJsonInt :: DecodeJson Int where
5555 decodeJson
@@ -58,19 +58,19 @@ instance decodeJsonInt :: DecodeJson Int where
5858 <=< decodeJson
5959
6060instance decodeJsonString :: DecodeJson String where
61- decodeJson = foldJsonString (Left " Value is not a String" ) Right
61+ decodeJson = caseJsonString (Left " Value is not a String" ) Right
6262
6363instance decodeJsonJson :: DecodeJson Json where
6464 decodeJson = Right
6565
66- instance decodeJsonChar :: DecodeJson Char where
66+ instance decodeJsonChar :: DecodeJson CodePoint where
6767 decodeJson j =
68- maybe (Left $ " Expected character but found: " <> show j) Right
69- =<< charAt 0 <$> decodeJson j
68+ maybe (Left $ " Expected character but found: " <> stringify j) Right
69+ =<< codePointAt 0 <$> decodeJson j
7070
71- instance decodeStrMap :: DecodeJson a => DecodeJson (SM.StrMap a ) where
71+ instance decodeForeignObject :: DecodeJson a => DecodeJson (FO.Object a ) where
7272 decodeJson
73- = lmap (" Couldn't decode StrMap : " <> _)
73+ = lmap (" Couldn't decode ForeignObject : " <> _)
7474 <<< (traverse decodeJson <=< decodeJObject)
7575
7676instance decodeArray :: DecodeJson a => DecodeJson (Array a ) where
@@ -89,8 +89,8 @@ instance decodeMap :: (Ord a, DecodeJson a, DecodeJson b) => DecodeJson (M.Map a
8989instance decodeVoid :: DecodeJson Void where
9090 decodeJson _ = Left " Value cannot be Void"
9191
92- decodeJArray :: Json -> Either String JArray
92+ decodeJArray :: Json -> Either String ( Array Json )
9393decodeJArray = maybe (Left " Value is not an Array" ) Right <<< toArray
9494
95- decodeJObject :: Json -> Either String JObject
95+ decodeJObject :: Json -> Either String ( FO.Object Json )
9696decodeJObject = maybe (Left " Value is not an Object" ) Right <<< toObject
0 commit comments