git.iko.soy/rowtype-yoga/purescript-yoga-json
on main help sign in
Support BigInt keys for maps
commit70aa164e86d3d9fa4d3f4f53a6485bab0d077b93 authorMark Eibes <mark.eibes@gmail.com> dateTue 21 Feb 2023, 12:30 (4 years ago) parentc987237
3 files changed+24−3
.gitignore+2−1 src/Yoga/JSON.purs+9−1 test/BasicsSpec.purs+13−1
.gitignore+2−1
@@ -7,4 +7,5 @@
77/.psa*
88/.spago/
99/.nyc_output/
10−/coverage/
10+/coverage/
11+/.idea/
src/Yoga/JSON.purs+9−1
@@ -69,7 +69,7 @@ import Foreign.Index (readProp)
6969import Foreign.Object (Object)
7070import Foreign.Object as Object
7171import JS.BigInt (BigInt)
72−import JS.BigInt (fromInt, fromNumber, fromString) as BigInt
72+import JS.BigInt (fromInt, fromNumber, fromString, toString) as BigInt
7373import Partial.Unsafe (unsafeCrashWith)
7474import Prim.Row as Row
7575import Prim.RowList (class RowToList, Cons, Nil, RowList)
@@ -549,6 +549,8 @@ instance (WriteForeign a) ⇒ WriteForeign (Map String a) where
549549 writeImpl = foldrWithIndex Object.insert Object.empty >>> writeImpl
550550else instance (WriteForeign a) ⇒ WriteForeign (Map Int a) where
551551 writeImpl = foldrWithIndex (show >>> Object.insert) Object.empty >>> writeImpl
552+else instance (WriteForeign a) ⇒ WriteForeign (Map BigInt a) where
553+ writeImpl = foldrWithIndex (BigInt.toString >>> Object.insert) Object.empty >>> writeImpl
552554else instance (Newtype nt key, WriteForeign (Map key value)) ⇒ WriteForeign (Map nt value) where
553555 writeImpl = (unsafeCoerce ∷ (_ → Map key value)) >>> writeImpl
554556
@@ -556,6 +558,8 @@ instance (ReadForeign a) ⇒ ReadForeign (Map String a) where
556558 readImpl = (readImpl ∷ (_ → _ (Object a))) >>> map (foldrWithIndex Map.insert Map.empty)
557559else instance (ReadForeign a) ⇒ ReadForeign (Map Int a) where
558560 readImpl = (readImpl ∷ (_ → _ (Object a))) >>> map (foldrWithIndex (unsafeStringToInt >>> Map.insert) Map.empty)
561+else instance (ReadForeign a) ⇒ ReadForeign (Map BigInt a) where
562+ readImpl = (readImpl ∷ (_ → _ (Object a))) >>> map (foldrWithIndex (unsafeStringToBigInt >>> Map.insert) Map.empty)
559563else instance (Newtype nt key, ReadForeign (Map key value)) ⇒ ReadForeign (Map nt value) where
560564 readImpl = (readImpl ∷ (_ → _ (Map key value))) >>> map (unsafeCoerce ∷ (Map key value → Map nt value))
561565
@@ -619,6 +623,10 @@ unsafeStringToInt ∷ String → Int
619623unsafeStringToInt = Int.fromString >>>
620624 (fromMaybe' \_ → unsafeCrashWith "impossible")
621625
626+unsafeStringToBigInt ∷ String → BigInt
627+unsafeStringToBigInt = BigInt.fromString >>>
628+ (fromMaybe' \_ → unsafeCrashWith "impossible")
629+
622630sequenceCombining ∷
623631 ∀ f a.
624632 Monoid (f a) ⇒
test/BasicsSpec.purs+13−1
@@ -66,6 +66,8 @@ spec = describe "En- and decoding" $ do
6666 $ roundtrips (Map.fromFoldable [ (Stringy "A" /\ "B"), (Stringy "C" /\ "D") ])
6767 it "roundtrips Map with Int newtype keys"
6868 $ roundtrips (Map.fromFoldable [ (Inty 4 /\ "B"), (Inty 8 /\ "D") ])
69+ it "roundtrips Map with BigInt newtype keys"
70+ $ roundtrips (Map.fromFoldable [ (BigInty (BigInt.fromInt 5) /\ "B"), (BigInty big /\ "D") ])
6971 it "roundtrips BigInt" do
7072 let
7173 inputStr = """{ "number":1, "big": 18014398509481982, "mediumBig": 1652955871799, "smallBig": 10 }"""
@@ -79,7 +81,6 @@ spec = describe "En- and decoding" $ do
7981 it "roundtrips BigInt (2)" do
8082 let
8183 smallBig = BigInt.fromInt 10
82− big = unsafePartial $ fromJust $ BigInt.fromString "18014398509481982"
8384
8485 expected = { big, smallBig }
8586 json = writeJSON expected
@@ -217,3 +218,14 @@ derive newtype instance Eq Inty
217218derive newtype instance Ord Inty
218219derive newtype instance WriteForeign Inty
219220derive newtype instance ReadForeign Inty
221+
222+newtype BigInty = BigInty BigInt
223+
224+derive instance Newtype BigInty _
225+derive newtype instance Show BigInty
226+derive newtype instance Eq BigInty
227+derive newtype instance Ord BigInty
228+derive newtype instance WriteForeign BigInty
229+derive newtype instance ReadForeign BigInty
230+
231+big = unsafePartial $ fromJust $ BigInt.fromString "18014398509481982"