trait JsonCodec[A] extends JsonDecoder[A] with JsonEncoder[A]
A JsonCodec[A]
instance has the ability to encode values of type A
into JSON, together with
the ability to decode such JSON into values of type A
.
The trait is the intersection composition of JsonDecoder[A]
and JsonEncoder[A]
, and
instances should satisfy round-tripping laws: that is, for every value, instances must be able
to successfully encode the value into JSON, and then successfully decode the same value from
such JSON.
For more information, see JsonDecoder and JsonEncoder.
{{ val intCodec: JsonCodec[Int] = JsonCodec[Int]
intCodec.encodeJson(intCodec.encodeJson(42)) == Right(42) }}
- Alphabetic
- By Inheritance
- JsonCodec
- JsonEncoder
- JsonEncoderPlatformSpecific
- JsonDecoder
- JsonDecoderPlatformSpecific
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Abstract Value Members
- abstract def decoder: JsonDecoder[A]
- abstract def encoder: JsonEncoder[A]
- abstract def unsafeDecode(trace: List[JsonError], in: RetractReader): A
Low-level, unsafe method to decode a value or throw an exception.
Low-level, unsafe method to decode a value or throw an exception. This method should not be called in application code, although it can be implemented for user-defined data structures.
- Definition Classes
- JsonDecoder
- abstract def unsafeEncode(a: A, indent: Option[Int], out: Write): Unit
- Definition Classes
- JsonEncoder
Concrete Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def *>[B](that: => JsonDecoder[B]): JsonDecoder[B]
An alias for JsonDecoder#zipRight.
An alias for JsonDecoder#zipRight.
- Definition Classes
- JsonDecoder
- final def <*[B](that: => JsonDecoder[B]): JsonDecoder[A]
An alias for JsonDecoder#zipLeft.
An alias for JsonDecoder#zipLeft.
- Definition Classes
- JsonDecoder
- final def <*>[B](that: => JsonDecoder[B]): JsonDecoder[(A, B)]
An alias for JsonDecoder#zip.
An alias for JsonDecoder#zip.
- Definition Classes
- JsonDecoder
- final def <+>[B](that: => JsonDecoder[B]): JsonDecoder[Either[A, B]]
An alias for JsonDecoder#orElseEither.
An alias for JsonDecoder#orElseEither.
- Definition Classes
- JsonDecoder
- final def <>[A1 >: A](that: => JsonDecoder[A1]): JsonDecoder[A1]
An alias for JsonDecoder#orElse.
An alias for JsonDecoder#orElse.
- Definition Classes
- JsonDecoder
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- final def both[B](that: => JsonEncoder[B]): JsonEncoder[(A, B)]
Returns a new encoder that is capable of encoding a tuple containing the values of this encoder and the specified encoder.
Returns a new encoder that is capable of encoding a tuple containing the values of this encoder and the specified encoder.
- Definition Classes
- JsonEncoder
- final def bothWith[B, C](that: => JsonEncoder[B])(f: (C) => (A, B)): JsonEncoder[C]
Returns a new encoder that is capable of encoding a user-defined value, which is create from a tuple of the values of this encoder and the specified encoder, from the specified user- defined function.
Returns a new encoder that is capable of encoding a user-defined value, which is create from a tuple of the values of this encoder and the specified encoder, from the specified user- defined function.
- Definition Classes
- JsonEncoder
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- final def contramap[B](f: (B) => A): JsonEncoder[B]
Returns a new encoder, with a new input type, which can be transformed to the old input type by the specified user-defined function.
Returns a new encoder, with a new input type, which can be transformed to the old input type by the specified user-defined function.
- Definition Classes
- JsonEncoder
- final def decodeJson(str: CharSequence): Either[String, A]
Attempts to decode a value of type
A
from the specifiedCharSequence
, but may fail with a human-readable error message if the provided text does not encode a value of this type.Attempts to decode a value of type
A
from the specifiedCharSequence
, but may fail with a human-readable error message if the provided text does not encode a value of this type.Note: This method may not entirely consume the specified character sequence.
- Definition Classes
- JsonDecoder
- final def either[B](that: => JsonEncoder[B]): JsonEncoder[Either[A, B]]
Returns a new encoder that can accepts an
Either[A, B]
to either, and uses either this encoder or the specified encoder to encode the two different types of values.Returns a new encoder that can accepts an
Either[A, B]
to either, and uses either this encoder or the specified encoder to encode the two different types of values.- Definition Classes
- JsonEncoder
- final def eitherWith[B, C](that: => JsonEncoder[B])(f: (C) => Either[A, B]): JsonEncoder[C]
Returns a new encoder with a new input type, which can be transformed to either the input type of this encoder, or the input type of the specified encoder, using the user-defined transformation function.
Returns a new encoder with a new input type, which can be transformed to either the input type of this encoder, or the input type of the specified encoder, using the user-defined transformation function.
- Definition Classes
- JsonEncoder
- final def encodeJson(a: A, indent: Option[Int]): CharSequence
Encodes the specified value into a JSON string, with the specified indentation level.
Encodes the specified value into a JSON string, with the specified indentation level.
- Definition Classes
- JsonEncoder
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- def fromJsonAST(json: Json): Either[String, A]
Decode a value from an already parsed Json AST.
Decode a value from an already parsed Json AST.
The default implementation encodes the Json to a byte stream and uses decode to parse that. Override to provide a more performant implementation.
- Definition Classes
- JsonDecoder
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def isNothing(a: A): Boolean
This default may be overriden when this value may be missing within a JSON object and still be encoded.
This default may be overriden when this value may be missing within a JSON object and still be encoded.
- Definition Classes
- JsonEncoder
- Annotations
- @nowarn()
- def map[B](f: (A) => B): JsonDecoder[B]
Returns a new codec whose decoded values will be mapped by the specified function.
Returns a new codec whose decoded values will be mapped by the specified function.
- Definition Classes
- JsonDecoder
- final def mapOrFail[B](f: (A) => Either[String, B]): JsonDecoder[B]
Returns a new codec whose decoded values will be mapped by the specified function, which may itself decide to fail with some type of error.
Returns a new codec whose decoded values will be mapped by the specified function, which may itself decide to fail with some type of error.
- Definition Classes
- JsonDecoder
- final def narrow[B <: A]: JsonEncoder[B]
Returns this encoder but narrowed to the its given sub-type
Returns this encoder but narrowed to the its given sub-type
- Definition Classes
- JsonEncoder
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def orElse[A1 >: A](that: => JsonDecoder[A1]): JsonDecoder[A1]
Returns a new codec that combines this codec and the specified codec using fallback semantics: such that if this codec fails, the specified codec will be tried instead.
Returns a new codec that combines this codec and the specified codec using fallback semantics: such that if this codec fails, the specified codec will be tried instead. This method may be unsafe from a security perspective: it can use more memory than hand coded alternative and so lead to DOS.
For example, in the case of an alternative between
Int
andBoolean
, a hand coded alternative would look like:val decoder: JsonDecoder[AnyVal] = JsonDecoder.peekChar[AnyVal] { case 't' | 'f' => JsonDecoder[Boolean].widen case c => JsonDecoder[Int].widen }
- Definition Classes
- JsonDecoder
- final def orElseEither[B](that: => JsonDecoder[B]): JsonDecoder[Either[A, B]]
Returns a new codec that combines this codec and the specified codec using fallback semantics: such that if this codec fails, the specified codec will be tried instead.
Returns a new codec that combines this codec and the specified codec using fallback semantics: such that if this codec fails, the specified codec will be tried instead.
- Definition Classes
- JsonDecoder
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toJsonAST(a: A): Either[String, Json]
Converts a value to a Json AST
Converts a value to a Json AST
The default implementation encodes the value to a Json byte stream and uses decode to parse that back to an AST. Override to provide a more performant implementation.
- Definition Classes
- JsonEncoder
- def toString(): String
- Definition Classes
- AnyRef → Any
- def unsafeDecodeMissing(trace: List[JsonError]): A
- Definition Classes
- JsonDecoder
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- final def widen[B >: A]: JsonDecoder[B]
Returns this decoder but widened to the its given super-type
Returns this decoder but widened to the its given super-type
- Definition Classes
- JsonDecoder
- def xmap[B](f: (A) => B, g: (B) => A): JsonCodec[B]
- Definition Classes
- JsonCodec → JsonEncoder → JsonDecoder
- final def zip[B](that: => JsonDecoder[B]): JsonDecoder[(A, B)]
Returns a new codec that combines this codec and the specified codec into a single codec that decodes a tuple of the values decoded by the respective codecs.
Returns a new codec that combines this codec and the specified codec into a single codec that decodes a tuple of the values decoded by the respective codecs.
- Definition Classes
- JsonDecoder
- final def zipLeft[B](that: => JsonDecoder[B]): JsonDecoder[A]
Zips two codecs, but discards the output on the right hand side.
Zips two codecs, but discards the output on the right hand side.
- Definition Classes
- JsonDecoder
- final def zipRight[B](that: => JsonDecoder[B]): JsonDecoder[B]
Zips two codecs, but discards the output on the left hand side.
Zips two codecs, but discards the output on the left hand side.
- Definition Classes
- JsonDecoder
- final def zipWith[B, C](that: => JsonDecoder[B])(f: (A, B) => C): JsonDecoder[C]
Zips two codecs into one, transforming the outputs of both codecs by the specified function.
Zips two codecs into one, transforming the outputs of both codecs by the specified function.
- Definition Classes
- JsonDecoder