You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+21-31Lines changed: 21 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,7 @@ data Json
31
31
| JNumber Number
32
32
| JBoolean Boolean
33
33
| JArray (Array Json)
34
-
| JObject (StrMap Json)
34
+
| JObject (Object Json)
35
35
```
36
36
37
37
And indeed, some might even say this is the obvious approach.
@@ -45,25 +45,6 @@ both in terms of speed and memory churn.
45
45
46
46
Much of the design of Argonaut follows naturally from this design decision.
47
47
48
-
### Types
49
-
50
-
The most important type in this library is, of course, `Json`, which is the
51
-
type of JSON data in its native JavaScript representation.
52
-
53
-
As the (hypothetical) algebraic data type declaration above indicates, there
54
-
are six possibilities for a JSON value: it can be `null`, a string, a number, a
55
-
boolean, an array of JSON values, or an object mapping string keys to JSON
56
-
values.
57
-
58
-
For convenience, and to ensure that values have the appropriate underlying
59
-
data representations, Argonaut also declares types for each of these individual
60
-
possibilities, whose names correspond to the data constructor names above.
61
-
62
-
Therefore, `JString`, `JNumber`, and `JBoolean` are synonyms for the primitive
63
-
PureScript types `String`, `Number`, and `Boolean` respectively; `JArray` is a
64
-
synonym for `Array Json`; and `JObject` is a synonym for `StrMap Json`.
65
-
Argonaut defines a type `JNull` as the type of the `null` value in JavaScript.
66
-
67
48
### Introducing Json values
68
49
69
50
(Or, where do `Json` values come from?)
@@ -97,7 +78,7 @@ follows:
97
78
98
79
```purescript
99
80
import Data.Tuple (Tuple(..))
100
-
import Data.StrMap as StrMap
81
+
import Foreign.Object as StrMap
101
82
import Data.Argonaut.Core as A
102
83
103
84
someNumber = A.fromNumber 23.6
@@ -122,20 +103,29 @@ expression instead.
122
103
The type of `foldJson` is:
123
104
124
105
```purescript
125
-
foldJson :: forall a.
126
-
(JNull -> a) -> (JBoolean -> a) -> (JNumber -> a) ->
127
-
(JString -> a) -> (JArray -> a) -> (JObject -> a) ->
128
-
Json -> a
106
+
foldJson
107
+
:: forall a
108
+
. (Unit -> a)
109
+
-> (Boolean -> a)
110
+
-> (Number -> a)
111
+
-> (String -> a)
112
+
-> (Array Json -> a)
113
+
-> (Object Json -> a)
114
+
-> Json
115
+
-> a
129
116
```
130
117
131
118
That is, `foldJson` takes six functions, which all must return values of some
132
119
particular type `a`, together with one `Json` value. `foldJson` itself also
133
120
returns a value of the same type `a`.
134
121
135
122
A use of `foldJson` is very similar to a `case ... of` expression, as it allows
136
-
you to handle each of the six possibilities for the `Json` value you passed in.
137
-
Thinking of it this way, each of the six function arguments is like one of the
138
-
case alternatives. Just like in a `case ... of` expression, the final value
123
+
you to handle each of the six possibilities for the `Json` value you passed in. Thinking of it this way, each of the six function arguments is like one of the
124
+
case alternatives.
125
+
126
+
The function that takes `Unit` as an argument is for matching `null` values. As there is only one possible `null` value, we use the PureScript `Unit` type, as correspondingly there is only one possible `Unit` value.
127
+
128
+
Just like in a `case ... of` expression, the final value
139
129
that the whole expression evaluates to comes from evaluating exactly one of the
140
130
'alternatives' (functions) that you pass in. In fact, you can tell that this
141
131
is the case just by looking at the type signature of `foldJson`, because of a
@@ -168,7 +158,7 @@ basicInfo = foldJson
168
158
" characters long.")
169
159
(\xs -> "Got an array, which had " <> Data.Array.length xs <>
170
160
" items.")
171
-
(\obj -> "Got an object, which had " <> Data.StrMap.size obj <>
161
+
(\obj -> "Got an object, which had " <> Foreign.Object.size obj <>
172
162
" items.")
173
163
```
174
164
@@ -189,7 +179,7 @@ For example, we can write a function which tests whether a JSON value is the
189
179
string "lol" like this:
190
180
191
181
```purescript
192
-
foldJsonString :: forall a. a -> (JString -> a) -> Json -> a
182
+
foldJsonString :: forall a. a -> (String -> a) -> Json -> a
193
183
194
184
isJsonLol = foldJsonString false (_ == "lol")
195
185
```
@@ -203,7 +193,7 @@ type, you'll get a `Just` value. Otherwise, you'll get `Nothing`. For example,
0 commit comments