@@ -59,8 +59,20 @@ module Aeson
59
59
, jsonToAeson
60
60
, parseJsonStringToAeson
61
61
, stringifyAeson
62
- , toObject
63
62
, 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
64
76
) where
65
77
66
78
import Prelude
@@ -80,10 +92,10 @@ import Data.Argonaut
80
92
, encodeJson
81
93
, fromArray
82
94
, fromObject
83
- , isNull
84
95
, jsonNull
85
96
, stringify
86
97
)
98
+ import Data.Argonaut (isNull , fromString ) as Argonaut
87
99
import Data.Argonaut.Encode.Encoders (encodeBoolean , encodeString , encodeUnit )
88
100
import Data.Argonaut.Parser (jsonParser )
89
101
import Data.Array (foldr , fromFoldable )
@@ -93,7 +105,7 @@ import Data.BigInt (BigInt)
93
105
import Data.BigInt as BigInt
94
106
import Data.Either (Either (Right, Left), fromRight , note )
95
107
import Data.Foldable (fold , foldM )
96
- import Data.Int (round , toNumber )
108
+ import Data.Int (round )
97
109
import Data.Int as Int
98
110
import Data.Maybe (Maybe (Just, Nothing), fromJust , maybe )
99
111
import Data.Sequence (Seq )
@@ -259,7 +271,7 @@ getFieldOptional' = getFieldOptional'_ decodeAeson
259
271
maybe (pure Nothing ) decode (FO .lookup str obj)
260
272
where
261
273
decode aeson@(Aeson { patchedJson: AesonPatchedJson json }) =
262
- if isNull json then
274
+ if Argonaut . isNull json then
263
275
pure Nothing
264
276
else
265
277
Just <$> (lmap (AtKey str) <<< decoder) aeson
@@ -331,10 +343,6 @@ constAesonCases v =
331
343
c :: forall (b :: Type ). b -> a
332
344
c = const v
333
345
334
- toObject :: Aeson -> Maybe (Object Aeson )
335
- toObject =
336
- caseAeson $ constAesonCases Nothing # _ { caseObject = Just }
337
-
338
346
caseAesonObject :: forall (a :: Type ). a -> (Object Aeson -> a ) -> Aeson -> a
339
347
caseAesonObject def f = caseAeson (constAesonCases def # _ { caseObject = f })
340
348
@@ -368,6 +376,77 @@ caseAesonBigInt def f = caseAesonNumber def \str ->
368
376
caseAesonNull :: forall (a :: Type ). a -> (Unit -> a ) -> Aeson -> a
369
377
caseAesonNull def f = caseAeson (constAesonCases def # _ { caseNull = f })
370
378
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
+
371
450
-- ------ Decode helpers --------
372
451
373
452
-- | Ignore numeric index and reuse Argonaut decoder.
@@ -561,7 +640,7 @@ instance EncodeAeson Aeson where
561
640
encodeString
562
641
(fromArray <<< map bumpIndices)
563
642
(fromObject <<< map bumpIndices)
564
- encodeNumber n = encodeJson $ toNumber ix + n
643
+ encodeNumber n = encodeJson $ Int . toNumber ix + n
565
644
bumpNumberIndexBy (Seq .length numberIndex)
566
645
pure $
567
646
(Aeson { patchedJson: AesonPatchedJson (bumpIndices json), numberIndex })
0 commit comments