zio / zio-json

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

Scala 3 support #15

Open jdegoes opened 3 years ago

jdegoes commented 3 years ago

We should support Scala 3 with the same API as Scala 2. This way users can easily transition from Scala 2 to Scala 3 without having to change any of their ZIO JSON code.

There are several steps to this support:

  1. Enable cross-compilation, which will be easier after #9
  2. Enable type class derivation, this could be done via patching Magnolia or independently via Scala 3 type class derivation
fommil commented 3 years ago

I spent a few hours looking into this. Ignoring tests for now, which are going to have to wait for deps, some import scala.language.implicitConversions are needed (or stop using implicit conversions) and replacing do / while with while according to https://gitter.im/lampepfl/dotty?at=5fbe1088b03a464f082f410d

The efficient replacement for do { body } while (cond) is while ({ body; cond }).

fsvehla commented 3 years ago

Scala 3 in Magnolia is still WIP.

I’m having a branch which uses Scala 3 derivation and implements a subset of the annotations, which I’m now actively working on.

fommil commented 3 years ago

FYI I have a typeclass derivation mechanism that works on Scala 2 and Scala 3. So if we migrated to that we'd get support for both at once.

https://gitlab.com/fommil/shapely

jdegoes commented 3 years ago

The only thing I'm concerned about with shapely is that it appears to use nested eithers / tuples.

But I think it could form the basis for adding derivation directly to ZIO Schema. If we have derivation there, then we can implement ZIO JSON derivation in terms of the Schema.

The limitation right now is that ZIO Schema has no concept of annotations. That would have to be thought out and retrofitted into the existing design.

fommil commented 3 years ago

it appears to use nested eithers / tuples

not sure where you see that. The CaseClassN and SealedTraitN data types are all "flat" so there's one alloc per transformation (and boxing, if folk are using primitives, but nobody's going to be cracking that problem any time soon...)

jdegoes commented 3 years ago

@fommil Probably my mistake, I was trying to "read" the generated code from build.sbt. 😄

My proposal would be to incorporate either @fsvehla's ongoing WIP or Shapley (or parts of both) into ZIO Schema, with added support for annotations, and then base ZIO JSON derivation on that.

fsvehla commented 3 years ago

I’ll make some time to check out ZIO schema this weekend.

fommil commented 3 years ago

magnolia is supposed to support scala 3 now. apparently.

In terms of what's missing, shapely just needs a little macro that can provide access to the annotations, and a little macro that provides the name of a type, and then it should be able to match the magnolia implementation feature for feature.

guizmaii commented 10 months ago

Isn't this one done?