Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala - Writing Json object to file and reading it

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.

like image 996
yAsH Avatar asked Jul 08 '13 07:07

yAsH


People also ask

Is it possible to read and write JSON files in Scala?

You can access this package with a separate import, but scala.util.parsing.json.JSON is deprecated as of Scala 2.13. Needless to say, stay away from this package. It’s fitting for new Scala JSON libraries like circe self identify as “Yet another JSON library for Scala”. os-lib and ujson make it easy to read and write JSON files with Scala.

How to write in a file in Scala with Java?

But in scala we use java library to write in a file because scala does not provide writing to file. So we can import java.io._ package from java library to write in a file because scala standard library does not contains any class to write.

What is OS-lib and ujson in Scala?

os-lib and ujson make it easy to read and write JSON files with Scala. It’s painful to work with JSON and Scala without these libraries. Read this article or Hands on Scala Programming to learn more details about how ujson is implemented and other use cases.

How to create a new file using printwriter in Scala?

To create a new file to write to, we create a new instance of PrintWriter, and pass a new File object to it. Now, to write to the file, we call the method write () on the object we created. d. Finally At this point, nothing is really visible in the file. To see these changes reflect in the file demo1.txt, we need to close it with Scala.


1 Answers

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

val json = JacksMapper.writeValueAsString[MyClass](instance)

... and deserializing

val obj = JacksMapper.readValue[MyClass](json)

(edit)

You can make also writing and reading simple one-liners using FileUtils from commons-io doing

val json = FileUtils readFileToString (file, encoding)

and

FileUtils write (file, json, encoding) 
like image 151
Bruno Grieder Avatar answered Oct 08 '22 22:10

Bruno Grieder