Skip to content

Commit 6e47ea5

Browse files
authored
Add more helper functions needed for aeson-bridge mlabs-haskell#1
2 parents 254a28e + 9bec021 commit 6e47ea5

File tree

1 file changed

+88
-9
lines changed

1 file changed

+88
-9
lines changed

Diff for: src/Aeson.purs

+88-9
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,20 @@ module Aeson
5959
, jsonToAeson
6060
, parseJsonStringToAeson
6161
, stringifyAeson
62-
, toObject
6362
, toStringifiedNumbersJson
63+
, isNull
64+
, isBoolean
65+
, isNumber
66+
, isString
67+
, isArray
68+
, isObject
69+
, toNull
70+
, toBoolean
71+
, toNumber
72+
, toString
73+
, toArray
74+
, toObject
75+
, fromString
6476
) where
6577

6678
import Prelude
@@ -80,10 +92,10 @@ import Data.Argonaut
8092
, encodeJson
8193
, fromArray
8294
, fromObject
83-
, isNull
8495
, jsonNull
8596
, stringify
8697
)
98+
import Data.Argonaut (isNull, fromString) as Argonaut
8799
import Data.Argonaut.Encode.Encoders (encodeBoolean, encodeString, encodeUnit)
88100
import Data.Argonaut.Parser (jsonParser)
89101
import Data.Array (foldr, fromFoldable)
@@ -93,7 +105,7 @@ import Data.BigInt (BigInt)
93105
import Data.BigInt as BigInt
94106
import Data.Either (Either(Right, Left), fromRight, note)
95107
import Data.Foldable (fold, foldM)
96-
import Data.Int (round, toNumber)
108+
import Data.Int (round)
97109
import Data.Int as Int
98110
import Data.Maybe (Maybe(Just, Nothing), fromJust, maybe)
99111
import Data.Sequence (Seq)
@@ -259,7 +271,7 @@ getFieldOptional' = getFieldOptional'_ decodeAeson
259271
maybe (pure Nothing) decode (FO.lookup str obj)
260272
where
261273
decode aeson@(Aeson { patchedJson: AesonPatchedJson json }) =
262-
if isNull json then
274+
if Argonaut.isNull json then
263275
pure Nothing
264276
else
265277
Just <$> (lmap (AtKey str) <<< decoder) aeson
@@ -331,10 +343,6 @@ constAesonCases v =
331343
c :: forall (b :: Type). b -> a
332344
c = const v
333345

334-
toObject :: Aeson -> Maybe (Object Aeson)
335-
toObject =
336-
caseAeson $ constAesonCases Nothing # _ { caseObject = Just }
337-
338346
caseAesonObject :: forall (a :: Type). a -> (Object Aeson -> a) -> Aeson -> a
339347
caseAesonObject def f = caseAeson (constAesonCases def # _ { caseObject = f })
340348

@@ -368,6 +376,77 @@ caseAesonBigInt def f = caseAesonNumber def \str ->
368376
caseAesonNull :: forall (a :: Type). a -> (Unit -> a) -> Aeson -> a
369377
caseAesonNull def f = caseAeson (constAesonCases def # _ { caseNull = f })
370378

379+
verbAesonType :: forall a b. b -> (a -> b) -> (b -> (a -> b) -> Aeson -> b) -> Aeson -> b
380+
verbAesonType def f g = g def f
381+
382+
isAesonType :: forall a. (Boolean -> (a -> Boolean) -> Aeson -> Boolean) -> Aeson -> Boolean
383+
isAesonType = verbAesonType false (const true)
384+
385+
-- | Check if the provided `Json` is the `null` value
386+
isNull :: Aeson -> Boolean
387+
isNull = isAesonType caseAesonNull
388+
389+
-- | Check if the provided `Aeson` is a `Boolean`
390+
isBoolean :: Aeson -> Boolean
391+
isBoolean = isAesonType caseAesonBoolean
392+
393+
-- | Check if the provided `Aeson` is a `Number`
394+
isNumber :: Aeson -> Boolean
395+
isNumber = isAesonType caseAesonNumber
396+
397+
-- | Check if the provided `Aeson` is a `String`
398+
isString :: Aeson -> Boolean
399+
isString = isAesonType caseAesonString
400+
401+
-- | Check if the provided `Aeson` is an `Array`
402+
isArray :: Aeson -> Boolean
403+
isArray = isAesonType caseAesonArray
404+
405+
-- | Check if the provided `Aeson` is an `Object`
406+
isObject :: Aeson -> Boolean
407+
isObject = isAesonType caseAesonObject
408+
409+
toAesonType
410+
:: forall a
411+
. (Maybe a -> (a -> Maybe a) -> Aeson -> Maybe a)
412+
-> Aeson
413+
-> Maybe a
414+
toAesonType = verbAesonType Nothing Just
415+
416+
-- | Convert `Aeson` to the `Unit` value if the `Aeson` is the null value
417+
toNull :: Aeson -> Maybe Unit
418+
toNull = toAesonType caseAesonNull
419+
420+
-- | Convert `Aeson` to a `Boolean` value, if the `Aeson` is a boolean.
421+
toBoolean :: Aeson -> Maybe Boolean
422+
toBoolean = toAesonType caseAesonBoolean
423+
424+
-- | Convert `Aeson` to a `Number` value, if the `Aeson` is a number.
425+
toNumber :: Aeson -> Maybe String
426+
toNumber = toAesonType caseAesonNumber
427+
428+
-- | Convert `Aeson` to a `String` value, if the `Aeson` is a string. To write a
429+
-- | `Aeson` value to a JSON string, see `stringify`.
430+
toString :: Aeson -> Maybe String
431+
toString = toAesonType caseAesonString
432+
433+
-- | Convert `Aeson` to an `Array` of `Aeson` values, if the `Aeson` is an array.
434+
toArray :: Aeson -> Maybe (Array Aeson)
435+
toArray = toAesonType caseAesonArray
436+
437+
-- | Convert `Aeson` to an `Object` of `Aeson` values, if the `Aeson` is an object.
438+
toObject :: Aeson -> Maybe (Object Aeson)
439+
toObject = toAesonType caseAesonObject
440+
441+
-- | Construct the `Json` representation of a `String` value.
442+
-- | Note that this function only produces `Json` containing a single piece of `String`
443+
-- | data (similar to `fromBoolean`, `fromNumber`, etc.).
444+
-- | This function does NOT convert the `String` encoding of a JSON value to `Json` - For that
445+
-- | purpose, you'll need to use `jsonParser`.
446+
fromString :: String -> Aeson
447+
fromString str = Aeson
448+
{ patchedJson: AesonPatchedJson (Argonaut.fromString str), numberIndex: mempty }
449+
371450
-------- Decode helpers --------
372451

373452
-- | Ignore numeric index and reuse Argonaut decoder.
@@ -561,7 +640,7 @@ instance EncodeAeson Aeson where
561640
encodeString
562641
(fromArray <<< map bumpIndices)
563642
(fromObject <<< map bumpIndices)
564-
encodeNumber n = encodeJson $ toNumber ix + n
643+
encodeNumber n = encodeJson $ Int.toNumber ix + n
565644
bumpNumberIndexBy (Seq.length numberIndex)
566645
pure $
567646
(Aeson { patchedJson: AesonPatchedJson (bumpIndices json), numberIndex })

0 commit comments

Comments
 (0)