Issue with serializing / deserializing of ADTs
See original GitHub issueHi there 👋
I’m not sure if this a known bug / feature or whether something can be tweaked in the configuration to enable this. In cases that we have a defined implicit codec for both the sealed trait and the leaf node, serializing the leaf node will always use the implicit of the leaf node regardless if the type is specified as the ADT. Subsequently, deserialization fails if we try to deserialize it as the ADT. Example:
import com.github.plokhotnyuk.jsoniter_scala.core._
sealed trait Pet {
val name: String
}
object Pet {
import com.github.plokhotnyuk.jsoniter_scala.macros._
implicit val petCodec: JsonValueCodec[Pet] = JsonCodecMaker.make
final case class Dog(name: String) extends Pet
object Dog {
implicit val dogCodec: JsonValueCodec[Dog] = JsonCodecMaker.make
}
}
object Main {
import Pet.Dog
def serializePet(pet: Pet): String = writeToString[Pet](pet)
def deserializePet(json: String): Pet = readFromString[Pet](json)
def main(args: Array[String]): Unit = {
val json = serializePet(Dog("Lassie"))
readFromString[Dog](json) // Works
deserializePet(json) // Raises error
}
}
fails with:
Exception in thread "main" com.github.plokhotnyuk.jsoniter_scala.core.JsonReaderException: expected key: "type", offset: 0x00000007, buf:
+----------+-------------------------------------------------+------------------+
| | 0 1 2 3 4 5 6 7 8 9 a b c d e f | 0123456789abcdef |
+----------+-------------------------------------------------+------------------+
| 00000000 | 7b 22 6e 61 6d 65 22 3a 22 4c 61 73 73 69 65 22 | {"name":"Lassie" |
| 00000010 | 7d | } |
+----------+-------------------------------------------------+------------------+
Where this becomes problematic is when we have serialize / deserialize other cases classes in the codebase, some of them having Pet
and some others having Dog
as a parameter. E.g.,
case class Person(name: String, pet: Option[Pet])
case class DogPerson(name: String, dog: Dog)
implicit val personCodec: JsonValueCodec[Person] = JsonCodecMaker.make
implicit val dogPersonCodec: JsonValueCodec[DogPerson] = JsonCodecMaker.make
readFromString[DogPerson](writeToString(DogPerson(...))) // works
readFromString[Person](writeToString(Person(...))) // error
Is there something that can be done in this case?
Issue Analytics
- State:
- Created a year ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Generated serialize/deserialize is wrong for ADTs · Issue #3695
This is incorrect because the payload part should recursively have serialize called on it. The main issue here is that the SerdeGenerator doesn' ......
Read more >ADT Json serialization/deserialization - scala - Stack Overflow
The problem is that the type of d3Decoded is always of type Data2 and not the desired Data3 . The solution I came...
Read more >having issue in Deserialize data in future callout methods and ...
Introducing JSON - and the Apex implementation of deserialization/serialization in the JSON Class does not preserve any pre-excisting order.
Read more >Could ADTs extend Product with Serializable in Dotty?
Akka is moving to a different default serialization . Seriously, it looks to me like Java serialization is becoming steadily more vestigial. It ......
Read more >Serialization (C#) | Microsoft Learn
When you use basic serialization, the versioning of objects may create problems. You would use custom serialization when versioning issues are ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@kyri-petrou Thanks a bunch for your feedback! Happy coding and running with jsoniter-scala!
Thanks again a lot for your help! Very much appreciated