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: docs/decoding-objects.md
+5-44Lines changed: 5 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,9 +53,9 @@ let make = (name, age, isAdmin, lastLogin) =>
53
53
54
54
...the real goal of `bs-decode` is to give you the tools you need to build up complex record types (like this one) from JSON. You can certainly use `field` and `optionalField` to piece these things together, but the following approaches will make your life easier.
55
55
56
-
### Let-Ops (Coming Soon)
56
+
### Let-Ops
57
57
58
-
In the very near future, `bs-decode`will be switching from BuckleScript to Melange. This will give us access to newer OCaml features, such as [binding operators](https://v2.ocaml.org/manual/bindingops.html). This dramatically simplifies the syntax for decoding and constructing complex objects:
58
+
With the switch to Melange, `bs-decode`now has access to newer OCaml features, such as [binding operators](https://v2.ocaml.org/manual/bindingops.html). This dramatically simplifies the syntax for decoding and constructing complex objects:
Once available, this will replace the "Pipeline" decoding (see below). Unlike the other strategies outlined below, the order of the field decoders doesn't matter. It's much easier to see how each field is used in the constructor, and it works with labeled functions and literal record construction.
71
+
This is the preferred method of decoding JSON into Reason records, as it allows constructing tuples, records, and calling functions with labeled arguments.
72
72
73
73
### Haskell Validation Style
74
74
75
-
It's also possible to use `map` and `apply` functions (often in their infix form `<$>` and `<*>`) to build up a larger decoder from smaller ones. This style my look more familiar if you've used validation libraries in Haskell.
75
+
It's also possible to use `map` and `apply` functions (often in their infix form `<$>` and `<*>`) to build up a larger decoder from smaller ones. This style my look more familiar if you've used validation libraries in Haskell. Importantly, this will not work if `User.make` expects labeled arguments, and it's important to decode the fields in the same order that `User.make` expects to receive its arguments.
76
76
77
77
```reasonml
78
78
let ((<$>), (<*>)) = Decode.(map, apply);
@@ -86,42 +86,3 @@ let decode =
86
86
<*> optionalField("lastLogin", date)
87
87
);
88
88
```
89
-
90
-
### Combining Decoders with `mapN`
91
-
92
-
The provided `map2`...`map5` functions can be used to take the results of up to 5 decoders and combine them using a function that receives the up-to-5 values if all decoders succeed. This is simple to use, but obviously limiting if the record you want to construct has more than 5 fields.
93
-
94
-
```reasonml
95
-
let decode =
96
-
Decode.(
97
-
map4(
98
-
User.make,
99
-
field("name", string),
100
-
field("age", intFromNumber),
101
-
field("isAdmin", boolean),
102
-
optionalField("lastLogin", date),
103
-
)
104
-
);
105
-
```
106
-
107
-
### Pipeline-Style (Deprecated)
108
-
109
-
**Note:** This style of decoding has been deprecated and will be removed in v2.0.
110
-
111
-
Given the `user` type above and its `make` function, you can build up a record by decoding each field in a style inspired by the [Elm Decode Pipeline](https://package.elm-lang.org/packages/NoRedInk/elm-decode-pipeline/3.0.1/) library for Elm.
112
-
113
-
The order of decoded fields is significant, as the pipeline leverages the partial application of the `make` function. Each `field` or `optionalField` line in the example below fills in the next available slot in the `make` function.
114
-
115
-
```reasonml
116
-
let decode = json =>
117
-
Decode.Pipeline.(
118
-
succeed(make)
119
-
|> field("name", string)
120
-
|> field("age", intFromNumber)
121
-
|> field("isAdmin", boolean)
122
-
|> optionalField("lastLogin", date)
123
-
|> run(json)
124
-
);
125
-
```
126
-
127
-
Unlike other decode functions we've looked at, the Pipeline style is not eager. Instead, nothing will be decoded until the whole pipeline is executed by using the `run` function with the appropriate JSON.
0 commit comments