how to Serialize the Enum fields in scala using Gson library

I have a class Message i want it to serilize into Gson and then Deserialize it but the problem is my Message class contains Enum fields i want these fields to be included when its converted to json but it throws stackoverflowexception
Here is my code
Message.scala

package models.UserNotifications.MailMessages

import org.joda.time.DateTime
import models.UserNotifications.MailMessages.ReadStatus._
import models.UserNotifications.MailMessages.DeleteStatus._
import models.UserNotifications.MailMessages.DeleteReason._
import scala.collection.mutable.MutableList
import java.time.LocalDateTime;



    @SerialVersionUID(1)
    class Message(val uuid: Int ,val subject : String, val body : String, var artworkUuid : Int,
        val dateTime : LocalDateTime = LocalDateTime.now()) extends Ordered[Message] with Serializable{


       def this(){
        this(0,"", "",0)
       }


    def this(uuid: Int,
       subject : String,
       body : String,
       artworkUuid : Int,
       dateTime : LocalDateTime,
       readStatus : ReadStatus,
       delete : DeleteStatus
       ){
    this( uuid , subject , body , artworkUuid,dateTime)
    this.readStatus = readStatus
    this.deleteStatus= delete
    }


      /*
       * returns sorted dateTime
       */
      def compare(other: Message): Int = other.dateTime.compareTo(this.dateTime)

       var readStatus : ReadStatus = READ
       var deleteStatus : DeleteStatus = DELETED

        var IdList = new MutableList[Int]()
        addUuidToList(uuid)


    /*
     * Setters
     */

         def setArtworkUuid(setartworkUuid : Int)={
          artworkUuid = setartworkUuid
        }

        def setReadStatus(readstatus : String)={
          readStatus = ReadStatus.withName(readstatus)
        }

        def setDelete(setdelete : String) = {
          deleteStatus = DeleteStatus.withName(setdelete)
        } 

    def addUuidToList(uuid : Int)  = {
        IdList += uuid   
      }



    /*
     * Getters
     */

        def getUuid : Int ={
          uuid
        }
        def getSubject : String = {
          subject
        }

        def getBody : String ={
          body
        }

        def getArtworkUuid : Int = {
          artworkUuid
        }

       def getdateTime : LocalDateTime = {
           dateTime  
        }

        def getReadStatus : ReadStatus = {
          readStatus
        }

        def getDeleteStatus  : DeleteStatus = {
          deleteStatus
        }

        def getUuidsList:MutableList[Int]  = {
        IdList
      }


    }

ReadStatus,scala
package models.UserNotifications.MailMessages

import com.google.gson.annotations.SerializedName

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

}

object Test extends App{
 import com.google.gson._
        import models.UserNotifications.MailMessages.ReadStatus._
        import models.UserNotifications.MailMessages.DeleteStatus._
        import java.time.LocalDateTime;


  val msg=new Message(321,"subject1","bodyText1",101,LocalDateTime.now(),READ,DELETED)

       val gson = new Gson();
       val g=gson.toJson(msg)
           println("converted to  json"+g)

}

if i put @transient annotation:

   @transient var readStatus : ReadStatus = READ
   @transient var deleteStatus : DeleteStatus = DELETED

then its converted into json and prints following output

converted to json{"uuid":321,"subject":"subject1","body":"bodyText1","artworkUuid":101,"dateTime":{"date":{"year":2015,"month":7,"day":8},"time":{"hour":13,"minute":32,"second":38,"nano":624000000}},"IdList":{"first0":{"elem":321,"next":{}},"last0":{"elem":321,"next":{}},"len":1}}

but if I removed the @transient annotation

following exception thrown

[error] (run-main-0) java.lang.StackOverflowError
java.lang.StackOverflowError
    at java.lang.StringBuffer.append(StringBuffer.java:272)
    at java.io.StringWriter.write(StringWriter.java:101)
    at com.google.gson.stream.JsonWriter.string(JsonWriter.java:534)
    at com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:402)
    at com.google.gson.stream.JsonWriter.value(JsonWriter.java:495)
    at com.google.gson.internal.bind.TypeAdapters$7.write(TypeAdapters.java:246)
    at com.google.gson.internal.bind.TypeAdapters$7.write(TypeAdapters.java:231)
    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)

please help me how can i add these two fields

 var readStatus : ReadStatus = READ
    var deleteStatus : DeleteStatus = DELETED

to the object for serialization


Source: json

Leave a Reply