mongodb - How to serialize a Scala Collection List into json using Gson lib -
i have class named inbox
contains int
, list
of messages
. message
class.
the problem want store objects of inbox
class in mongodb. have serialize these objects using gson, throws exception on deserialization.
java.lang.runtimeexception: failed invoke public scala.collection.immutable.list() no args
here inbox
class :
@serialversionuid(1) class inbox(val uuid: int, var messagelist: listbuffer[message]) extends serializable { adduuidtolist(uuid) /* * auxiliary constructor */ def this() { this(0, listbuffer[message]()) } def addtomessagelist(addmessage: message) = { messagelist += addmessage } var idlist = new mutablelist[int]() def adduuidtolist(uuid : int) = { idlist += uuid } /* * getters */ def getuuid: int = uuid /* * returns sorted list based on datetime attribute of message class */ def getmessagelist : listbuffer[message] = { //var sortedlist = messagelist.sorted messagelist } def getuuidslist: mutablelist[int] = idlist }
and message
class :
class message( val uuid: int, val subject: string, val body: string, var artworkuuid: int, val datetime: localdatetime = localdatetime.now() ) extends ordered[message] serializable
my test
application :
object test extends app { val inbox = new inbox(333,messagelist) val gson = new gson() val g = gson.tojson(inbox) println(g + " converting json") var inboxobj = gson.fromjson(g, classof[inbox]) println("message object returned " + inboxobj) }
the output printed on console :
{"uuid":333,"messagelist":{"scala$collection$mutable$listbuffer$$start":{},"last0":{"head":{"uuid":321,"subject":"subject1","body":"bodytext1","artworkuuid":101,"datetime":{"date":{"year":2015,"month":7,"day":14},"time":{"hour":11,"minute":6,"second":51,"nano":579000000}},"readstatusint":0,"delete":{"deletestatusint":1,"deletereasonint":1}},"tl":{}},"exported":false,"len":2},"idlist":{"first0":{"elem":333,"next":{}},"last0":{"elem":333,"next":{}},"len":1}}converting json [error] (run-main-0) java.lang.runtimeexception: failed invoke public scala.collection.immutable.list() no args java.lang.runtimeexception: failed invoke public scala.collection.immutable.list() no args @ com.google.gson.internal.constructorconstructor$3.construct(constructorconstructor.java:107) @ com.google.gson.internal.bind.reflectivetypeadapterfactory$adapter.read(reflectivetypeadapterfactory.java:186) @ com.google.gson.internal.bind.reflectivetypeadapterfactory$1.read(reflectivetypeadapterfactory.java:103) @ com.google.gson.internal.bind.reflectivetypeadapterfactory$adapter.read(reflectivetypeadapterfactory.java:196) @ com.google.gson.internal.bind.reflectivetypeadapterfactory$1.read(reflectivetypeadapterfactory.java:103) @ com.google.gson.internal.bind.reflectivetypeadapterfactory$adapter.read(reflectivetypeadapterfactory.java:196) @ com.google.gson.gson.fromjson(gson.java:810) @ com.google.gson.gson.fromjson(gson.java:775) @ com.google.gson.gson.fromjson(gson.java:724) @ com.google.gson.gson.fromjson(gson.java:696) @ testactor$.delayedendpoint$testactor$1(testactor.scala:307) @ testactor$delayedinit$body.apply(testactor.scala:54) @ scala.function0$class.apply$mcv$sp(function0.scala:40) @ scala.runtime.abstractfunction0.apply$mcv$sp(abstractfunction0.scala:12) @ scala.app$$anonfun$main$1.apply(app.scala:76) @ scala.app$$anonfun$main$1.apply(app.scala:76) @ scala.collection.immutable.list.foreach(list.scala:383) @ scala.collection.generic.traversableforwarder$class.foreach(traversableforwarder.scala:35) @ scala.app$class.main(app.scala:76) @ testactor$.main(testactor.scala:54) @ testactor.main(testactor.scala) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) caused by: java.lang.instantiationexception @ sun.reflect.instantiationexceptionconstructoraccessorimpl.newinstance(instantiationexceptionconstructoraccessorimpl.java:48) @ java.lang.reflect.constructor.newinstance(constructor.java:422) @ com.google.gson.internal.constructorconstructor$3.construct(constructorconstructor.java:104) @ com.google.gson.internal.bind.reflectivetypeadapterfactory$adapter.read(reflectivetypeadapterfactory.java:186) @ com.google.gson.internal.bind.reflectivetypeadapterfactory$1.read(reflectivetypeadapterfactory.java:103) @ com.google.gson.internal.bind.reflectivetypeadapterfactory$adapter.read(reflectivetypeadapterfactory.java:196) @ com.google.gson.internal.bind.reflectivetypeadapterfactory$1.read(reflectivetypeadapterfactory.java:103) @ com.google.gson.internal.bind.reflectivetypeadapterfactory$adapter.read(reflectivetypeadapterfactory.java:196) @ com.google.gson.gson.fromjson(gson.java:810) @ com.google.gson.gson.fromjson(gson.java:775) @ com.google.gson.gson.fromjson(gson.java:724) @ com.google.gson.gson.fromjson(gson.java:696)
maybe problem listbuffer convert before java.util list, try code:
import scala.collection.javaconverters._ import scala.collection.mutable.listbuffer import com.google.gson.gson import java.util.arraylist import java.lang.reflect.type import com.google.gson.reflect.typetoken object gsoninscala extends app { var id=4 var fruits = new listbuffer[string]() fruits += "apple" fruits += "banana" fruits += "orange" val tmp : inbox = inbox(id, fruits) val json = tmp.tojson() println("object2json: " + json) } import scala.collection.javaconversions._ import scala.collection.mutable.listbuffer import com.google.gson.gson import java.util.arraylist import com.google.gson.annotations.expose; import java.util.{list => javalist} case class inbox(uuid:int,@expose(deserialize = false) messagelist:listbuffer[string]) { val msglist:javalist[string] = messagelist def tojson() = new gson().tojson(this) }
i output
[info] running gsoninscala object2json: {"uuid":4,"messagelist":{"scala$collection$mutable$listbuffer$$start":{},"last0":{"head":"orange","tl":{}},"exported":false,"len":3},"msglist":["apple","banana","orange"]} [success] total time: 2 s, completed 14-jul-2015 11:07:16
i hope helps you
Comments
Post a Comment