Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specification of media type #2398

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions bench/src/main/scala/org/http4s/bench/CirceJsonBench.scala
@@ -1,10 +1,11 @@
package org.http4s
package bench

import java.util.concurrent.TimeUnit

import cats.effect.IO
import io.circe._
import io.circe.parser._
import java.util.concurrent.TimeUnit
import org.http4s.circe._
import org.openjdk.jmh.annotations._

Expand All @@ -24,7 +25,10 @@ class CirceJsonBench {

@Benchmark
def decode_adaptive(in: BenchState): Either[DecodeFailure, Json] =
jsonDecoderAdaptive[IO](in.cutoff).decode(in.req, strict = true).value.unsafeRunSync
jsonDecoderAdaptive[IO](in.cutoff, MediaType.application.json)
.decode(in.req, strict = true)
.value
.unsafeRunSync
}

object CirceJsonBench {
Expand Down
23 changes: 15 additions & 8 deletions circe/src/main/scala/org/http4s/circe/CirceInstances.scala
@@ -1,16 +1,16 @@
package org.http4s
package circe

import java.nio.ByteBuffer

import cats._
import cats.data.NonEmptyList
import cats.effect._
import cats.implicits._
import fs2.Chunk
import io.circe._
import io.circe.jawn._
import io.circe.jawn.CirceSupportParser.facade
import java.nio.ByteBuffer

import cats.data.NonEmptyList
import io.circe.jawn._
import org.http4s.headers.`Content-Type`
import org.http4s.jawn.JawnInstances
import org.typelevel.jawn.ParseException
Expand Down Expand Up @@ -47,10 +47,13 @@ trait CirceInstances extends JawnInstances {

// default cutoff value is based on benchmarks results
implicit def jsonDecoder[F[_]: Sync]: EntityDecoder[F, Json] =
jsonDecoderAdaptive(cutoff = 100000)
jsonDecoderAdaptive(cutoff = 100000, MediaType.application.json)

def jsonDecoderAdaptive[F[_]: Sync](cutoff: Long): EntityDecoder[F, Json] =
EntityDecoder.decodeBy(MediaType.application.json) { msg =>
def jsonDecoderAdaptive[F[_]: Sync](
cutoff: Long,
r1: MediaRange,
rs: MediaRange*): EntityDecoder[F, Json] =
EntityDecoder.decodeBy(r1, rs: _*) { msg =>
msg.contentLength match {
case Some(contentLength) if contentLength < cutoff =>
jsonDecoderByteBufferImpl[F](msg)
Expand All @@ -59,7 +62,11 @@ trait CirceInstances extends JawnInstances {
}

def jsonOf[F[_]: Sync, A](implicit decoder: Decoder[A]): EntityDecoder[F, A] =
jsonDecoder[F].flatMapR { json =>
jsonOf(MediaType.application.json)

def jsonOf[F[_]: Sync, A](r1: MediaRange, rs: MediaRange*)(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This introduces an overload. Could we get this version renamed?

implicit decoder: Decoder[A]): EntityDecoder[F, A] =
jsonDecoderAdaptive[F](cutoff = 100000, r1, rs: _*).flatMapR { json =>
decoder
.decodeJson(json)
.fold(
Expand Down