From 2733cb4fd14baf7dcac3744f942e819a6e9f41a7 Mon Sep 17 00:00:00 2001 From: NAJ10 Date: Sat, 9 Feb 2019 11:50:38 +0000 Subject: [PATCH 1/2] Allow specification of media type --- .../org/http4s/bench/CirceJsonBench.scala | 8 +++++-- .../org/http4s/circe/CirceInstances.scala | 23 ++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/bench/src/main/scala/org/http4s/bench/CirceJsonBench.scala b/bench/src/main/scala/org/http4s/bench/CirceJsonBench.scala index 20076c968ff..cecf243bb7a 100644 --- a/bench/src/main/scala/org/http4s/bench/CirceJsonBench.scala +++ b/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._ @@ -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 { diff --git a/circe/src/main/scala/org/http4s/circe/CirceInstances.scala b/circe/src/main/scala/org/http4s/circe/CirceInstances.scala index d041b2c221a..ff8bd73a2a8 100644 --- a/circe/src/main/scala/org/http4s/circe/CirceInstances.scala +++ b/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 @@ -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) @@ -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*)( + implicit decoder: Decoder[A]): EntityDecoder[F, A] = + jsonDecoderAdaptive[F](cutoff = 100000, r1, rs: _*).flatMapR { json => decoder .decodeJson(json) .fold( From e99b63634634d12214a44a841a6367467a6b479c Mon Sep 17 00:00:00 2001 From: NAJ10 Date: Tue, 12 Feb 2019 08:29:40 +0000 Subject: [PATCH 2/2] Rename method following code review --- circe/src/main/scala/org/http4s/circe/CirceInstances.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/circe/src/main/scala/org/http4s/circe/CirceInstances.scala b/circe/src/main/scala/org/http4s/circe/CirceInstances.scala index ff8bd73a2a8..a2080b40801 100644 --- a/circe/src/main/scala/org/http4s/circe/CirceInstances.scala +++ b/circe/src/main/scala/org/http4s/circe/CirceInstances.scala @@ -62,9 +62,9 @@ trait CirceInstances extends JawnInstances { } def jsonOf[F[_]: Sync, A](implicit decoder: Decoder[A]): EntityDecoder[F, A] = - jsonOf(MediaType.application.json) + jsonOfWithMedia(MediaType.application.json) - def jsonOf[F[_]: Sync, A](r1: MediaRange, rs: MediaRange*)( + def jsonOfWithMedia[F[_]: Sync, A](r1: MediaRange, rs: MediaRange*)( implicit decoder: Decoder[A]): EntityDecoder[F, A] = jsonDecoderAdaptive[F](cutoff = 100000, r1, rs: _*).flatMapR { json => decoder