Skip to content

Commit c7fc2cf

Browse files
Add decoders for Char
1 parent f8fdc1e commit c7fc2cf

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/Data/Argonaut/Decode/Class.purs

+3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ instance decodeJsonNonEmptyList :: (DecodeJson a) => DecodeJson (NonEmptyList a)
7676
instance decodeJsonCodePoint :: DecodeJson CodePoint where
7777
decodeJson = decodeCodePoint
7878

79+
instance decodeChar :: DecodeJson Char where
80+
decodeJson = decodeChar
81+
7982
instance decodeForeignObject :: DecodeJson a => DecodeJson (FO.Object a) where
8083
decodeJson = decodeForeignObject decodeJson
8184

src/Data/Argonaut/Decode/Decoders.purs

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import Data.Traversable (traverse)
2626
import Data.TraversableWithIndex (traverseWithIndex)
2727
import Data.Tuple (Tuple(..))
2828
import Foreign.Object as FO
29+
import Data.String.CodeUnits (toChar) as CU
2930

3031
decodeIdentity
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+
151157
decodeForeignObject
152158
:: forall a
153159
. (Json -> Either JsonDecodeError a)

test/Test/Main.purs

+12
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ jsonParser' = either (liftEffect <<< throw) pure <<< jsonParser
7171
main :: Effect Unit
7272
main = 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+
225237
manualRecordDecode :: Test
226238
manualRecordDecode = do
227239
fooJson <- jsonParser' """{ "bar": [1, 2, 3], "baz": true }"""

0 commit comments

Comments
 (0)