From 24427cdfe380236b388d55d215c8b054fac646e1 Mon Sep 17 00:00:00 2001 From: Jamie Bertram Date: Wed, 27 Oct 2021 13:19:20 -0400 Subject: [PATCH] Add withJsonPatch --- package-lock.json | 13 +++++++++++++ package.json | 3 ++- src/Data/BigInt/Argonaut.js | 16 +++++++++++++++- src/Data/BigInt/Argonaut.purs | 14 ++++++++++++++ test/Main.purs | 9 ++++++--- 5 files changed, 50 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index d6a2fb6..261f391 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,6 +7,19 @@ "version": "1.6.50", "resolved": "https://registry.npmjs.org/big-integer/-/big-integer-1.6.50.tgz", "integrity": "sha512-+O2uoQWFRo8ysZNo/rjtri2jIwjr3XfeAgRjAUADRqGG+ZITvyn8J1kvXLTaKVr3hhGXk+f23tKfdzmklVM9vQ==" + }, + "bignumber.js": { + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.1.tgz", + "integrity": "sha512-IdZR9mh6ahOBv/hYGiXyVuyCetmGJhtYkqLBpTStdhEGjegpPlUawydyaF3pbIOFynJTpllEs+NP+CS9jKFLjA==" + }, + "json-bigint": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/json-bigint/-/json-bigint-1.0.0.tgz", + "integrity": "sha512-SiPv/8VpZuWbvLSMtTDU8hEfrZWg/mH/nV/b4o0CYbSxu1UIQPLdwKOCIyLQX+VIPO5vrLX3i8qtqFyhdPSUSQ==", + "requires": { + "bignumber.js": "^9.0.0" + } } } } diff --git a/package.json b/package.json index 2bfd300..1a27fa6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "name": "web-common", "dependencies": { - "big-integer": "^1.6.50" + "big-integer": "^1.6.50", + "json-bigint": "^1.0.0" } } diff --git a/src/Data/BigInt/Argonaut.js b/src/Data/BigInt/Argonaut.js index 5809458..1a137e5 100644 --- a/src/Data/BigInt/Argonaut.js +++ b/src/Data/BigInt/Argonaut.js @@ -1,6 +1,20 @@ "use strict"; -const bigInt = require("big-integer"); +var bigInt = require("big-integer"); +var JSONbig = require("json-bigint"); + +exports.patchJson = function () { + var stringify = JSON.stringify; + var parse = JSON.parse; + JSON.stringify = JSONbig.stringify; + JSON.parse = JSONbig.parse; + return { stringify: stringify, parse: parse }; +} + +exports.restoreJson = function (original) { + JSON.stringify = original.stringify; + JSON.parse = original.parse; +} exports.decodeBigInt = function (fail, succ, json) { if (typeof json === "number" || typeof json === "bigint") { diff --git a/src/Data/BigInt/Argonaut.purs b/src/Data/BigInt/Argonaut.purs index c09c39c..17432bb 100644 --- a/src/Data/BigInt/Argonaut.purs +++ b/src/Data/BigInt/Argonaut.purs @@ -23,10 +23,12 @@ module Data.BigInt.Argonaut , toNonEmptyString , toNumber , toString + , withJsonPatch , xor ) where import Prologue + import Data.Argonaut.Aeson (maybeToEither) import Data.Argonaut.Core (Json) import Data.Argonaut.Decode (class DecodeJson, JsonDecodeError(..)) @@ -37,6 +39,10 @@ import Data.Function.Uncurried (Fn3, runFn3) import Data.Generic.Rep (class Generic) import Data.Newtype (class Newtype, over, unwrap) import Data.String.NonEmpty (NonEmptyString) +import Effect (Effect) +import Effect.Aff (Aff, bracket) +import Effect.Class (liftEffect) +import Foreign (Foreign) newtype BigInt = BigInt BI.BigInt @@ -63,6 +69,14 @@ foreign import decodeBigInt :: forall a. Fn3 a (BI.BigInt -> a) Json a foreign import encodeBigInt :: BI.BigInt -> Json +foreign import patchJson :: Effect Foreign + +foreign import restoreJson :: Foreign -> Effect Unit + +withJsonPatch :: forall a. Aff a -> Aff a +withJsonPatch = + bracket (liftEffect patchJson) (liftEffect <<< restoreJson) <<< const + instance decodeJsonBigInt :: DecodeJson BigInt where decodeJson = maybeToEither (TypeMismatch "BigInt or Number") diff --git a/test/Main.purs b/test/Main.purs index e1581e5..4cf7cb5 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -1,10 +1,12 @@ module Test.Main where import Prologue + import Data.Array.Extra.Spec (arrayExtraSpec) +import Data.BigInt.Argonaut (withJsonPatch) +import Data.Cursor.Spec (cursorSpec) import Data.Foldable.Extra.Spec (foldableExtraSpec) import Data.String.Extra.Spec (stringExtraSpec) -import Data.Cursor.Spec (cursorSpec) import Effect (Effect) import Effect.Aff (launchAff_) import PlutusTx.AssocMap.Spec (assocMapSpec) @@ -13,8 +15,9 @@ import Test.Spec.Runner (runSpec) main :: Effect Unit main = - launchAff_ - $ runSpec [ consoleReporter ] do + launchAff_ do + withJsonPatch do + runSpec [ consoleReporter ] do cursorSpec arrayExtraSpec foldableExtraSpec