Skip to content

Commit

Permalink
anyJsonCodec
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Apr 6, 2020
1 parent 4cc75db commit da237ed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
10 changes: 9 additions & 1 deletion core/src/main/scala/sttp/tapir/Tapir.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sttp.tapir
import java.nio.charset.{Charset, StandardCharsets}

import sttp.model.{Cookie, CookieValueWithMeta, CookieWithMeta, Header, HeaderNames, MultiQueryParams, StatusCode}
import sttp.tapir.CodecFormat.{OctetStream, TextPlain}
import sttp.tapir.CodecFormat.{Json, OctetStream, TextPlain}
import sttp.tapir.EndpointOutput.StatusMapping
import sttp.tapir.internal.{ModifyMacroSupport, StatusMappingMacro}
import sttp.tapir.model.ServerRequest
Expand Down Expand Up @@ -57,6 +57,14 @@ trait Tapir extends TapirDerivedInputs with ModifyMacroSupport {
def plainBody[T: Codec[String, *, TextPlain]](charset: Charset): EndpointIO.Body[String, T] =
EndpointIO.Body(RawBodyType.StringBody(charset), implicitly, EndpointIO.Info.empty)

/**
* Json codecs are usually derived from json-library-specific implicits. That's why integrations with
* various json libraries define `jsonBody` methods, which directly require the library-specific implicits.
*
* If you have a custom json codec, you should use this method instead.
*/
def anyJsonBody[T: Codec[String, *, Json]]: EndpointIO.Body[String, T] = anyFromUtf8StringBody(implicitly[Codec[String, T, Json]])

def rawBinaryBody[R: RawBodyType.Binary](implicit codec: Codec[R, R, OctetStream]): EndpointIO.Body[R, R] =
EndpointIO.Body(implicitly[RawBodyType.Binary[R]], codec, EndpointIO.Info.empty)
def binaryBody[R: RawBodyType.Binary, T: Codec[R, *, OctetStream]]: EndpointIO.Body[R, T] =
Expand Down
13 changes: 11 additions & 2 deletions doc/endpoint/json.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# Working with JSON

Json values are supported through codecs which encode/decode values to json strings. However, third-party libraries are
needed for actual json parsing/printing. Currently, [Circe](https://github.com/circe/circe),
Json values are supported through codecs, which encode/decode values to json strings. Most often, you'll be using a
third-party library to perform the actual json parsing/printing. Currently, [Circe](https://github.com/circe/circe),
[µPickle](http://www.lihaoyi.com/upickle/), [Spray JSON](https://github.com/spray/spray-json) and
[Play JSON](https://github.com/playframework/play-json) are supported.

All of the integrations, when imported into scope, define a `jsonBody[T]` method. This method depends on
library-specific implicits being in scope, and derives from them a json codec. The derivation also requires implicit
`Schema[T]` and `Validator[T]` instances, which should be automatically derived. For more details see documentation
on supporting [custom types](customtypes.html).

If you have a custom, implicit `Codec[String, T, Json]` instance, you should use the `anyJsonBody[T]` method instead.
This description of endpoint input/output, instead of deriving a codec basing on other library-specific implicits, uses
the json codec that is in scope.

## Circe

To use Circe add the following dependency to your project:
Expand Down

0 comments on commit da237ed

Please sign in to comment.