Skip to content

Instantly share code, notes, and snippets.

@atcol
Last active August 29, 2015 14:18
Show Gist options
  • Save atcol/b96fac19c4ebd3163bb5 to your computer and use it in GitHub Desktop.
Save atcol/b96fac19c4ebd3163bb5 to your computer and use it in GitHub Desktop.
json4s & Jackson StackOverflow error when serializing case class with Char field

Using the model.scala and build.sbt files defined in this gist, perform the following to reproduce a StackOverflowError when serializing a case class instance that has a Char field.

Now start sbt console:

sbt console

and paste the following:

import org.json4s._
import org.json4s.jackson.JsonMethods._
import org.json4s.jackson.Serialization
import org.json4s.{DefaultFormats, Formats}
import org.json4s.jackson.Json
import model.SomeType

val json = Json(DefaultFormats)
json.write(SomeType('a'))

You should now see a stacktrace along the lines of:

java.lang.StackOverflowError
  at scala.reflect.Manifest$class.canEqual(Manifest.scala:52)
  at scala.reflect.ManifestFactory$ClassTypeManifest.canEqual(Manifest.scala:239)
  at scala.reflect.Manifest$class.equals(Manifest.scala:58)
  at scala.reflect.ManifestFactory$ClassTypeManifest.equals(Manifest.scala:239)
  at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:1024)
  at org.json4s.reflect.package$Memo.apply(package.scala:36)
  at org.json4s.reflect.ScalaType$.apply(descriptors.scala:33)
  at org.json4s.reflect.Reflector$.scalaTypeOf(Reflector.scala:33)
  at org.json4s.Extraction$.decomposeObject$1(Extraction.scala:89)
  at org.json4s.Extraction$.internalDecomposeWithBuilder(Extraction.scala:196)
  at org.json4s.Extraction$.addField$1(Extraction.scala:82)
  at org.json4s.Extraction$.decomposeObject$1(Extraction.scala:110)
  at org.json4s.Extraction$.internalDecomposeWithBuilder(Extraction.scala:196)
  at org.json4s.Extraction$.addField$1(Extraction.scala:82)
  at org.json4s.Extraction$.decomposeObject$1(Extraction.scala:110)
  at org.json4s.Extraction$.internalDecomposeWithBuilder(Extraction.scala:196)
  at org.json4s.Extraction$.addField$1(Extraction.scala:82)
  at org.json4s.Extraction$.decomposeObject$1(Extraction.scala:110)

etc

name := """json4s_test"""
version := "1.0"
scalaVersion := "2.11.6"
libraryDependencies ++= Seq(
"org.json4s" %% "json4s-jackson" % "3.2.11"
)
javacOptions ++= Seq("-source", "1.6", "-target", "1.6")
package model
case class SomeType(charField: Char) {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment