@@ -13,6 +13,7 @@ module JSON
1313 , toNull
1414 , toBoolean
1515 , toNumber
16+ , toInt
1617 , toString
1718 , toArray
1819 , toJArray
@@ -27,6 +28,7 @@ import Prelude
2728import Data.Either (Either (..))
2829import Data.Function.Uncurried (runFn2 , runFn3 , runFn7 )
2930import Data.Maybe (Maybe (..))
31+ import Data.Int as Int
3032import JSON.Internal (JArray , JObject , JSON ) as Exports
3133import JSON.Internal (JArray , JObject , JSON )
3234import JSON.Internal as Internal
@@ -60,7 +62,7 @@ fromNumberWithDefault fallback n = runFn2 Internal._fromNumberWithDefault fallba
6062
6163-- | Converts an `Int` into `JSON`.
6264-- |
63- -- | There is no corresponding `toInt` as JSON doesn't have a concept of integers - this is provided
65+ -- | Note: JSON doesn't have a concept of integers. This is provided
6466-- | as a convenience to avoid having to convert `Int` to `Number` before creating a `JSON` value.
6567foreign import fromInt :: Int -> JSON
6668
@@ -119,6 +121,12 @@ toBoolean json = runFn7 Internal._case fail Just fail fail fail fail json
119121toNumber :: JSON -> Maybe Number
120122toNumber json = runFn7 Internal ._case fail fail Just fail fail fail json
121123
124+ -- | Converts a `JSON` `Number` into an `Int`.
125+ -- |
126+ -- | This is provided for convenience only.
127+ toInt :: JSON -> Maybe Int
128+ toInt = toNumber >=> Int .fromNumber
129+
122130-- | Converts a `JSON` value to `String` if the `JSON` is a string.
123131toString :: JSON -> Maybe String
124132toString json = runFn7 Internal ._case fail fail fail Just fail fail json
0 commit comments