File tree Expand file tree Collapse file tree 3 files changed +21
-0
lines changed Expand file tree Collapse file tree 3 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,9 @@ instance decodeJsonNonEmptyList :: (DecodeJson a) => DecodeJson (NonEmptyList a)
7676instance decodeJsonCodePoint :: DecodeJson CodePoint where
7777 decodeJson = decodeCodePoint
7878
79+ instance decodeChar :: DecodeJson Char where
80+ decodeJson = decodeChar
81+
7982instance decodeForeignObject :: DecodeJson a => DecodeJson (FO.Object a ) where
8083 decodeJson = decodeForeignObject decodeJson
8184
Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ import Data.Traversable (traverse)
2626import Data.TraversableWithIndex (traverseWithIndex )
2727import Data.Tuple (Tuple (..))
2828import Foreign.Object as FO
29+ import Data.String.CodeUnits (toChar ) as CU
2930
3031decodeIdentity
3132 :: forall a
@@ -148,6 +149,11 @@ decodeCodePoint json =
148149 note (Named " CodePoint" $ UnexpectedValue json)
149150 =<< map (codePointAt 0 ) (decodeString json)
150151
152+ decodeChar :: Json -> Either JsonDecodeError Char
153+ decodeChar json =
154+ note (Named " Char" $ UnexpectedValue json)
155+ =<< map CU .toChar (decodeString json)
156+
151157decodeForeignObject
152158 :: forall a
153159 . (Json -> Either JsonDecodeError a )
Original file line number Diff line number Diff line change @@ -71,6 +71,7 @@ jsonParser' = either (liftEffect <<< throw) pure <<< jsonParser
7171main :: Effect Unit
7272main = flip runReaderT 0 do
7373 suite " Either Check" eitherCheck
74+ suite " Char Check" charCheck
7475 suite " Encode/Decode NonEmpty Check" nonEmptyCheck
7576 suite " Encode/Decode Checks" encodeDecodeCheck
7677 suite " Encode/Decode Record Checks" encodeDecodeRecordCheck
@@ -222,6 +223,17 @@ eitherCheck = do
222223 Left err ->
223224 false <?> printJsonDecodeError err
224225
226+ charCheck :: Test
227+ charCheck = do
228+ test " Test EncodeJson/DecodeJson Char test" do
229+ quickCheck \(x :: Char ) ->
230+ case decodeJson (encodeJson x) of
231+ Right decoded ->
232+ decoded == x
233+ <?> (" x = " <> show x <> " , decoded = " <> show decoded)
234+ Left err ->
235+ false <?> printJsonDecodeError err
236+
225237manualRecordDecode :: Test
226238manualRecordDecode = do
227239 fooJson <- jsonParser' """ { "bar": [1, 2, 3], "baz": true }"""
You can’t perform that action at this time.
0 commit comments