Reputation: 12144
First off here's my code:
// Description.scala
package com.wausoft.jsonrpc.model
import com.google.gson.annotations.SerializedName
class Description {
@SerializedName("Language")
var language = ""
@SerializedName("Description")
var description = ""
}
// Item.scala
package com.wausoft.jsonrpc.model
import com.google.gson.annotations.SerializedName
class Item {
@SerializedName("Number")
var number = 0
@SerializedName("Description")
var description: List[Description] = Nil
}
// Result.scala
package com.wausoft.jsonrpc.model
import com.google.gson.annotations.SerializedName
class Result {
@SerializedName("Type")
var typeNum = 0
@SerializedName("Description")
var description: List[Description] = Nil
@SerializedName("Items")
var items: List[Item] = Nil
}
// Response.scala
package com.wausoft.jsonrpc.model
class Response {
var jsonRPC = ""
var result: List[Result] = Nil
var id = 0
}
// main file
package com.wausoft.jsonrpc
import scala.io.Source
import com.wausoft.jsonrpc.model._
import com.google.gson._
object Program {
def getJson(file: String): String = Source.fromFile(file)("UTF-8").mkString
def parseJFile(json: String): Response = new Gson().fromJson(json, classOf[Response])
// insert main method here
}
I get the error in the title when I try to parse the data to a POSO, it works fine if I replace all of the lists with arrays, but the thing that bothers me is that this works:
var lst: List[Int] = Nil
lst = List(1,2,3,4,5)
lst foreach print // produces 12345
If the above example works, why doesn't my code? I mean I'm basically doing the exact same, with the only exception that I let Gson handle the list making, is it because it's wrapped in a class, or did I miss something?
Upvotes: 4
Views: 3993
Reputation: 910
See How can I use Gson in Scala to serialize a List?.
Quote from the question: "Gson doesn't know about Scala's collection classes". This answer shows how to serialize a class with a Scala List member to JSON using GSON. The former is the source of the problem, the later is the solution.
Upvotes: 2