|
1 | | -module Json (module Json.Internal) where |
2 | | - |
3 | | -import Json.Internal |
4 | | - ( Json |
5 | | - |
6 | | - , parse |
7 | | - , print |
8 | | - , printIndented |
9 | | - |
| 1 | +module JSON |
| 2 | + ( parse |
| 3 | + , null |
| 4 | + , fromBoolean |
| 5 | + , fromNumberWithDefault |
| 6 | + , fromNumber |
| 7 | + , fromInt |
| 8 | + , fromString |
| 9 | + , fromArray |
| 10 | + , fromObject |
10 | 11 | , case_ |
11 | 12 | , toNull |
12 | 13 | , toBoolean |
13 | 14 | , toNumber |
14 | 15 | , toString |
15 | 16 | , toArray |
16 | 17 | , toObject |
| 18 | + , print |
| 19 | + , printIndented |
| 20 | + , module Internal |
| 21 | + ) where |
17 | 22 |
|
18 | | - , null |
19 | | - , fromNumber |
20 | | - , fromNumberWithDefault |
21 | | - , fromInt |
22 | | - , fromBoolean |
23 | | - , fromString |
24 | | - , fromArray |
25 | | - , fromObject |
26 | | - ) |
| 23 | +import Prelude |
| 24 | + |
| 25 | +import Data.Either (Either(..)) |
| 26 | +import Data.Function.Uncurried (runFn2, runFn3, runFn7) |
| 27 | +import Data.Maybe (Maybe(..)) |
| 28 | +import JSON.Internal (JSON) as Internal |
| 29 | +import JSON.Internal (JSON, Object, _case, _fromNumberWithDefault, _parse) |
| 30 | + |
| 31 | +-- | Attempts to parse a string as a JSON value. If parsing fails, an error message detailing the |
| 32 | +-- | cause may be returned in the `Left` of the result. |
| 33 | +parse :: String -> Either String JSON |
| 34 | +parse j = runFn3 _parse Left Right j |
| 35 | + |
| 36 | +-- | The JSON `null` value. |
| 37 | +null :: JSON |
| 38 | +null = _null |
| 39 | + |
| 40 | +foreign import _null :: JSON |
| 41 | + |
| 42 | +-- | Creates a `JSON` value from a `Boolean`. |
| 43 | +foreign import fromBoolean :: Boolean -> JSON |
| 44 | + |
| 45 | +-- | Creates a `JSON` value from a `Number`, using a fallback `Int` value for cases where the |
| 46 | +-- | PureScript number value is not valid for JSON. |
| 47 | +fromNumberWithDefault :: Int -> Number -> JSON |
| 48 | +fromNumberWithDefault fallback n = runFn2 _fromNumberWithDefault fallback n |
| 49 | + |
| 50 | +-- | Creates a `JSON` value from a `Number`. |
| 51 | +-- | |
| 52 | +-- | The PureScript `Number` type admits infinities and a `NaN` value which are not allowed in JSON, |
| 53 | +-- | so when encountered, this function will treat those values as 0. |
| 54 | +fromNumber :: Number -> JSON |
| 55 | +fromNumber n = runFn2 _fromNumberWithDefault 0 n |
| 56 | + |
| 57 | +-- | Creates a `JSON` value from an `Int`. |
| 58 | +-- | |
| 59 | +-- | There is no corresponding `toInt` as JSON doesn't have a concept of integers - this is provided |
| 60 | +-- | as a convenience to avoid having to convert `Int` to `Number` before creating a `JSON` value. |
| 61 | +foreign import fromInt :: Int -> JSON |
| 62 | + |
| 63 | +-- | Creates a `JSON` value from a `String`. |
| 64 | +-- | |
| 65 | +-- | **Note**: this does not parse a string as a JSON value, it takes a PureScript string and |
| 66 | +-- | produces the corresponding `JSON` value for that string, similar to the other functions like |
| 67 | +-- | `fromBoolean` and `fromNumber`. |
| 68 | +-- | |
| 69 | +-- | To take a string that contains printed JSON and turn it into a `JSON` value, see |
| 70 | +-- | [`parse`](#v:parse). |
| 71 | +foreign import fromString :: String -> JSON |
| 72 | + |
| 73 | +-- | Creates a `JSON` value from an array of `JSON` values. |
| 74 | +foreign import fromArray :: Array JSON -> JSON |
| 75 | + |
| 76 | +-- | Creates a `JSON` value from an `Object`. |
| 77 | +foreign import fromObject :: Object -> JSON |
| 78 | + |
| 79 | +-- | Performs case analysis on a JSON value. |
| 80 | +-- | |
| 81 | +-- | As the `JSON` type is not a PureScript sum type, pattern matching cannot be used to |
| 82 | +-- | discriminate between the potential varieties of value. This function provides an equivalent |
| 83 | +-- | mechanism by accepting functions that deal with each variety, similar to an exaustive `case` |
| 84 | +-- | statement. |
| 85 | +case_ |
| 86 | + :: forall a |
| 87 | + . (Unit -> a) |
| 88 | + -> (Boolean -> a) |
| 89 | + -> (Number -> a) |
| 90 | + -> (String -> a) |
| 91 | + -> (Array JSON -> a) |
| 92 | + -> (Object -> a) |
| 93 | + -> JSON |
| 94 | + -> a |
| 95 | +case_ a b c d e f json = runFn7 _case a b c d e f json |
| 96 | + |
| 97 | +fail :: forall a b. a -> Maybe b |
| 98 | +fail _ = Nothing |
| 99 | + |
| 100 | +-- | Converts a `JSON` value to `Null` if the `JSON` is `null`. |
| 101 | +toNull :: JSON -> Maybe Unit |
| 102 | +toNull json = runFn7 _case Just fail fail fail fail fail json |
| 103 | + |
| 104 | +-- | Converts a `JSON` value to `Boolean` if the `JSON` is a boolean. |
| 105 | +toBoolean :: JSON -> Maybe Boolean |
| 106 | +toBoolean json = runFn7 _case fail Just fail fail fail fail json |
| 107 | + |
| 108 | +-- | Converts a `JSON` value to `Number` if the `JSON` is a number. |
| 109 | +toNumber :: JSON -> Maybe Number |
| 110 | +toNumber json = runFn7 _case fail fail Just fail fail fail json |
| 111 | + |
| 112 | +-- | Converts a `JSON` value to `String` if the `JSON` is a string. |
| 113 | +toString :: JSON -> Maybe String |
| 114 | +toString json = runFn7 _case fail fail fail Just fail fail json |
| 115 | + |
| 116 | +-- | Converts a `JSON` value to `Array JSON` if the `JSON` is an array. |
| 117 | +toArray :: JSON -> Maybe (Array JSON) |
| 118 | +toArray json = runFn7 _case fail fail fail fail Just fail json |
| 119 | + |
| 120 | +-- | Converts a `JSON` value to `Object` if the `JSON` is an object. |
| 121 | +toObject :: JSON -> Maybe Object |
| 122 | +toObject json = runFn7 _case fail fail fail fail fail Just json |
| 123 | + |
| 124 | +-- | Prints a JSON value as a compact (single line) string. |
| 125 | +foreign import print :: JSON -> String |
| 126 | + |
| 127 | +-- | Prints a JSON value as a "pretty" string, |
| 128 | +foreign import printIndented :: JSON -> String |
0 commit comments