Knows Not Much
Knows Not Much

Reputation: 31586

Parsing custom keys in Json with Circe

I have written this code

import io.circe._, io.circe.generic.auto._, io.circe.parser._, io.circe.syntax._, io.circe.generic.extras._
implicit val config: Configuration = Configuration.default
case class Company(name: String)
case class Quote(average: Double)
case class Stats(price: Double)
@ConfiguredJsonCodec case class Bulk(company: Company, @JsonKey("advanced-stats") stats: Stats, quote: Quote)
val input = """{"AAPL": {"company": {"name": "Apple"},"advanced-stats": {"price":10},"quote": {"average":10}}}"""
val parsed = decode[Map[String, Bulk]](input)

When I try to execute this inside of ammonite I get an error

cmd5.sc:1: macro annotation could not be expanded (you cannot use a macro annotation in the same compilation run that defines it)
@ConfiguredJsonCodec case class Bulk(company: Company, @JsonKey("advanced-stats") stats: Stats, quote: Quote)

When I copy paste this code into a file and try to compile it then it gives compilation error

could not find Lazy implicit value of type io.circe.generic.extras.codec.ConfiguredAsObjectCodec

Edit:: Thanks to the answer below the code started to work on ammonite. it still doesn't compile when I copy paste it into a Scala file. I googled and changed the code to

object DefaultValues {
  implicit val useDefaultValues = Configuration.default.withDefaults
}

import DefaultValues._
@ConfiguredJsonCodec
case class Bulk(
    company: Company,
    @JsonKey("advanced-stats") stats: Stats,
    quote: Quote
)

but it still says

could not find Lazy implicit value of type io.circe.generic.extras.codec.ConfiguredAsObjectCodec[Bulk]

Upvotes: 0

Views: 645

Answers (1)

Mateusz Kubuszok
Mateusz Kubuszok

Reputation: 27605

Have you enabled macro annotations? Scala 2.13 require flag that you can enable in Ammonite with:

interp.configureCompiler(_.settings.YmacroAnnotations.value = true)

in earlier versions of Ammonite which used Scala 2.12 and earlier you have to use

// replace with Scala version appropriate for your Ammonite
//                                             \/
import $plugin.$ivy.`org.scalamacros:paradise_2.12.11:2.1.1`

Upvotes: 2

Related Questions