4

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?

Electric Coffee
  • 11,733
  • 9
  • 70
  • 131

1 Answers1

2

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.

Community
  • 1
  • 1
Dave Swartz
  • 910
  • 6
  • 14
  • 1
    I'm not serializing you know... I'm deserializing – Electric Coffee Mar 16 '14 at 22:36
  • 2
    Given this is written communication, which is so prone to misinterpretation of tone, I will chalk up what I am detecting in your responses as our communication styles not being compatible. Best of luck, but I will not be assisting you any further. – Dave Swartz Mar 16 '14 at 23:14
  • apologies for incompatibility – Electric Coffee Mar 16 '14 at 23:17
  • @ElectricCoffee JavaConverters has "asScala" methods for going in the opposite direction. See the ScalaDoc. I don't know Gson, so I can't provide exact code without a lot of pointless digging. – Ed Staub Mar 17 '14 at 03:15
  • @EdStaub gson _should_ just act as an instantiator and put in values as needed... but I could be wrong, the issue I see with using asJava or asScala, is that it would probably require me to edit every class, in which case it would be easier to just use an array... – Electric Coffee Mar 17 '14 at 06:40
  • @ElectricCoffee If that's really onerous, I suspect some judicious use of implicits might help - but it would depend a lot on context. As should be obvious from the other thread, most folks try to reuse someone else's Scala xSON wrapper, to avoid having to reinvent this wheel. – Ed Staub Mar 17 '14 at 15:47