类对象没有在Gson中转换

时间:2015-07-04 12:16:28

标签: json scala gson scala-2.11

我有一个名为Message的类我希望它使用Gson库转换为json但它抛出stackoverflow异常这里是我的类

package models.Notifications
import models.Notifications.MessageStatus._
import models.Notifications.DeleteStatus._
import com.mongodb.BasicDBObject
import com.mongodb.ReflectionDBObject

import org.joda.time.DateTime


@SerialVersionUID(1)
class Message(val uuid : Int,val subject : String, val bodyText : String, var artworkUuid : Int, val dateTime : DateTime) extends  Serializable{

  def this()  = {  
    this(0,"","",1,DateTime.parse("0"))
  }

var messageStatus : MessageStatus = READ
var delete : DeleteStatus = DELETED

def this(uuid: Int,
    subject : String,
    bodyText : String,
    artworkUuid : Int,
    dateTime : DateTime,
    messageStatus : MessageStatus,
    delete : DeleteStatus
    ){
this( uuid , subject , bodyText , artworkUuid,dateTime)
this.messageStatus = messageStatus
this.delete= delete
}

/* 
 *getters 
 * */

  def getUuid : Int ={
  uuid
  } 

  def getSubject : String ={
  subject
  } 

  def getBodyText : String ={
  bodyText
  } 

  def getArtworkUuid : Int ={
  artworkUuid
  }

   def getDateTime : DateTime ={
  dateTime 
  } 


}

我正在像json一样转换它

object Test extends app{
import com.google.gson._
 val createdDate = new DateTime(year1, month1, day1, hour1, min1)

val message=new Message(123,"subject","bodyText",100,createdDate,READ,DELETED)
  val gson = new Gson();
        try{
           val g=gson.toJson(message)

        }catch{
          case e:Exception=>e.printStackTrace()
        }

} 

我得到了以下异常

java.lang.StackOverflowError
    at com.google.gson.reflect.TypeToken.equals(TypeToken.java:284)
    at java.util.HashMap.getNode(HashMap.java:571)
    at java.util.HashMap.get(HashMap.java:556)
    at java.util.Collections$SynchronizedMap.get(Collections.java:2584)
    at com.google.gson.Gson.getAdapter(Gson.java:335)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:55)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:99)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:219)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:99)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:219)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:99)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:219)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:99)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:219)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:99)

我在堆栈上发现了一些类似的问题,但是如果我的类包含循环引用我怎么不删除它并让这个工作请帮助我

我没有解决我的问题

这是两个类

object ReadStatus extends Enumeration{


  type ReadStatus = Value
  val READ , UNREAD = Value

}
package models.UserNotifications.MailMessages

object DeleteStatus extends Enumeration {

  type DeleteStatus = Value
  val DELETED, ACTIVE = Value

}

1 个答案:

答案 0 :(得分:2)

deletemessageStatus字段包含在序列化中,其中一个字段包含一些循环依赖项。我要么添加@transient注释:

@transient var delete : DeleteStatus = DELETED

或向Gson询问exclude these fields