Skip to content

Commit

Permalink
Add Codec type class (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierBlanvillain authored and travisbrown committed Jun 10, 2019
1 parent 9f5f7e8 commit 9227b9c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
17 changes: 17 additions & 0 deletions modules/core/shared/src/main/scala/io/circe/Codec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import io.circe._

/**
* A type class that provides back and forth conversion between values of type `A`
* and the [[Json]] format. Must obey the laws defined in [[io.circe.tests.CodecLaws]].
*/
trait Codec[A] extends Encoder[A] with Decoder[A]

object Codec {
def apply[A](implicit instance: Codec[A]): Codec[A] = instance

implicit def fromEncoderDecoder[A](implicit e: Encoder[A], d: Decoder[A]): Codec[A] =
new Codec[A] {
def apply(c: HCursor): Decoder.Result[A] = d(c)
def apply(a: A): Json = e(a)
}
}
3 changes: 3 additions & 0 deletions modules/core/shared/src/main/scala/io/circe/Decoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import scala.collection.immutable.{ Map => ImmutableMap, Set, SortedMap, SortedS
import scala.collection.mutable.Builder
import scala.util.{ Failure, Success, Try }

/**
* A type class that provides a way to produce a value of type `A` from a [[Json]] value.
*/
trait Decoder[A] extends Serializable { self =>

/**
Expand Down

0 comments on commit 9227b9c

Please sign in to comment.