iCodeLikeImDrunk
iCodeLikeImDrunk

Reputation: 17866

Why does Gson().toJson() of object with Enumeration throw StackOverflowError?

import Tenum.Tenum
import com.google.gson.Gson

object Temp extends App {

  val gson = new Gson()
  gson.toJson(new Status("foo", Tenum.X))

  System.exit(1)
}

case class Status(id: String, tenum: Tenum)

object Tenum extends Enumeration {

  type Tenum = Value

  val X = Value
}

I thought it would just print:

{id:"foo", tenum:"X"}

Upvotes: 0

Views: 308

Answers (1)

lucas
lucas

Reputation: 6971

Probably because Scala Enumeration has a field on it that is self referential, and Gson is trying to serialize it, getting stuck in an endless loop.

I'd try serializing Tenum.X.toString (or providing a json field on your enum) or looking around for a Scala Gson wrapper.

Upvotes: 2

Related Questions