Embedded collection of Enumeration values causes errors in serialization of collection of objects
See original GitHub issueI have observed weird behavior of serialization of objects with collection of Enumeration values (for example case class User(roles: Seq[Roles.Value])
) . Serialization of collection of this objects (for example Seq[User]
) produces JSON Object with double-colon attribute: {"::":[obj1, obj2, ...]}
, when correct output should be JSON Array: [obj1, obj2, ...]
.
This error does not occur when instead of Enumeration values we have other objects (even with custom Encoders).
Workaround: Implement your own Encoder
for collection of Enumeration values.
Minimal example:
import io.circe._
import io.circe.syntax._
import io.circe.generic.auto._
object Roles extends Enumeration {
val Admin, Guest = Value
}
implicit object RoleEncoder extends Encoder[Roles.Value] {
override def apply(r: Roles.Value): Json = r.toString.asJson
}
case class User(roles: Seq[Roles.Value])
val u = User(Seq(Roles.Admin))
Seq(u).asJson.noSpaces
// ERROR: res0: String = {"::":[{"roles":["Admin"]}]}
implicit object RolesSeqEncoder extends Encoder[Seq[Roles.Value]] {
override def apply(rs: Seq[Roles.Value]): Json = rs.map(_.asJson).asJson
}
Seq(u).asJson.noSpaces
// OK: res1: String = [{"roles":["Admin"]}]
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Jackson - EnumValues serialization fails when dynamically ...
I'm using Spring Boot and I have an error when it tries to convert a Bean into JSON with Jackson . ... After...
Read more >Serialization of enum with associated type | topolog's tech blog
When encoding an object we put all the properties to the container and then this container is being serialised to the binary data....
Read more >problem saving embedded objects - Google Groups
when trying to persist embedded enums/objects i get the following error: ... Caused by: ... set the value '7:0' to the property 'protocol'...
Read more >Java Object Serialization Specification: 1 - System Architecture
A serializable class can declare which of its fields are saved or restored, and write and read optional values and objects.
Read more >JSON Serialization Usage
This causes problems when you want a connected object model in your server, ... Collections are serialized in full including the shallow copy...
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 FreeTop 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
Top GitHub Comments
@dzikowski I’ve got an enumeration-less minimization:
And I’ve tracked down the problem, although I don’t have a solution yet (short of adding a Shapeless dependency to core so we can use
Lazy
in the defaultSeq
encoder, which unfortunately isn’t really an option).Thanks for reporting this! It’s definitely a big problem, in an area where I thought we’d worked out all the corner cases. I’ll do my best to get a fix in today.
Confirmed that this is resolved in all current Scala version on master.