I have a Map like below
val map : scala.collection.mutable.Map[String,Any] = Map(
dummy1 -> ["cat1", "hash1", 101, 1373269076, {"1" : ["dummy", "dummy", "dummy"]}],
dummy2 -> ["cat1", "hash1", 102, 1373269076, {"2" : ["dummy", "dummy", "dummy"]}],
dummy3 -> ["cat1", "hash1", 103, 1373269076, {"3" : ["dummy", "dummy", "dummy"]}]
)
I converted it into a Json string and then wrote it into a file with the code below
Some(new PrintWriter("foo.txt")).foreach{p =>
p.write(JSONObject(map.toMap).toString()); p.close
}
Am able to read the Json string from the file using
val json_string = scala.io.Source.fromFile("foo.txt").getLines.mkString
How do I get my map back from the Json string above?
EDIT: Am able to read the map with
val map1 = JSON.parseFull(json_string).get.asInstanceOf[Map[String,Any]]
But, this process is taking more time as the size of the map increases.
Try using a likely faster (and more thorough) mapper.
I would recommend using JacksMapper which wraps the excellent Jackson for a more pleasant Scala usage.
Serializing to JSON becomes as simple as
... and deserializing
(edit)
You can make also writing and reading simple one-liners using FileUtils from commons-io doing
and
I actually got a lot more use from json4s. The documentation is much more clear and comprehensive, and the usage seems slightly easier.
A similar operation to the one you are requesting would look like this