I'm trying to convert an array of JSON objects from a key to an array of objects in Scala...
Here is the code:
case class RoomList(val rooms : Array[Room])
case class Room(val name : String)
val json = "{\"rooms\" : [{\"name\" : \"Test\"}]}"
println(json.unpickle[RoomList])
Here is the exception
Exception in thread "main" java.lang.InstantiationException: [Lhipchat.Room;
at sun.misc.Unsafe.allocateInstance(Native Method)
at hipchat.HipChat$HipchatRoomListUnpickler1$2$ScalaArray$u005BhipchatRoom$u005DUnpickler1$2$.unpickle(HipChat.scala:46)
at hipchat.HipChat$HipchatRoomListUnpickler1$2$.unpickle(HipChat.scala:46)
at hipchat.HipChat.getRooms(HipChat.scala:46)
at bot.Bot$.main(Bot.scala:11)
at bot.Bot.main(Bot.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Here is a RoomList:
val room = new Room("test")
val rooms = Array(room)
val rl = new RoomList(rooms)
println(rl.pickle)
Pickled:
JSONPickle({
"tpe": "hipchat.RoomList",
"rooms": {
}
})
And a pickled Room:
JSONPickle({
"tpe": "hipchat.Room",
"name": "test"
})
Yep, you're correct that there was a period of time when scala/pickling had some trouble dealing with certain collection types of objects. Though, it's worth noting that this has been fixed for a little while now.
Given:
case class Room(val name: String)
case class RoomList(val rooms: Array[Room])
And some RoomList
, rl
, for example,
val rl = RoomList(Array(Room("foo"), Room("biz"), Room("bang")))
In the REPL, you get:
scala> val p = rl.pickle
p: scala.pickling.json.JSONPickle =
JSONPickle({
"tpe": "RoomList",
"rooms": {
"elems": [
{
"tpe": "Room",
"name": "foo"
},
{
"tpe": "Room",
"name": "biz"
},
{
"tpe": "Room",
"name": "bang"
}
]
}
})
We can check in the REPL to make sure that the pickled/unpickled version and the original version of rl
have the same elements:
scala> rl.pickle.unpickle[RoomList].rooms.sameElements(rl.rooms)
res0: Boolean = true
Added this to the scala/pickling test suite as well :) https://github.com/scala/pickling/blob/2.10.x/core/src/test/scala/pickling/roomlist-object-array.scala
"Collections of non-primitive types are not supported" (yet)
https://github.com/scala/pickling/issues/6
From what I can see, scala.pickling
is still very rough around the edges.
Furthermore, the default pickling format JSONPickleFormat
requires some meta-data (the tpe
field) and I'm guessing won't be parsing arbitrary JSON objects.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.