Skip to content

Commit 83dd71f

Browse files
committed
Updates for 0.12, drop J_ synonyms
1 parent e15eea5 commit 83dd71f

File tree

8 files changed

+103
-170
lines changed

8 files changed

+103
-170
lines changed

README.md

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ data Json
3131
| JNumber Number
3232
| JBoolean Boolean
3333
| JArray (Array Json)
34-
| JObject (StrMap Json)
34+
| JObject (Object Json)
3535
```
3636

3737
And indeed, some might even say this is the obvious approach.
@@ -45,25 +45,6 @@ both in terms of speed and memory churn.
4545

4646
Much of the design of Argonaut follows naturally from this design decision.
4747

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-
6748
### Introducing Json values
6849

6950
(Or, where do `Json` values come from?)
@@ -97,7 +78,7 @@ follows:
9778

9879
```purescript
9980
import Data.Tuple (Tuple(..))
100-
import Data.StrMap as StrMap
81+
import Foreign.Object as StrMap
10182
import Data.Argonaut.Core as A
10283
10384
someNumber = A.fromNumber 23.6
@@ -122,20 +103,29 @@ expression instead.
122103
The type of `foldJson` is:
123104

124105
```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
129116
```
130117

131118
That is, `foldJson` takes six functions, which all must return values of some
132119
particular type `a`, together with one `Json` value. `foldJson` itself also
133120
returns a value of the same type `a`.
134121

135122
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
139129
that the whole expression evaluates to comes from evaluating exactly one of the
140130
'alternatives' (functions) that you pass in. In fact, you can tell that this
141131
is the case just by looking at the type signature of `foldJson`, because of a
@@ -168,7 +158,7 @@ basicInfo = foldJson
168158
" characters long.")
169159
(\xs -> "Got an array, which had " <> Data.Array.length xs <>
170160
" items.")
171-
(\obj -> "Got an object, which had " <> Data.StrMap.size obj <>
161+
(\obj -> "Got an object, which had " <> Foreign.Object.size obj <>
172162
" items.")
173163
```
174164

@@ -189,7 +179,7 @@ For example, we can write a function which tests whether a JSON value is the
189179
string "lol" like this:
190180

191181
```purescript
192-
foldJsonString :: forall a. a -> (JString -> a) -> Json -> a
182+
foldJsonString :: forall a. a -> (String -> a) -> Json -> a
193183
194184
isJsonLol = foldJsonString false (_ == "lol")
195185
```
@@ -203,7 +193,7 @@ type, you'll get a `Just` value. Otherwise, you'll get `Nothing`. For example,
203193
we could have written `isJsonLol` like this, too:
204194

205195
```purescript
206-
toString :: Json -> Maybe JString
196+
toString :: Json -> Maybe String
207197
208198
isJsonLol json =
209199
case toString json of

bower.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
"package.json"
2828
],
2929
"dependencies": {
30-
"purescript-enums": "^3.0.0",
31-
"purescript-functions": "^3.0.0",
32-
"purescript-gen": "^1.0.0",
33-
"purescript-maps": "^3.0.0"
30+
"purescript-enums": "#compiler/0.12",
31+
"purescript-functions": "#compiler/0.12",
32+
"purescript-gen": "#compiler/0.12",
33+
"purescript-foreign-object": "#compiler/0.12"
3434
},
3535
"devDependencies": {
36-
"purescript-quickcheck": "^4.6.1"
36+
"purescript-quickcheck": "#compiler/0.12"
3737
}
3838
}

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
"test": "pulp test"
77
},
88
"devDependencies": {
9-
"eslint": "^3.19.0",
10-
"pulp": "^11.0.0",
11-
"purescript-psa": "^0.5.0",
12-
"purescript": "^0.11.1",
13-
"rimraf": "^2.6.1"
9+
"eslint": "^4.19.1",
10+
"pulp": "^12.2.0",
11+
"purescript-psa": "^0.6.0",
12+
"purescript": "^0.11.7",
13+
"rimraf": "^2.6.2"
1414
}
1515
}

src/Data/Argonaut/Core.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ function id(x) {
44
return x;
55
}
66

7-
exports.fromNull = function () {
8-
return null;
9-
};
10-
117
exports.fromBoolean = id;
128
exports.fromNumber = id;
139
exports.fromString = id;
@@ -28,7 +24,7 @@ function isArray(a) {
2824
}
2925

3026
exports._foldJson = function (isNull, isBool, isNum, isStr, isArr, isObj, j) {
31-
if (j == null) return isNull(null);
27+
if (j == null) return isNull();
3228
else if (typeof j === "boolean") return isBool(j);
3329
else if (typeof j === "number") return isNum(j);
3430
else if (typeof j === "string") return isStr(j);

0 commit comments

Comments
 (0)