Skip to main content

Codec

There are extension methods for Encoder, Decoder and Codec.

Encoder[A].renameFields

With renameFields, you can make the existing JSON Encoder to rename the existing fields.

import extras.circe.codecs.encoder._

Encoder[A].renameFields (
"name1" -> "newName1",
"name2" -> "newName2"
) // Encoder[A]

Example

import io.circe._
import io.circe.syntax._
import io.circe.generic.semiauto._
import extras.circe.codecs.encoder._

final case class Something(n: Int, s: String, price: BigDecimal)
object Something {
implicit val somethingEncoder: Encoder[Something] =
deriveEncoder[Something]
.renameFields(
"n" -> "id",
"s" -> "name"
)
}

val something = Something(1, "Vibranium Shield", BigDecimal(999999999))
// something: Something = Something(
// n = 1,
// s = "Vibranium Shield",
// price = 999999999
// )
something.asJson
// res1: Json = JObject(
// value = object[price -> 999999999,id -> 1,name -> "Vibranium Shield"]
// )
something.asJson.spaces2
// res2: String = """{
// "price" : 999999999,
// "id" : 1,
// "name" : "Vibranium Shield"
// }"""

Comparison

Let's compare it with the Encoder without renameFields syntax.

import io.circe._
import io.circe.syntax._
import io.circe.generic.semiauto._
import extras.circe.codecs.encoder._

final case class Something(n: Int, s: String, price: BigDecimal)
object Something {
val originalSomethingEncoder: Encoder[Something] = deriveEncoder
implicit val somethingEncoder: Encoder[Something] = originalSomethingEncoder
.renameFields(
"n" -> "id",
"s" -> "name"
)
}

val something = Something(1, "Vibranium Shield", BigDecimal(999999999))
// something: Something = Something(
// n = 1,
// s = "Vibranium Shield",
// price = 999999999
// )

Something.originalSomethingEncoder(something)
// res4: Json = JObject(
// value = object[n -> 1,s -> "Vibranium Shield",price -> 999999999]
// )
Something.originalSomethingEncoder(something).spaces2
// res5: String = """{
// "n" : 1,
// "s" : "Vibranium Shield",
// "price" : 999999999
// }"""

something.asJson
// res6: Json = JObject(
// value = object[price -> 999999999,id -> 1,name -> "Vibranium Shield"]
// )
something.asJson.spaces2
// res7: String = """{
// "price" : 999999999,
// "id" : 1,
// "name" : "Vibranium Shield"
// }"""

Decoder[A].renameFields

With renameFields, you can make the existing JSON Decoder to rename the existing fields.

import extras.circe.codecs.encoder._

Decoder[A].renameFields (
"name1" -> "newName1",
"name2" -> "newName2"
) // Decoder[A]

Example

import io.circe._
import io.circe.syntax._
import io.circe.generic.semiauto._
import extras.circe.codecs.decoder._

final case class Something(n: Int, s: String, price: BigDecimal)
object Something {
implicit val somethingDecoder: Decoder[Something] =
deriveDecoder[Something]
.renameFields(
"n" -> "id",
"s" -> "name"
)
}

val jsonString =
"""
{
"id": 1,
"name": "Vibranium Shield",
"price": 999999999
}
"""
// jsonString: String = """
// {
// "id": 1,
// "name": "Vibranium Shield",
// "price": 999999999
// }
// """

import io.circe.parser._
decode[Something](jsonString)
// res9: Either[Error, Something] = Right(
// value = Something(n = 1, s = "Vibranium Shield", price = 999999999)
// )

Comparison

Let's compare it with the Encoder without renameFields syntax.

import io.circe._
import io.circe.syntax._
import io.circe.generic.semiauto._
import extras.circe.codecs.decoder._

final case class Something(n: Int, s: String, price: BigDecimal)
object Something {
val originalSomethingDecoder: Decoder[Something] = deriveDecoder
implicit val somethingDecoder: Decoder[Something] =
originalSomethingDecoder
.renameFields(
"n" -> "id",
"s" -> "name"
)
}

val jsonString =
"""
{
"id": 1,
"name": "Vibranium Shield",
"price": 999999999
}
"""
// jsonString: String = """
// {
// "id": 1,
// "name": "Vibranium Shield",
// "price": 999999999
// }
// """

import io.circe.parser._
decode[Something](jsonString)(Something.originalSomethingDecoder)
// res11: Either[Error, Something] = Left(
// value = DecodingFailure(Attempt to decode value on failed cursor, List(DownField(n)))
// )

decode[Something](jsonString)
// res12: Either[Error, Something] = Right(
// value = Something(n = 1, s = "Vibranium Shield", price = 999999999)
// )

Codec[A].renameFields (Encoder + Decoder)

With renameFields, you can make the existing JSON Codec to rename the existing fields.

import extras.circe.codecs.codec._

Codec[A].renameFields (
"name1" -> "newName1",
"name2" -> "newName2"
) // Codec[A]

Example

import io.circe._
import io.circe.syntax._
import io.circe.generic.semiauto._
import extras.circe.codecs.codec._

final case class Something(n: Int, s: String, price: BigDecimal)
object Something {
implicit val somethingCodec: Codec[Something] =
deriveCodec[Something]
.renameFields(
"n" -> "id",
"s" -> "name"
)
}

val something = Something(1, "Vibranium Shield", BigDecimal(999999999))
// something: Something = Something(
// n = 1,
// s = "Vibranium Shield",
// price = 999999999
// )
something.asJson
// res14: Json = JObject(
// value = object[price -> 999999999,id -> 1,name -> "Vibranium Shield"]
// )
val jsonString = something.asJson.spaces2
// jsonString: String = """{
// "price" : 999999999,
// "id" : 1,
// "name" : "Vibranium Shield"
// }"""

import io.circe.parser._
decode[Something](jsonString)
// res15: Either[Error, Something] = Right(
// value = Something(n = 1, s = "Vibranium Shield", price = 999999999)
// )

Comparison

Let's compare it with the Encoder without renameFields syntax.

import io.circe._
import io.circe.syntax._
import io.circe.generic.semiauto._
import extras.circe.codecs.codec._

final case class Something(n: Int, s: String, price: BigDecimal)
object Something {
val originalSomethingCodec: Codec[Something] = deriveCodec
implicit val somethingCodec: Codec[Something] = originalSomethingCodec
.renameFields(
"n" -> "id",
"s" -> "name"
)
}

val something = Something(1, "Vibranium Shield", BigDecimal(999999999))
// something: Something = Something(
// n = 1,
// s = "Vibranium Shield",
// price = 999999999
// )

Something.originalSomethingCodec(something)
// res17: Json = JObject(
// value = object[n -> 1,s -> "Vibranium Shield",price -> 999999999]
// )
Something.originalSomethingCodec(something).spaces2
// res18: String = """{
// "n" : 1,
// "s" : "Vibranium Shield",
// "price" : 999999999
// }"""

something.asJson
// res19: Json = JObject(
// value = object[price -> 999999999,id -> 1,name -> "Vibranium Shield"]
// )
val jsonString = something.asJson.spaces2
// jsonString: String = """{
// "price" : 999999999,
// "id" : 1,
// "name" : "Vibranium Shield"
// }"""

import io.circe.parser._
decode[Something](jsonString)(Something.originalSomethingCodec)
// res20: Either[Error, Something] = Left(
// value = DecodingFailure(Attempt to decode value on failed cursor, List(DownField(n)))
// )

decode[Something](jsonString)
// res21: Either[Error, Something] = Right(
// value = Something(n = 1, s = "Vibranium Shield", price = 999999999)
// )