Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 39 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"dependencies": {
"@mlabs-haskell/json-bigint": "^1.0.0"
"@mlabs-haskell/json-bigint": "^1.0.0",
"big-integer": "^1.6.51"
}
}
14 changes: 7 additions & 7 deletions packages.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ in upstream
-------------------------------
-}
let upstream =
https://github.com/purescript/package-sets/releases/download/psc-0.14.5-20211116/packages.dhall
sha256:7ba810597a275e43c83411d2ab0d4b3c54d0b551436f4b1632e9ff3eb62e327a
https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20230105/packages.dhall
sha256:3e9fbc9ba03e9a1fcfd895f65e2d50ee2f5e86c4cd273f3d5c841b655a0e1bda

let additions =
{ sequences =
Expand All @@ -129,8 +129,8 @@ let additions =
, "unfoldable"
, "unsafe-coerce"
]
, repo = "https://github.com/hdgarrood/purescript-sequences"
, version = "v3.0.2"
, repo = "https://github.com/garganscript/purescript-sequences"
, version = "cae456c1a7463785ad33981a93e7a9cb5fc7872c"
}
, mote =
{ dependencies = [ "these", "transformers", "arrays" ]
Expand All @@ -149,9 +149,9 @@ let additions =
, "prelude"
, "tuples"
]
, repo = "https://github.com/mlabs-haskell/purescript-bignumber"
, version = "705923edd892a3397b90d28ce7db9a7181dcd599"
, repo = "https://github.com/errfrom/purescript-bignumber"
, version = "9b3179ad07428d189e42a7a205aab9c7c4849d4a"
}
}

in upstream // additions
in upstream // additions
40 changes: 21 additions & 19 deletions src/Aeson.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const {BigNumber} = require("bignumber.js")
import BigNumber from "bignumber.js";

const JSONbig = require("@mlabs-haskell/json-bigint")({})
import JSONbig from "@mlabs-haskell/json-bigint";

//---

const identity = x => x
exports.fromBoolean = identity
exports.fromString = identity
exports.fromFiniteBigNumber = identity
exports.fromArray = identity
exports.fromObject = identity
exports.aesonNull = null

const _caseAeson =
export const fromBoolean = identity
export const fromString = identity
export const fromFiniteBigNumber = identity
export const fromArray = identity
export const fromObject = identity
export const aesonNull = null

export const _caseAeson =
caseNull =>
caseBoolean =>
caseBigNumber =>
Expand Down Expand Up @@ -41,7 +41,6 @@ const _caseAeson =
throw "Imposible happened: JSON object is incorrect: "
+ json.toString() + " " + typeof json;
}
exports._caseAeson = _caseAeson

// Hack zone.
// BigNumberFixed is instanceof BigNumber but
Expand Down Expand Up @@ -104,16 +103,17 @@ const traverseFormattingBigNumber = json => {
return result
}

exports.stringifyAeson = json => JSONbig.stringify(traverseFormattingBigNumber(json))
export function stringifyAeson(json) {
return JSONbig.stringify(traverseFormattingBigNumber(json));
}

