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
Next, import the modules used in this tutorial -- you can also install `argonaut` and only import `Data.Argonaut`if you'd like to cut down on imports:
86
86
87
-
```
87
+
```purs
88
88
import Prelude
89
89
90
90
import Control.Alternative
@@ -125,7 +125,7 @@ encodeJson :: EncodeJson a => a -> Json
125
125
126
126
> Tip: There is no `Show` instance for `Json`. To print a `Json` value as a valid JSON string, use `stringify` -- it's the same as the [JavaScript `stringify` method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify).
127
127
128
-
```purs
128
+
```sh
129
129
> user = { name: "Tom", age: Just 25, team: Just "Red Team" } :: User
@@ -143,7 +143,7 @@ decodeJson :: DecodeJson a => Json -> Either String a
143
143
144
144
> Tip: To parse a JSON string as a `Json` value, you can use the `jsonParser`function(which can fail). If you are sure you have valid JSON, then consider writing it in an FFI file and foreign importing it as `Json` as described in the [`argonaut-core` documentation](https://github.com/purescript-contrib/purescript-argonaut-core#introducing-json-values).
@@ -155,7 +155,7 @@ Right "{\"name\":\"Tom\",\"age\":25,\"team\":null}"
155
155
156
156
Decoding can fail if the `Json` doesn't match the shape expected by a `DecodeJson` instance; in that case, an error is returned instead of the decoded value.
> (decodeJson =<< jsonParser badUserJsonString) :: Either String User
161
161
Left "JSON was missing expected field: team"
@@ -275,7 +275,7 @@ If your type can be represented easily with a `String`, `Number`, `Boolean`, or
275
275
However, quite often your data type will require representation as an object. This library provides combinators in`Data.Argonaut.Decode.Combinators` which are useful fordecoding objects into PureScript types by looking up keysin the object and decoding them according to their `DecodeJson` instances.
276
276
277
277
- Use `.:` (`getField`) to decode a field;if the field is missing, this will decode to a `Maybe`
278
-
- Use `.=` (`defaultField`) to provide a default value for a field which may not exist. If decoding fails, you'll still get an error; if decoding succeeds with a value of type `Maybe a`, then this default value will handle the `Nothing` case.
278
+
- Use `.!=` (`defaultField`) to provide a default value for a field which may not exist. If decoding fails, you'll still get an error; if decoding succeeds with a value of type `Maybe a`, then this default value will handle the `Nothing` case.
279
279
280
280
Let's use these combinators to decode a `Json` object into our `AppUser` record.
281
281
@@ -475,7 +475,7 @@ newtype User = User
475
475
derive instance newtypeUser :: Newtype User _
476
476
derive newtype instance showUser :: Show User
477
477
478
-
decodeUser :: Json -> Either (Array String) User
478
+
decodeUser :: Json -> Either String User
479
479
decodeUser json = do
480
480
obj <- decodeJson json
481
481
name <- obj .: "name"
@@ -486,7 +486,7 @@ decodeUser json = do
486
486
487
487
Running this in the REPL with bad input, we only see the first error:
0 commit comments