Skip to content

Commit 0e8a34c

Browse files
Fix #8
1 parent 34851cb commit 0e8a34c

4 files changed

Lines changed: 21 additions & 11 deletions

File tree

spago.dhall

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
, "maybe"
2020
, "newtype"
2121
, "nullable"
22+
, "numbers"
2223
, "ordered-collections"
2324
, "partial"
2425
, "prelude"

src/Yoga/JSON.purs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import Data.Array.NonEmpty (NonEmptyArray, fromArray, toArray)
4141
import Data.Bifunctor (lmap)
4242
import Data.BigInt (BigInt)
4343
import Data.BigInt as BigInt
44-
import Data.Either (Either, hush, note)
44+
import Data.Either (Either(..), hush, note)
4545
import Data.FoldableWithIndex (foldrWithIndex)
4646
import Data.Identity (Identity(..))
4747
import Data.Int as Int
@@ -51,6 +51,7 @@ import Data.Map as Map
5151
import Data.Maybe (Maybe(..), fromMaybe, fromMaybe', maybe)
5252
import Data.Newtype (class Newtype)
5353
import Data.Nullable (Nullable, toMaybe, toNullable)
54+
import Data.Number as Number
5455
import Data.Symbol (class IsSymbol, reflectSymbol)
5556
import Data.Traversable (sequence, traverse)
5657
import Data.TraversableWithIndex (traverseWithIndex)
@@ -185,18 +186,25 @@ readBigInt ∷
185186
readBigInt = unsafeReadTagged "BigInt"
186187

187188
instance 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+
200208
instance ReadForeign String where
201209
readImpl = readString
202210

test/BasicsSpec.purs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ spec = describe "En- and decoding" $ do
5454
$ roundtrips (Map.fromFoldable [(Inty 4 /\ "B"),(Inty 8 /\ "D")])
5555
it "roundtrips BigInt" do
5656
let
57-
inputStr = """{ "number":1, "big": 18014398509481982, "smallBig": 10 }"""
58-
expected = """{"smallBig":"10","number":1,"big":"18014398509481982"}"""
59-
parsed _ ({ number Int, big BigInt, smallBig BigInt })
57+
inputStr = """{ "number":1, "big": 18014398509481982, "mediumBig": 1652955871799, "smallBig": 10 }"""
58+
expected = """{"smallBig":"10","number":1,"mediumBig":"1652955871799","big":"18014398509481982"}"""
59+
parsed _ ({ number Int, mediumBig :: BigInt, big BigInt, smallBig BigInt })
6060
parsed = readJSON inputStr
6161
stringified = parsed <#> writeJSON
6262
stringified `shouldEqual` (Right expected)
@@ -65,7 +65,7 @@ spec = describe "En- and decoding" $ do
6565
-- let
6666
-- smallBig = BigInt.fromInt 10
6767
-- big = unsafePartial $ fromJust $ BigInt.fromString "18014398509481982"
68-
68+
6969
-- expected = { big, smallBig }
7070
-- json = spy "json" $ writeJSON expected
7171

test/GenericsSpec.purs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ spec = describe "Generics" $ do
4646
writeJSON (ATaggedInt 1) `shouldEqual` """{"type":"ATaggedInt","value":1}"""
4747
writeJSON (ATaggedString "Abc") `shouldEqual` """{"type":"ATaggedString","value":"Abc"}"""
4848

49+
4950
describe "data HalfEnum = NotEnum Int | IsEnum" do
5051
it "roundtrips" do
5152
roundtrips (NotEnum 1)

0 commit comments

Comments
 (0)