exports.parseAeson = Nothing => Just => jsonStr => {
export const parseAeson = Nothing => Just => jsonStr => {
try {
return Just(JSONbig.parse(jsonStr))
} catch (err) {
return Nothing
}
}

// ---

const constant = x => _ => x
Expand All @@ -135,13 +135,13 @@ const arrEq = (a, b) =>{
// Loop here is better than something like:
//
// return Array.from(a)
// .reduce((acc, ai, i) => acc && aesonEq(ai, b[i]), true)
// .reduce((acc, ai, i) => acc && aesonEqUncurried(ai, b[i]), true)
//
// ... because it allows us to fail fast
// arrays are not equal
// as soon as we encounter first inequality
for (let i = 0; i < a.length; i++)
if (!aesonEq(a[i], b[i]))
if (!aesonEqUncurried(a[i], b[i]))
return false

// Are you still here?
Expand All @@ -167,7 +167,7 @@ const objectEq = (a, b) => {

for (let i = 0; i < aKeys.length; i++) {
let key = aKeys[i]
if (!aesonEq(a[key], b[key]))
if (!aesonEqUncurried(a[key], b[key]))
return false
}

Expand All @@ -190,7 +190,7 @@ const typeOf = _caseAeson
(constant(tArr))
(constant(tObj))

const aesonEq = (a, b) => {
const aesonEqUncurried = (a, b) => {
// If "constructors" are different
// aesons are not equal
const tOfA = typeOf(a)
Expand All @@ -209,4 +209,6 @@ const aesonEq = (a, b) => {
throw "Imposible happened: Unexpected type of JSON: " + a.toString
}

exports.aesonEq = a => b => aesonEq(a, b)
export function aesonEq(a) {
return b => aesonEqUncurried(a, b);
}
10 changes: 6 additions & 4 deletions src/Aeson.purs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ import Data.Argonaut (JsonDecodeError(..), printJsonDecodeError) as DataArgonaut
import Data.Argonaut (fromArray, fromObject, jsonNull) as Argonaut
import Data.Argonaut.Encode.Encoders (encodeBoolean, encodeString)
import Data.Array (cons, fromFoldable, head, length, tail, toUnfoldable, (!!))
import Data.Bifunctor (lmap)
import Data.Bifunctor (lmap, rmap)
import Data.BigInt (BigInt)
import Data.BigInt as BigInt
import Data.BigNumber (BigNumber, isInteger)
import Data.BigNumber as BigNumber
import Data.Bitraversable (rtraverse)
import Data.Either (Either(Right, Left), fromRight, note)
import Data.Foldable (foldM, intercalate)
import Data.Int as Int
Expand Down Expand Up @@ -672,7 +673,8 @@ instance (Ord a, DecodeAeson a) => DecodeAeson (Set a) where
decodeAeson x = Set.fromFoldable <$> (decodeAeson x :: _ (Array _))

instance (Ord k, DecodeAeson k, DecodeAeson v) => DecodeAeson (Map k v) where
decodeAeson x = Map.fromFoldable <$> (decodeAeson x :: _ (Array _))
decodeAeson x = Map.fromFoldable <$>
(traverse (rtraverse decodeAeson) =<< (decodeAeson x :: _ (Array (Tuple k Aeson))))

instance DecodeAeson a => DecodeAeson (Maybe a) where
decodeAeson aeson =
Expand Down Expand Up @@ -818,7 +820,7 @@ else instance
, EncodeAeson b
) =>
EncodeTupleAux (Tuple a b) where
tupleToArray (Tuple a b) = [ encodeAeson a, encodeAeson b ]
tupleToArray (Tuple a b) = [ encodeAeson a, encodeAeson b ]

instance EncodeTupleAux (Tuple a b) => EncodeAeson (Tuple a b) where
-- Encodes nested tuple of arbitrary size, (like Boolean /\ Boolean /\ Boolean /\ ...)
Expand All @@ -840,7 +842,7 @@ instance (EncodeAeson a) => EncodeAeson (Set a) where
encodeAeson x = encodeAeson $ (Set.toUnfoldable x :: Array _)

instance (EncodeAeson k, EncodeAeson v) => EncodeAeson (Map k v) where
encodeAeson x = encodeAeson $ (Map.toUnfoldable x :: Array _)
encodeAeson x = encodeAeson $ map (rmap encodeAeson) (Map.toUnfoldable x :: Array _)

instance EncodeAeson a => EncodeAeson (Maybe a) where
encodeAeson Nothing = aesonNull
Expand Down
4 changes: 2 additions & 2 deletions test/Gen.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import Aeson (Aeson, Finite, aesonNull, finiteBigNumber, fromArray, fromBoolean,
import Control.Lazy (defer, fix)
import Data.Argonaut (Json)
import Data.Argonaut.Gen (genJson)
import Data.Array.NonEmpty as LNA
import Data.BigNumber (BigNumber)
import Data.BigNumber as BigNumber
import Data.Either (fromRight)
import Data.Int as Int
import Data.List.NonEmpty as LNE
import Data.Maybe (fromJust, maybe)
import Data.String as S
import Data.Tuple (Tuple(Tuple))
Expand All @@ -22,7 +22,7 @@ import Test.QuickCheck (arbitrary)
import Test.QuickCheck.Gen (Gen, chooseInt, frequency, resize, sized, vectorOf)

frequency' :: Partial => forall a. Array (Tuple Number (Gen a)) -> Gen a
frequency' = frequency <<< fromJust <<< LNE.fromFoldable
frequency' = frequency <<< fromJust <<< LNA.fromFoldable

oneOf :: forall a. Partial => Array (Gen a) -> Gen a
oneOf arr = frequency' $ Tuple 1.0 <$> arr
Expand Down