sarah w
sarah w

Reputation: 3515

class object is not converting in Gson

i have a class named Message i want it to convert into json using Gson library but it throws stackoverflow exception here is my class

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 
  } 


}

and i am converting it in json like this

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()
        }

} 

and i got the following exception

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)

I found some similar question on stack but i did not solve my problem if my class contains circular references how should i remove this and make this work please help me

these are the two classes

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

}

Upvotes: 0

Views: 850

Answers (1)

bjfletcher
bjfletcher

Reputation: 11518

The delete and messageStatus fields get included in the serialization, one of which contains some cyclic dependency. I'd either add @transient annotation:

@transient var delete : DeleteStatus = DELETED

or ask Gson to exclude these fields.

Upvotes: 2

Related Questions