-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace Buf -> String -> Case Class conversions with Buf -> Case Clas…
…s conversions in most JSON Decoders.
- Loading branch information
Showing
5 changed files
with
56 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,25 @@ | ||
package io.finch.circe | ||
|
||
import cats.syntax.show._ | ||
import com.twitter.finagle.netty3.ChannelBufferBuf | ||
import com.twitter.util.{Return, Throw, Try} | ||
import io.circe.Decoder | ||
import io.circe.jawn.decode | ||
import io.circe.jawn._ | ||
import io.finch.{Decode, Error} | ||
import io.finch.internal.BufText | ||
import java.nio.charset.StandardCharsets | ||
|
||
trait Decoders { | ||
|
||
/** | ||
* Maps a Circe's [[Decoder]] to Finch's [[Decode]]. | ||
*/ | ||
implicit def decodeCirce[A: Decoder]: Decode.Json[A] = Decode.json((b, cs) => | ||
|
||
// TODO: Eliminate toString conversion | ||
// See https://github.com/finagle/finch/issues/511 | ||
// Jawn can parse from ByteBuffer's if they represent UTF-8 strings. | ||
// We can check the charset here and do parsing w/o extra to-string conversion. | ||
decode[A](BufText.extract(b, cs)).fold[Try[A]]( | ||
error => Throw[A](Error(error.show)), | ||
value => Return(value) | ||
) | ||
) | ||
implicit def decodeCirce[A: Decoder]: Decode.Json[A] = Decode.json { (b, cs) => | ||
val attemptJson = cs match { | ||
case StandardCharsets.UTF_8 => | ||
parseByteBuffer(ChannelBufferBuf.Owned.extract(b).toByteBuffer()).right.flatMap(_.as[A]) | ||
case _ => decode[A](BufText.extract(b, cs)) | ||
} | ||
attemptJson.fold[Try[A]](error => Throw[A](Error(error.show)), value => Return(value)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters