git.iko.soy/rowtype-yoga/purescript-yoga-json
on main help sign in
Fix #8
commit0e8a34c8d8c47e93efeed11a3c55d71b2a7832fa authorMark Eibes <mark.eibes@gmail.com> dateMon 4 Jul 2022, 19:27 (4 years ago) parent34851cb
4 files changed+21−11
spago.dhall+1 src/Yoga/JSON.purs+15−7 test/BasicsSpec.purs+4−4 test/GenericsSpec.purs+1
spago.dhall+1
@@ -19,6 +19,7 @@
1919 , "maybe"
2020 , "newtype"
2121 , "nullable"
22+ , "numbers"
2223 , "ordered-collections"
2324 , "partial"
2425 , "prelude"
src/Yoga/JSON.purs+15−7
@@ -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
test/BasicsSpec.purs+4−4
@@ -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+1
@@ -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)