0

I have the following, and want to use jerkson in scala, but am having issues with this. I'm sure this is a very amateur error, but was hoping to get some help from here.

  scala> val orig=List("this", List("a", "b", List("c", "d")))
  orig: List[java.lang.Object] = List(this, List(a, b, List(c, d)))

 val json_ver=generate(orig)
 json_ver: String = ["this",["a","b",["c","d"]]]

 //now i want to go from json_ver back to orig list of lists
 //I've tried parse[List[Any]](json_ver)
 //parse[List[List[Any]]](json_ver)

All to no avail. I would really appreciate it if someone could point me in the right direction

JPC
  • 1,891
  • 13
  • 29

1 Answers1

2

A word of warning: the original Codahale Jerkson has been abandoned and there's no official build for Scala 2.10 (although there are some Github forks for 2.10). jackson-module-scala (which jerkson wraps), on the other hand, is fully maintained and supported.

[EDIT] after clarifying the question.

The original data structure is using Lists of Any (Lists and Strings). The one that's returned from the parser is List, but lists inside of it arejava.util.ArrayList. That type is also a list, but not the same and doesn't fit in to Scala collections as natively. Among other things, it has a different implementation of toString, which is why the output is different. Note that it's still a list of Any, just not the Scala Lists.

One way to solve it would be just to use collection converters to convert to Scala:

import scala.collection.JavaConverters._
// somewhere down the line
val parsedList = parse[List[Any]](json_ver)

parsedList.foreach {
   case s: String => println("string: " + s)
   case l: util.ArrayList[Any] => doSomething(l.asScala.toList)
}

...
def doSomething(lst: List[Any]) {
   println(lst)
}

Another way is that if you were to use Jackson, you could configure it to use Java arrays:

val mapper = new ObjectMapper()
mapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true)
mapper.registerModule(DefaultScalaModule)
mapper.readValue[List[Any]](json_ver, classOf[List[Any]])
Alex Yarmula
  • 10,477
  • 5
  • 33
  • 32
  • sorry, what I meant was: I want to be able to convert the json_ver back to a structure that matches that of orig, in other words a list with multiple nested lists. The parse command here doesn't seem to convert all the json objects that are within json_ver. – JPC Apr 08 '13 at 00:11
  • The original data structure is not a list of lists. It's `List[Object]`, because there are both Strings ("this") and Lists in it. The one returned by the parse method is `List[Any]`. Besides that difference, I see the same elements in both. – Alex Yarmula Apr 08 '13 at 00:19
  • ok, so I shouldn't expect the parse to make this List(this,List(a,b,List(c,d))? perhaps I misunderstood how parse here worked – JPC Apr 08 '13 at 00:50
  • I really appreciate the help. I don't seem to follow though on when to use this conversion. Would you mind posting a snippet of code which does this with the example provided above? That would be awesome! – JPC Apr 08 '13 at 02:53
  • I think it's best to think of a use case of what you're trying to accomplish. I updated the answer with an example of converting ArrayList to a Scala list. In general, sounds like what you really want is some class or case class to wrap your data structure, instead of just using lists. – Alex Yarmula Apr 08 '13 at 04:30