@@ -2,65 +2,69 @@ module JSON
22 ( parse
33 , null
44 , fromBoolean
5- , fromNumberWithDefault
65 , fromNumber
6+ , fromNumberWithDefault
77 , fromInt
88 , fromString
99 , fromArray
10- , fromObject
10+ , fromJArray
11+ , fromJObject
1112 , case_
1213 , toNull
1314 , toBoolean
1415 , toNumber
1516 , toString
1617 , toArray
17- , toObject
18+ , toJArray
19+ , toJObject
1820 , print
1921 , printIndented
20- , module Internal
22+ , module Exports
2123 ) where
2224
2325import Prelude
2426
2527import Data.Either (Either (..))
2628import Data.Function.Uncurried (runFn2 , runFn3 , runFn7 )
2729import Data.Maybe (Maybe (..))
28- import JSON.Internal (JSON ) as Internal
29- import JSON.Internal (JSON , Object , _case , _fromNumberWithDefault , _parse )
30+ import JSON.Internal (JArray , JObject , JSON ) as Exports
31+ import JSON.Internal (JArray , JObject , JSON )
32+ import JSON.Internal as Internal
3033
3134-- | Attempts to parse a string as a JSON value. If parsing fails, an error message detailing the
3235-- | cause may be returned in the `Left` of the result.
3336parse :: String -> Either String JSON
34- parse j = runFn3 _parse Left Right j
37+ parse j = runFn3 Internal . _parse Left Right j
3538
3639-- | The JSON `null` value.
3740null :: JSON
3841null = _null
3942
43+ -- | The JSON `null` value.
4044foreign import _null :: JSON
4145
42- -- | Creates a `JSON` value from a `Boolean `.
46+ -- | Converts a `Boolean` into `JSON `.
4347foreign import fromBoolean :: Boolean -> JSON
4448
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`.
49+ -- | Converts a `Number` into `JSON`.
5150-- |
5251-- | The PureScript `Number` type admits infinities and a `NaN` value which are not allowed in JSON,
5352-- | so when encountered, this function will treat those values as 0.
5453fromNumber :: Number -> JSON
55- fromNumber n = runFn2 _fromNumberWithDefault 0 n
54+ fromNumber n = runFn2 Internal . _fromNumberWithDefault 0 n
5655
57- -- | Creates a `JSON` value from an `Int`.
56+ -- | Creates a `Number` into `JSON`, using a fallback `Int` value for cases where the
57+ -- | PureScript number value is not valid for JSON (`NaN`, `infinity`).
58+ fromNumberWithDefault :: Int -> Number -> JSON
59+ fromNumberWithDefault fallback n = runFn2 Internal ._fromNumberWithDefault fallback n
60+
61+ -- | Converts an `Int` into `JSON`.
5862-- |
5963-- | There is no corresponding `toInt` as JSON doesn't have a concept of integers - this is provided
6064-- | as a convenience to avoid having to convert `Int` to `Number` before creating a `JSON` value.
6165foreign import fromInt :: Int -> JSON
6266
63- -- | Creates a `JSON` value from a `String `.
67+ -- | Converts a `String` into `JSON `.
6468-- |
6569-- | **Note**: this does not parse a string as a JSON value, it takes a PureScript string and
6670-- | produces the corresponding `JSON` value for that string, similar to the other functions like
@@ -70,59 +74,69 @@ foreign import fromInt :: Int -> JSON
7074-- | [`parse`](#v:parse).
7175foreign import fromString :: String -> JSON
7276
73- -- | Creates a `JSON` value from an array of `JSON` values.
74- foreign import fromArray :: Array JSON -> JSON
77+ -- | Converts a `JArray` into `JSON`.
78+ foreign import fromJArray :: JArray -> JSON
79+
80+ -- | Converts an array of `JSON` values into `JSON`.
81+ fromArray :: Array JSON -> JSON
82+ fromArray js = fromJArray (Internal .fromArray js)
7583
76- -- | Creates a `JSON` value from an `Object `.
77- foreign import fromObject :: Object -> JSON
84+ -- | Converts a `JObject` into `JSON `.
85+ foreign import fromJObject :: JObject -> JSON
7886
7987-- | Performs case analysis on a JSON value.
8088-- |
8189-- | As the `JSON` type is not a PureScript sum type, pattern matching cannot be used to
8290-- | discriminate between the potential varieties of value. This function provides an equivalent
8391-- | mechanism by accepting functions that deal with each variety, similar to an exaustive `case`
8492-- | statement.
93+ -- |
94+ -- | The `Unit` case is for `null` values.
8595case_
8696 :: forall a
8797 . (Unit -> a )
8898 -> (Boolean -> a )
8999 -> (Number -> a )
90100 -> (String -> a )
91- -> (Array JSON -> a )
92- -> (Object -> a )
101+ -> (JArray -> a )
102+ -> (JObject -> a )
93103 -> JSON
94104 -> a
95- case_ a b c d e f json = runFn7 _case a b c d e f json
105+ case_ a b c d e f json = runFn7 Internal . _case a b c d e f json
96106
97107fail :: forall a b . a -> Maybe b
98108fail _ = Nothing
99109
100110-- | Converts a `JSON` value to `Null` if the `JSON` is `null`.
101111toNull :: JSON -> Maybe Unit
102- toNull json = runFn7 _case Just fail fail fail fail fail json
112+ toNull json = runFn7 Internal . _case Just fail fail fail fail fail json
103113
104114-- | Converts a `JSON` value to `Boolean` if the `JSON` is a boolean.
105115toBoolean :: JSON -> Maybe Boolean
106- toBoolean json = runFn7 _case fail Just fail fail fail fail json
116+ toBoolean json = runFn7 Internal . _case fail Just fail fail fail fail json
107117
108118-- | Converts a `JSON` value to `Number` if the `JSON` is a number.
109119toNumber :: JSON -> Maybe Number
110- toNumber json = runFn7 _case fail fail Just fail fail fail json
120+ toNumber json = runFn7 Internal . _case fail fail Just fail fail fail json
111121
112122-- | Converts a `JSON` value to `String` if the `JSON` is a string.
113123toString :: JSON -> Maybe String
114- toString json = runFn7 _case fail fail fail Just fail fail json
124+ toString json = runFn7 Internal ._case fail fail fail Just fail fail json
125+
126+ -- | Converts a `JSON` value to `JArray` if the `JSON` is an array.
127+ toJArray :: JSON -> Maybe JArray
128+ toJArray json = runFn7 Internal ._case fail fail fail fail Just fail json
115129
116130-- | Converts a `JSON` value to `Array JSON` if the `JSON` is an array.
117131toArray :: JSON -> Maybe (Array JSON )
118- toArray json = runFn7 _case fail fail fail fail Just fail json
132+ toArray json = Internal .toArray <$> toJArray json
119133
120134-- | 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
135+ toJObject :: JSON -> Maybe JObject
136+ toJObject json = runFn7 Internal . _case fail fail fail fail fail Just json
123137
124138-- | Prints a JSON value as a compact (single line) string.
125139foreign import print :: JSON -> String
126140
127- -- | Prints a JSON value as a "pretty" string,
141+ -- | Prints a JSON value as a "pretty" string with newlines and indentation.
128142foreign import printIndented :: JSON -> String
0 commit comments