如何使用Gson库在scala中序列化Enum字段

时间:2015-07-08 08:38:21

标签: json scala serialization enums gson

我有一个类消息我希望它将其串行化为Gson然后反序列化它但问题是我的Message类包含Enum字段我希望这些字段在转换为json时被包含但是它会抛出stackoverflowexception
这是我的代码 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,阶 package models.UserNotifications.MailMessages

导入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)

}

如果我提出@transient注释:

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

然后将其转换为json并在输出后打印

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

但是如果我删除了@transient注释

抛出异常后

[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)

请帮助我如何添加这两个字段

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

到序列化对象

0 个答案:

没有答案