Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
68a1e47
add node stuff to gitignore
shmish111 May 10, 2019
736a8dd
rebase upstream master and add spago
shmish111 Aug 14, 2020
090a0c9
add List decode/encode
shmish111 Aug 14, 2020
f34d0c3
encode/decode tuple using array as this is what is normally done
shmish111 Aug 14, 2020
9fcc6ee
encode/decode Map using array of tuples, not sure there is another way
shmish111 Aug 14, 2020
fd7f49f
encode/decode Set using array
shmish111 Aug 14, 2020
d356fd2
upgrade purs
shmish111 Aug 17, 2020
db3685a
use spago in travis
shmish111 Aug 17, 2020
e9064e2
Adding a roundtrip test for Map.
krisajenkins Sep 3, 2020
26bc66f
More flexible Map decoding.
krisajenkins Sep 3, 2020
401b78b
Merge pull request #1 from krisajenkins/fix-map
shmish111 Sep 3, 2020
16989ec
Adding encoders/decoders for Either.
krisajenkins Sep 3, 2020
c3c6431
Merge pull request #2 from krisajenkins/add-either
shmish111 Sep 3, 2020
1e0b95f
Adding Data.BigInt support.
krisajenkins Sep 3, 2020
3d70577
Merge pull request #3 from shmish111/add-big-ints
shmish111 Sep 3, 2020
ecd14ae
Adding a shell.nix file, for a repeatable env.
krisajenkins Sep 3, 2020
b01fe37
Merge pull request #4 from shmish111/nix-shell
shmish111 Sep 3, 2020
b77bf99
Adding extra map-decoding tests.
krisajenkins Sep 4, 2020
4963d86
Improving the resiliency of BigInt decoding.
krisajenkins Sep 4, 2020
bd0a108
Improved encoding/decoding for big integers.
krisajenkins Sep 8, 2020
1f6a328
Moving to a more formal test suite.
krisajenkins Sep 11, 2020
a2c5a0d
BigInteger support.
krisajenkins Sep 10, 2020
695e30b
Refactoring the test suite.
krisajenkins Sep 17, 2020
f5b1c43
Fixing a bug with aeson-style encoding on nested sum types.
krisajenkins Sep 17, 2020
57692ed
Simplifying a fromMaybe clause.
krisajenkins Sep 17, 2020
c7cb623
add node stuff to gitignore
shmish111 May 10, 2019
9054539
rebase upstream master and add spago
shmish111 Aug 14, 2020
57539e7
add List decode/encode
shmish111 Aug 14, 2020
209e040
encode/decode tuple using array as this is what is normally done
shmish111 Aug 14, 2020
33f2a7f
encode/decode Map using array of tuples, not sure there is another way
shmish111 Aug 14, 2020
60a7a0f
encode/decode Set using array
shmish111 Aug 14, 2020
0b95402
upgrade purs
shmish111 Aug 17, 2020
58a9b33
use spago in travis
shmish111 Aug 17, 2020
0377029
Adding a roundtrip test for Map.
krisajenkins Sep 3, 2020
961aa70
More flexible Map decoding.
krisajenkins Sep 3, 2020
652a993
Adding encoders/decoders for Either.
krisajenkins Sep 3, 2020
538d5e8
Adding Data.BigInt support.
krisajenkins Sep 3, 2020
b0584ad
Adding a shell.nix file, for a repeatable env.
krisajenkins Sep 3, 2020
bcd6e20
Adding extra map-decoding tests.
krisajenkins Sep 4, 2020
f840a43
Improving the resiliency of BigInt decoding.
krisajenkins Sep 4, 2020
2d2e351
Improved encoding/decoding for big integers.
krisajenkins Sep 8, 2020
cd8ed03
Moving to a more formal test suite.
krisajenkins Sep 11, 2020
040060d
BigInteger support.
krisajenkins Sep 10, 2020
2a62593
Refactoring the test suite.
krisajenkins Sep 17, 2020
22aac68
Fixing a bug with aeson-style encoding on nested sum types.
krisajenkins Sep 17, 2020
cc4519d
Simplifying a fromMaybe clause.
krisajenkins Sep 17, 2020
bd412a1
Merge pull request #7 from shmish111/rebase-upstream
shmish111 Oct 26, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improving the resiliency of BigInt decoding.
It will now decode ints as well.

Note that this is just us being more tolerant of recieving something
like `1234`. If the number sent is actually in the "big" integer range
(>2^32) then it won't work and it's the sender's fault. The JSON spec
doesn't support numbers that big. They'd have to be sent as strings.
  • Loading branch information
krisajenkins authored and shmish111 committed Oct 26, 2020
commit f840a4307c4d7d93eef76364be3295c4f58b8306
11 changes: 8 additions & 3 deletions src/Foreign/Generic/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,14 @@ instance eitherDecode :: (Decode a, Decode b) => Decode (Either a b) where
(readProp "Right" value >>= (map Right <<< decode))

instance bigIntDecode :: Decode BigInt where
decode value = do
str <- readString value
except $ note (pure (ForeignError ("Expected BigInt"))) $ BigInt.fromString str
decode json = decodeAsString json <|> decodeAsDigits json
where
decodeAsString value = do
str <- readString value
except $ note (pure (ForeignError ("Expected BigInt"))) $ BigInt.fromString str
decodeAsDigits value = do
int <- readInt value
pure $ BigInt.fromInt int

-- | The `Encode` class is used to generate encoding functions
-- | of the form `a -> Foreign` using `generics-rep` deriving.
Expand Down
5 changes: 4 additions & 1 deletion test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ main = do
, actual: runExcept (decodeJSON "null")
}
testRoundTrip [ Left 5, Right "Test" ]
testRoundTrip (BigInt.pow (BigInt.fromInt 2) (BigInt.fromInt 60)) -- 2^60. Anything over 2^32 will confuse JavaScript.
testRoundTrip (BigInt.pow (BigInt.fromInt 2) (BigInt.fromInt 60)) -- 2^60. Anything over 2^32 would baffle JavaScript.
assertEqual { expected: Right (BigInt.fromInt 50)
, actual: runExcept (decodeJSON "50")
}
testUnaryConstructorLiteral
let opts = defaultOptions { fieldTransform = toUpper }
testGenericRoundTrip opts (RecordTest { foo: 1, bar: "test", baz: 'a' })
Expand Down