Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zio.json.ast.Json.as[X] does not support nullable/Option fields #710

Open
GiGurra opened this issue Aug 20, 2022 · 6 comments
Open

zio.json.ast.Json.as[X] does not support nullable/Option fields #710

GiGurra opened this issue Aug 20, 2022 · 6 comments

Comments

@GiGurra
Copy link

GiGurra commented Aug 20, 2022

ast Json as method does not respect nullable fields. Instead of decoding to option it produces decoding error.

Example:

case class SomeClass(a: Int, b: Option[String])
object SomeClass:
  given JsonCodec[SomeClass] = DeriveJsonCodec.gen[SomeClass]

val start = SomeClass(1, None)
println(start)
println(start.toJson)
val back = start.toJson.fromJson[SomeClass]
val backAst = start.toJson.fromJson[Json]
println(back)
println(backAst)
println(backAst.flatMap(ast => ast.toJson.fromJson[SomeClass]))
println(backAst.flatMap(_.as[SomeClass]))

Prints

SomeClass(1,None)
{"a":1}
Right(SomeClass(1,None))
Right({"a":1})
Right(SomeClass(1,None))
Left(Missing fields: b) // WRONG
@loicdescotte
Copy link

loicdescotte commented Sep 7, 2022

It seems ok like this (using last RC) :

import zio.json._

case class SomeClass(a: Int, b: Option[String])
object SomeClass :
  given JsonCodec[SomeClass] = DeriveJsonCodec.gen[SomeClass]

val start = SomeClass(1, None)
println(start)

val back = start.toJson
println(back)
println(back.fromJson[SomeClass])

Prints:

SomeClass(1,None)
{"a":1}
Right(SomeClass(1,None))

@GiGurra
Copy link
Author

GiGurra commented Sep 7, 2022

@loicdescotte Your test does not extract/map/deserialize from the json ast. back in your case is just a string.

The issue is still there in the current/latest RC (0.3.0-RC11 and 0.3.0-RC11+7-12139a3d-SNAPSHOT)

you can verify this yourself by simply adding the last two lines of this to your own test code:

import zio.json._
case class SomeClass(a: Int, b: Option[String])
object SomeClass:
  given JsonCodec[SomeClass] = DeriveJsonCodec.gen[SomeClass]

val start = SomeClass(1, None)
println(start)

val back = start.toJson
println(back)
println(back.fromJson[SomeClass])

val backAst = start.toJsonAST
println(backAst.flatMap(_.as[SomeClass])) // fails

@ex0ns
Copy link
Contributor

ex0ns commented Nov 11, 2022

I ended up having a similar problem, but only in Scala 3, according to the syntax you used I guess this is you case too, #781 should solve that

@guizmaii
Copy link
Member

@GiGurra @loicdescotte @ex0ns Is this fixed now?

@ex0ns
Copy link
Contributor

ex0ns commented Oct 31, 2023

https://github.com/zio/zio-json/blob/series/2.x/zio-json/shared/src/test/scala/zio/json/DecoderSpec.scala#L130 is still passing so I guess it does, I have to say that I don't even remember why I needed that or if I even used it after fixing, but in theory, that should work

@guizmaii
Copy link
Member

@fsvehla I think we can close this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants