@@ -41,7 +41,7 @@ import Data.Array.NonEmpty (NonEmptyArray, fromArray, toArray)
4141import Data.Bifunctor (lmap )
4242import Data.BigInt (BigInt )
4343import Data.BigInt as BigInt
44- import Data.Either (Either , hush , note )
44+ import Data.Either (Either (..) , hush , note )
4545import Data.FoldableWithIndex (foldrWithIndex )
4646import Data.Identity (Identity (..))
4747import Data.Int as Int
@@ -51,6 +51,7 @@ import Data.Map as Map
5151import Data.Maybe (Maybe (..), fromMaybe , fromMaybe' , maybe )
5252import Data.Newtype (class Newtype )
5353import Data.Nullable (Nullable , toMaybe , toNullable )
54+ import Data.Number as Number
5455import Data.Symbol (class IsSymbol , reflectSymbol )
5556import Data.Traversable (sequence , traverse )
5657import Data.TraversableWithIndex (traverseWithIndex )
@@ -185,18 +186,25 @@ readBigInt ∷
185186readBigInt = unsafeReadTagged " BigInt"
186187
187188instance ReadForeign BigInt where
188- readImpl fValue = tryInt fValue <|> readBigInt fValue <|> tryString fValue
189+ readImpl fValue = tryInt fValue <|> tryNumber fValue <|> readBigInt fValue <|> tryString fValue
189190 where
190191 tryInt f = readInt f <#> BigInt .fromInt
192+ tryNumber f = do
193+ num <- readNumber f
194+ if Number .round num == num then
195+ BigInt .fromNumber num
196+ # note (err $ " Cannot convert Number " <> show num <> " to BigInt" )
197+ # except
198+ else
199+ (Left $ err $ " Cannot convert decimal Number " <> show num <> " to BigInt" )
200+ # except
191201 tryString f = do
192202 bi ← readString f
193203 BigInt .fromString bi
194- # note
195- ( singleton $ ForeignError $ " String " <> bi <>
196- " could not be converted to BigInt"
197- )
204+ # note ( err $ " Cannot convert String " <> bi <> " to BigInt" )
198205 # except
199-
206+ err = pure <<< ForeignError
207+
200208instance ReadForeign String where
201209 readImpl = readString
202210
0 commit comments