question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Embedded collection of Enumeration values causes errors in serialization of collection of objects

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
travisbrowncommented, Sep 26, 2016

@dzikowski I’ve got an enumeration-less minimization:

import io.circe._
import io.circe.syntax._
import io.circe.generic.auto._

object X { case class Foo(i: Int) }

implicit val encodeFoo: Encoder[X.Foo] = Encoder[Int].contramap(_.i)

case class Foos(values: Seq[X.Foo])

List(Foos(List(X.Foo(1)))).asJson.noSpaces
// res0: String = {"::":[{"xs":[1]}]}

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 default Seq 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.

0reactions
travisbrowncommented, Jun 11, 2019

Confirmed that this is resolved in all current Scala version on master.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found