Comments (8)
Thanks for this report! There are two issues here. The first is that you've got some maps with non-string keys. JSON doesn't support objects with non-string keys, and there are a number of ways to manage codecs for arbitrary maps—we're working on this in #186, and a solution will be available in 0.4.0, but for now you have to provide instances for Map[K, V]
manually if K
isn't String
. This isn't too hard—you can write something like this:
def decodeMap[K, V: Decoder](fromString: String => K): Decoder[Map[K, V]] =
Decoder[Map[String, V]].map(
_.map {
case (k, v) => (fromString(k), v)
}
)
def encodeMap[K, V: Encoder](toString: K => String): Encoder[Map[K, V]] =
Encoder[Map[String, V]].contramap(
_.map {
case (k, v) => (toString(k), v)
}
)
And then use these to define instances for your types.
The more serious problem is that Scrooge's traits aren't sealed, so while it's possible to get a LabelledGeneric
instance for demo.IntOpt.Immutable
, it's not for demo.IntOpt
, and that's what you need for the codecs, since that's the static type in the coproduct representations. You can derive instances for the non-Immutable
types very painfully:
implicit def genIntOpt(implicit
gen: LabelledGeneric[IntOpt.Immutable]
): LabelledGeneric.Aux[IntOpt, gen.Repr] = new LabelledGeneric[IntOpt] {
type Repr = gen.Repr
def to(t: IntOpt): Repr = t match {
case i: IntOpt.Immutable => gen.to(i)
case _ => gen.to(t.copy().asInstanceOf[IntOpt.Immutable])
}
def from(r: Repr): IntOpt = gen.from(r)
}
And then if you have instances for all of your maps with non-string keys (and for TFieldBlob
, etc.), you can automatically derive codecs for your Scrooge types.
I'd suggest not doing this, though. It's a lot of hoops to jump through, and in many cases you're better off keeping the Scrooge-generated classes as close as possible to the boundaries of your program, anyway. If you really wanted, you could lobby for the generated traits to be sealed (see @notxcain's comments on Gitter), but I'm not sure how well-received that will be, since I don't know off the top of my head what the motivation was for leaving them open.
from circe.
in many cases you're better off keeping the Scrooge-generated classes as close as possible to the boundaries of your program, anyway
Could you expand on that? By boundaries do you mean e.g. after an RPC call to a thrift service it's better to map the data from the scrooge generated class into our own case classes?
from circe.
@arnihermann Right. I know there's some documentation somewhere that talks about best practices in that respect, but it may have been Twitter-internal—if I find it I'll post a link here.
from circe.
Thanks that'd be great, I'd love to know more about those best practices.
from circe.
I imported the following packages and used your code , but I still got error.
import demo._
import shapeless._
import io.circe.generic.semiauto._
import io.circe.parser._
import io.circe.syntax._
implicit val gen = LabelledGeneric[IntOpt.Immutable]
implicit def genIntOpt(implicit
gen: LabelledGeneric[IntOpt.Immutable]
): LabelledGeneric.Aux[IntOpt, gen.Repr] = new LabelledGeneric[IntOpt] {
type Repr = gen.Repr
def to(t: IntOpt): Repr = t match {
case i: IntOpt.Immutable => gen.to(i)
case _ => gen.to(t.copy().asInstanceOf[IntOpt.Immutable])
}
def from(r: Repr): IntOpt = gen.from(r)
}
Compiler complains:
scala> IntOpt(Some(1)).asJson
<console>:43: error: could not find implicit value for parameter encoder: io.circe.Encoder[demo.IntOpt]
IntOpt(Some(1)).asJson
^
scala> deriveEncoder[IntOpt]
<console>:43: error: could not find Lazy implicit value of type io.circe.generic.encoding.DerivedObjectEncoder[demo.IntOpt]
deriveEncoder[IntOpt]
^
scala> deriveDecoder[IntOpt]
<console>:43: error: could not find Lazy implicit value of type io.circe.generic.decoding.DerivedDecoder[demo.IntOpt]
deriveDecoder[IntOpt]
^
scala> IntOpt(Some(1)).asJson
<console>:44: error: could not find implicit value for parameter encoder: io.circe.Encoder[demo.IntOpt]
IntOpt(Some(1)).asJson
Am I missing something?
from circe.
Any update? 👍
from circe.
This problem bit me too and confused me even further because sometimes my code would compile. I used the macro from https://github.com/travisbrown/scrooge-circe-demo/blob/master/src/main/scala/demo/package.scala to get rid of the passthrough fields, which cannot be generically derived (Map[Short, TFieldBlob]). Only after I moved the macro into its own compilation path did the code reliably compile. scalac was not expanding the macro during compilation, but wasn't giving the error that it wasn't expanding it. Now it works beautifully, though compiles slowly -- 1 minute when using finch with about 20 routes in one file.
from circe.
Closing this since scrooge-shapes now provides LabelledGeneric
instances that work with circe-generic in many cases, and circe-derivation also supports derivation for Scrooge types directly in many cases.
from circe.
Related Issues (20)
- How do I map a particular key and value in a custom way? HOT 2
- io.circe.pointer.literal.PointerInterpolatorSuite seems Flaky (or there's a bug?) HOT 2
- Maximal number of successive inlines (32) exceeded, Maybe this is caused by a recursive inline method? HOT 7
- Circe cannot decode output from auto-derived codec on Scala 3 HOT 1
- Scala3 derivation incorrectly assumes collection type for generic class HOT 4
- Stack overflow on 0.14.6 HOT 4
- Unexpected derivation of Encoder[Option[T]]
- Modifying JsonObject using ACursor/HCursor performance hit due to megamorphic calls HOT 1
- Invalid Json tree with `null` (java) values, NPE in printer HOT 1
- a problem with current derivations HOT 7
- Properly parse JSON object key with quotes HOT 1
- usage of ISO_ZONED_DATE_TIME as default formatter in encoder HOT 3
- Scala 2.13: derivedConfiguratedEncoder becomes unconfigured under some conditions HOT 1
- Expected `json` string interpolator from io.circe.literal to be able to interpolate variable inside of value strings HOT 3
- Invalid pathToRoot for `missing required field` validation error HOT 3
- flaky test PointerInterpolatorSuite HOT 2
- `StackOverflowError` if local class since 0.14.7 with Scala 3 HOT 2
- Adjust contrast of website nav menu colours
- Refined version update for JS and JVM HOT 4
- Move refined into its own repo with its own release cycle
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from circe.