zio / zio-json

Fast, secure JSON library with tight ZIO integration.
https://zio.dev/zio-json
Apache License 2.0
410 stars 146 forks source link

JsonCodec ≠ JsonDecoder + JsonEncoder: could not find JsonCodec.Typeclass for type String #167

Open guizmaii opened 3 years ago

guizmaii commented 3 years ago

Hi everyone,

I hope it's not too early to report things.

I tried to replace Circe in a Scala/ScalaJS project and I had the following errors:

[error] /Users/jules.ivanic/jules_ivanic/workspace/http4s-laminar-stack/modules/shared/src/main/scala/example/shared/Protocol.scala:10:70: magnolia: could not find JsonCodec.Typeclass for type String
[error]     in parameter 'search' of product type example.shared.Protocol.GetSuggestions.Request
[error]       implicit final val codec: JsonCodec[Request] = DeriveJsonCodec.gen
[error]                                                                      ^
[error] /Users/jules.ivanic/jules_ivanic/workspace/http4s-laminar-stack/modules/shared/src/main/scala/example/shared/Protocol.scala:15:71: magnolia: could not find JsonCodec.Typeclass for type Seq[String]
[error]     in parameter 'suggestions' of product type example.shared.Protocol.GetSuggestions.Response
[error]       implicit final val codec: JsonCodec[Response] = DeriveJsonCodec.gen
[error]                                                                       ^
[error] two errors found

It's open-source so you can easily reproduce it. See: https://github.com/keynmol/http4s-laminar-stack/pull/4/files

fsvehla commented 3 years ago

Hello, @guizmaii

I hope it’s not too late to respond :)

I have to dive deeper but there seems to be some issue with DeriveJsonCodec which does not happen when using Encoder + Decoder separately.

If you want to continue to work in the meantime, this encoding works:

package example.shared

import zio.json._

object Protocol {
  object GetSuggestions {

    final case class Request(search: String, prefixOnly: Option[Boolean] = None)
    object Request {
      implicit final val encoder: JsonEncoder[Request] = DeriveJsonEncoder.gen
      implicit final val decoder: JsonDecoder[Request] = DeriveJsonDecoder.gen
    }

    final case class Response(suggestions: Seq[String])
    object Response {
      implicit final val encoder: JsonEncoder[Response] = DeriveJsonEncoder.gen
      implicit final val decoder: JsonDecoder[Response] = DeriveJsonDecoder.gen
    }
  }
}
guizmaii commented 3 years ago

@fsvehla Thanks! 🙏

paulpdaniels commented 2 years ago

Was there any resolution of this in the latest versions?