Codec

trait Codec[A] extends Decoder[A] with Encoder[A]

A type class that provides back and forth conversion between values of type A and the Json format.

Note that this type class is only intended to make instance definition more convenient; it generally should not be used as a constraint.

Instances should obey the laws defined in io.circe.testing.CodecLaws.

Companion:
object
Source:
Codec.scala
trait Encoder[A]
trait Decoder[A]
trait Serializable
class Object
trait Matchable
class Any
trait AsRoot[A]
trait AsArray[A]
trait AsObject[A]

Value members

Concrete methods

def iemap[B](f: A => Either[String, B])(g: B => A): Codec[B]

Variant of imap which allows the Decoder to fail with message string.

Variant of imap which allows the Decoder to fail with message string.

Type parameters:
B

the type of the new Codec

Value parameters:
f

decode value or fail

g

encode value

Returns:

a codec for B

Source:
Codec.scala
def iemapTry[B](f: A => Try[B])(g: B => A): Codec[B]

Variant of imap which allows the Decoder to fail with Throwable.

Variant of imap which allows the Decoder to fail with Throwable.

Type parameters:
B

the type of the new Codec

Value parameters:
f

decode value or fail

g

encode value

Returns:

a codec for B

Source:
Codec.scala

Inherited methods

def apply(a: A): Json

Convert a value to JSON.

Convert a value to JSON.

Inherited from:
Encoder
Source:
Encoder.scala
def apply(c: HCursor): Result[A]

Decode the given HCursor.

Decode the given HCursor.

Inherited from:
Decoder
Source:
Decoder.scala
final def at(field: String): Decoder[A]

Create a new decoder that attempts to navigate to the specified field before decoding.

Create a new decoder that attempts to navigate to the specified field before decoding.

Inherited from:
Decoder
Source:
Decoder.scala
final def contramap[B](f: B => A): Encoder[B]

Create a new Encoder by applying a function to a value of type B before encoding as an A.

Create a new Encoder by applying a function to a value of type B before encoding as an A.

Inherited from:
Encoder
Source:
Encoder.scala
final def decodeJson(j: Json): Result[A]

Decode the given Json value.

Decode the given Json value.

Inherited from:
Decoder
Source:
Decoder.scala
final def either[B](decodeB: Decoder[B]): Decoder[Either[A, B]]

Choose the first succeeding decoder, wrapping the result in a disjunction.

Choose the first succeeding decoder, wrapping the result in a disjunction.

Inherited from:
Decoder
Source:
Decoder.scala
final def emap[B](f: A => Either[String, B]): Decoder[B]

Create a new decoder that performs some operation on the result if this one succeeds.

Create a new decoder that performs some operation on the result if this one succeeds.

Value parameters:
f

a function returning either a value or an error message

Inherited from:
Decoder
Source:
Decoder.scala
final def emapTry[B](f: A => Try[B]): Decoder[B]

Create a new decoder that performs some operation on the result if this one succeeds.

Create a new decoder that performs some operation on the result if this one succeeds.

Value parameters:
f

a function returning either a value or an error message

Inherited from:
Decoder
Source:
Decoder.scala
final def ensure(errors: A => List[String]): Decoder[A]

Build a new instance that fails with one or more errors if the condition does not hold for the result.

Build a new instance that fails with one or more errors if the condition does not hold for the result.

If the result of the function applied to the decoded value is the empty list, the new decoder will succeed with that value.

Inherited from:
Decoder
Source:
Decoder.scala
final def ensure(pred: A => Boolean, message: => String): Decoder[A]

Build a new instance that fails if the condition does not hold for the result.

Build a new instance that fails if the condition does not hold for the result.

Note that in the case of chained calls to this method, only the first failure will be returned.

Inherited from:
Decoder
Source:
Decoder.scala
final def flatMap[B](f: A => Decoder[B]): Decoder[B]

Monadically bind a function over this Decoder.

Monadically bind a function over this Decoder.

Inherited from:
Decoder
Source:
Decoder.scala

Create a new instance that handles any of this instance's errors with the given function.

Create a new instance that handles any of this instance's errors with the given function.

Note that in the case of accumulating decoding, only the first error will be used in recovery.

Inherited from:
Decoder
Source:
Decoder.scala
final def kleisli: Kleisli[Result, HCursor, A]

Convert to a Kleisli arrow.

Convert to a Kleisli arrow.

Inherited from:
Decoder
Source:
Decoder.scala
final def map[B](f: A => B): Decoder[B]

Map a function over this Decoder.

Map a function over this Decoder.

Inherited from:
Decoder
Source:
Decoder.scala
final def mapJson(f: Json => Json): Encoder[A]

Create a new Encoder by applying a function to the output of this one.

Create a new Encoder by applying a function to the output of this one.

Inherited from:
Encoder
Source:
Encoder.scala
final def or[AA >: A](d: => Decoder[AA]): Decoder[AA]

Choose the first succeeding decoder.

Choose the first succeeding decoder.

Inherited from:
Decoder
Source:
Decoder.scala
final def prepare(f: ACursor => ACursor): Decoder[A]

Create a new decoder that performs some operation on the incoming JSON before decoding.

Create a new decoder that performs some operation on the incoming JSON before decoding.

Inherited from:
Decoder
Source:
Decoder.scala
final def product[B](fb: Decoder[B]): Decoder[(A, B)]

Run two decoders and return their results as a pair.

Run two decoders and return their results as a pair.

Inherited from:
Decoder
Source:
Decoder.scala

Decode the given ACursor.

Decode the given ACursor.

Note that if you override the default implementation, you should also be sure to override tryDecodeAccumulating in order for fail-fast and accumulating decoding to be consistent.

Inherited from:
Decoder
Source:
Decoder.scala
final def validate(pred: HCursor => Boolean, message: => String): Decoder[A]

Build a new instance that fails if the condition does not hold for the input.

Build a new instance that fails if the condition does not hold for the input.

Inherited from:
Decoder
Source:
Decoder.scala
final def validate(errors: HCursor => List[String]): Decoder[A]

Build a new instance that fails if the condition does not hold for the input.

Build a new instance that fails if the condition does not hold for the input.

Inherited from:
Decoder
Source:
Decoder.scala
final def withErrorMessage(message: String): Decoder[A]

Build a new instance with the specified error message.

Build a new instance with the specified error message.

Inherited from:
Decoder
Source:
Decoder.scala