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)
76
76
instance decodeJsonCodePoint :: DecodeJson CodePoint where
77
77
decodeJson = decodeCodePoint
78
78
79
+ instance decodeChar :: DecodeJson Char where
80
+ decodeJson = decodeChar
81
+
79
82
instance decodeForeignObject :: DecodeJson a => DecodeJson (FO.Object a ) where
80
83
decodeJson = decodeForeignObject decodeJson
81
84
Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ import Data.Traversable (traverse)
26
26
import Data.TraversableWithIndex (traverseWithIndex )
27
27
import Data.Tuple (Tuple (..))
28
28
import Foreign.Object as FO
29
+ import Data.String.CodeUnits (toChar ) as CU
29
30
30
31
decodeIdentity
31
32
:: forall a
@@ -148,6 +149,11 @@ decodeCodePoint json =
148
149
note (Named " CodePoint" $ UnexpectedValue json)
149
150
=<< map (codePointAt 0 ) (decodeString json)
150
151
152
+ decodeChar :: Json -> Either JsonDecodeError Char
153
+ decodeChar json =
154
+ note (Named " Char" $ UnexpectedValue json)
155
+ =<< map CU .toChar (decodeString json)
156
+
151
157
decodeForeignObject
152
158
:: forall a
153
159
. (Json -> Either JsonDecodeError a )
Original file line number Diff line number Diff line change @@ -71,6 +71,7 @@ jsonParser' = either (liftEffect <<< throw) pure <<< jsonParser
71
71
main :: Effect Unit
72
72
main = flip runReaderT 0 do
73
73
suite " Either Check" eitherCheck
74
+ suite " Char Check" charCheck
74
75
suite " Encode/Decode NonEmpty Check" nonEmptyCheck
75
76
suite " Encode/Decode Checks" encodeDecodeCheck
76
77
suite " Encode/Decode Record Checks" encodeDecodeRecordCheck
@@ -222,6 +223,17 @@ eitherCheck = do
222
223
Left err ->
223
224
false <?> printJsonDecodeError err
224
225
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
+
225
237
manualRecordDecode :: Test
226
238
manualRecordDecode = do
227
239
fooJson <- jsonParser' """ { "bar": [1, 2, 3], "baz": true }"""
You can’t perform that action at this time.
0 commit comments