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/json.md
+12-10Lines changed: 12 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,19 +2,21 @@
2
2
3
3
Adding support for JSON (or other format) bodies in requests/responses is a matter of providing a [body serializer](requests/body.md) and/or a [response body specification](responses/body.md). Both are quite straightforward to implement, so integrating with your favorite JSON library shouldn't be a problem. However, there are some integrations available out-of-the-box.
4
4
5
-
Each integration is available as an import, which brings the implicit `BodySerializer`s and `asJson` methods into scope. Alternatively, these values are grouped intro traits (e.g. `sttp.client4.circe.SttpCirceApi`), which can be extended to group multiple integrations in one object, and thus reduce the number of necessary imports.
5
+
Each integration is available as an import, which brings `asJson` methods into scope. Alternatively, these values are grouped intro traits (e.g. `sttp.client4.circe.SttpCirceApi`), which can be extended to group multiple integrations in one object, and thus reduce the number of necessary imports.
6
6
7
7
The following variants of `asJson` methods are available:
8
8
9
-
* regular - deserializes the body to json, only if the response is successful (2xx)
10
-
*`always` - deserializes the body to json regardless of the status code
11
-
*`either` - uses different deserializers for error and successful (2xx) responses
9
+
*`asJson(b: B)` - serializes the body so that it can be used as a request's body, e.g. using `basicRequest.body(asJson(myValue))`
10
+
*`asJson[B]` - specifies that the body should be deserialized to json, but only if the response is successful (2xx); shoud be used to specify how a response should be handled, e.g. `basicRequest.response(asJson[T])`
11
+
*`asJsonAlways[B]` - specifies that the body should be deserialized to json, regardless of the status code
12
+
*`asJsonEither[E, B]` - specifies that the body should be deserialized to json, using different deserializers for error and successful (2xx) responses
12
13
13
14
The type signatures vary depending on the underlying library (required implicits and error representation differs), but they obey the following pattern:
It is also possible to set custom types as request bodies, as long as there's an implicit `BodySerializer[B]` value in scope, which is simply an alias for a function:
89
-
90
-
```scala
91
-
typeBodySerializer[B] =B=>BasicRequestBody
92
-
```
93
-
94
-
A `BasicRequestBody` is a wrapper for one of the supported request body types: a `String`/byte array or an input stream.
88
+
It is also possible to write custom serializers, which return arbitrary body representations. These should be
89
+
methods/functions which return instances of `BasicBody`, which is a wrapper for one of the supported request body
90
+
types: a `String`, byte array, an input stream, etc.
95
91
96
92
For example, here's how to write a custom serializer for a case class, with serializer-specific default content type:
This would add `BodySerializer` needed for serialization and `asXml` method needed for deserialization. Please notice, that `fromXML`, `toXML`, `CanWriteXML`, `XMLFormat` and `defaultScope` are members of code generated by scalaxb.
42
41
42
+
This would add `asXml` methods needed for serialization and deserialization. Please notice, that `fromXML`, `toXML`, `CanWriteXML`, `XMLFormat` and `defaultScope` are members of code generated by scalaxb.
43
+
44
+
Next to this trait, you might want to introduce `sttpScalaxb` package object to simplify imports.
43
45
44
-
Next to this trait, you might want to introduce `sttpScalaxb`
45
-
package object to simplify imports.
46
46
```scala
47
47
packageobjectsttpScalaxbextendsSttpScalaxbApi
48
48
```
49
49
50
50
From now on, XML serialization/deserialization would work for all classes generated from `.xsd` file as long as `XMLFormat` for the type in the question and `XmlElementLabel` for the top XML node would be implicitly provided in the scope.
51
51
52
52
Usage example:
53
+
53
54
```scala
54
55
valbackend:SyncBackend=DefaultSyncBackend()
55
56
valrequestPayload=Outer(Inner(42, b =true, "horses"), "cats") // `Outer` and `Inner` classes are generated by scalaxb from xsd file
@@ -61,7 +62,7 @@ import generated.Generated_OuterFormat // imports member of code generated by sc
0 commit comments