使用JsonSerializer与Gson和Scala时的StackOverflowError

时间:2014-02-16 09:49:12

标签: scala gson

我正在尝试在简单的测试中使用Gson和Scala。这在打印作者实例时工作正常,我正在接收作者的json表示。但是,当用book实例替换它时,我得到一个StackOverflowError。我在other places中读到,如果类之间存在循环引用,可能会发生这种情况,但我在这里看不到它。我正在附加下面的代码和错误堆栈的一部分,并感谢有关如何解决此问题的任何建议。

代码:

import com.google.gson._
import java.lang.reflect.Type
import scala.collection.mutable._

object GsonTest extends App {
  val gsonBuilder = new GsonBuilder
  gsonBuilder.registerTypeAdapter(classOf[Author], new AuthorSerializer)
  gsonBuilder.registerTypeAdapter(classOf[Book], new BookSerializer)

  val book = Book("test book")
  val author = Author("test author")
  book.authors += author

  val gson = new Gson
  println(gson.toJson(author))
  println(gson.toJson(book))
}

case class Author(name: String)

case class Book(name: String) {
  val authors = MutableList[Author]()
}

class AuthorSerializer extends JsonSerializer[Author] {
  override def serialize(src: Author, typeOfSrc: Type, context: JsonSerializationContext) = {
    val json = new JsonObject
    json.addProperty("name", src.name)
    json
  }
}

class BookSerializer extends JsonSerializer[Book] {
  override def serialize(src: Book, typeOfSrc: Type, context: JsonSerializationContext) = {
    val json = new JsonObject
    json.addProperty("name", src.name)

    val jsonAuthorArray = new JsonArray
    for (author <- src.authors) {
      jsonAuthorArray.add(context.serialize(author))
    }
    json.add("authors", jsonAuthorArray)
    json
  }
}

错误堆栈:

Exception in thread "main" java.lang.StackOverflowError
    at com.google.gson.reflect.TypeToken.equals(TypeToken.java:284)
    at java.util.HashMap.getNode(HashMap.java:578)
    at java.util.HashMap.get(HashMap.java:556)
    at java.util.Collections$SynchronizedMap.get(Collections.java:2644)
    at com.google.gson.Gson.getAdapter(Gson.java:332)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:55)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
    ...and so on

1 个答案:

答案 0 :(得分:3)

您缺少gsonBuilder.create(),因此类型适配器未正确注册:

  val gsonBuilder = new GsonBuilder
  gsonBuilder.registerTypeAdapter(classOf[Author], new AuthorSerializer)
  gsonBuilder.registerTypeAdapter(classOf[Book], new BookSerializer)

  val book = Book("test book")
  val author = Author("test author")
  book.authors += author

  val gson = gsonBuilder.create() // this line !!!
  println(gson.toJson(author))
  println(gson.toJson(book))