17 February 2009

Scala parsing JSON with scala.util.parsing.json

What I'm Trying To Do

I'm converting our internal corporate data format AXF into JSON and XML so we can publish some of our data on the web.

I don't expect that the AXF to JSON conversion would be of much interest outside my organisation. It suffices to say that it was written in C, and that it is completely general: Any AXF data file can be converted to JSON.

From past experience, it's non-trivial converting from AXF to XML. My thinking is to go from AXF to JSON (easy), and from JSON to XML (hopefully fairly easy).

Two JSON Formats

Forgive me if I'm repeating something that's already well known. There was a requirement from internal developers that the JSON be as compact as possible. To achieve that, I generate JSON in two formats:
  1. Row-major order
  2. Column-major order
Row-major order is much easier for humans to read and comprehend: It's an array of objects.

Column-major order JSON is about half the size of row-major JSON, because the key names are used just once at the beginning of each array of a given data type, rather than within each object as is the case with row-major JSON.

The C based axf2json utility has a -r and -c options to switch between these JSON output styles.

JSON to Scala

Here's the code for reading and parsing a JSON file into Scala:





import scala.io.Source
import scala.util.parsing.json._

object Json2Xml {

def main(args: Array[String]) = {
val fname = "mydata.json"

// Read the JSON file into json variable.
var json: String = ""
for (line <- Source.fromFile(fname).getLines) json += line

// Get parse result Option object back from try/catch block.
val option = try {
JSON.parseFull(json)
} catch {
case ex: Exception => ex.printStackTrace() None
}

// See what we found.
option match {
case None => println("observations JSON invalid")
case Some(elements) => // Turn it into XML.
}

()
}




It would be nice to be able to make a single call to a function and get back the entire ASCII contents of a file. So far as I can see, there's no such function in Scala that does that. There is in Groovy. It would like to not need to loop through the file a line at a time.

No comments:

Post a Comment

Followers