Try it yourself with our free Toml Formatter tool — runs entirely in your browser, no signup needed.

How to Parse TOML in Scala

How to parse TOML in Scala

TOML (Tom's Obvious, Minimal Language) is a popular configuration file format used in many applications. As a Scala developer, you may need to parse TOML files to read configuration data or settings. In this guide, we will walk through how to parse TOML in Scala using the toml-scala library.

Quick Example

Here is a minimal example of how to parse a TOML file in Scala:

import com.moandjiezana.toml.Toml

object TomlParser {
  def main(args: Array[String]): Unit = {
    val tomlString = """
      [database]
      server = "localhost"
      port = 5432
    """
    val toml = Toml.read(tomlString)
    val database = toml.getTable("database")
    println(s"Server: ${database.getString("server")}")
    println(s"Port: ${database.getLong("port")}")
  }
}

This code reads a TOML string, parses it, and prints out the values of the server and port fields in the database table.

Step-by-Step Breakdown

Let's walk through the code line by line:

  • import com.moandjiezana.toml.Toml: We import the Toml class from the toml-scala library.
  • val tomlString = """...""": We define a TOML string with a simple configuration.
  • val toml = Toml.read(tomlString): We use the Toml.read method to parse the TOML string into a Toml object.
  • val database = toml.getTable("database"): We get a reference to the database table in the TOML object.
  • println(s"Server: ${database.getString("server")}"): We print out the value of the server field in the database table.
  • println(s"Port: ${database.getLong("port")}"): We print out the value of the port field in the database table.

Handling Edge Cases

Here are a few common edge cases to consider when parsing TOML in Scala:

Empty/null input

If the input TOML string is empty or null, the Toml.read method will throw a NullPointerException. You can handle this case by checking for null or empty input before parsing:

val tomlString = ""
if (tomlString != null && tomlString.nonEmpty) {
  val toml = Toml.read(tomlString)
  // ...
} else {
  println("Error: empty or null input")
}

Invalid input

If the input TOML string is invalid (e.g. syntax error), the Toml.read method will throw a TomlException. You can handle this case by catching the exception:

try {
  val toml = Toml.read(tomlString)
  // ...
} catch {
  case e: TomlException => println(s"Error: invalid input - ${e.getMessage}")
}

Large input

If the input TOML string is very large, parsing it may take a significant amount of time. You can handle this case by using a streaming parser or by parsing the input in chunks:

val tomlString = "...large input..."
val toml = Toml.read(tomlString, chunkSize = 1024)

Unicode/special characters

TOML supports Unicode characters, but some special characters (e.g. \n, \t) may need to be escaped. You can handle this case by using the Toml.read method with the unescape parameter set to true:

val tomlString = "key = \"value\nwith newline\""
val toml = Toml.read(tomlString, unescape = true)

Common Mistakes

Here are a few common mistakes to avoid when parsing TOML in Scala:

Mistake 1: Forgetting to handle null input

// wrong
val toml = Toml.read(tomlString)

// correct
if (tomlString != null && tomlString.nonEmpty) {
  val toml = Toml.read(tomlString)
  // ...
}

Mistake 2: Not handling invalid input

// wrong
val toml = Toml.read(tomlString)

// correct
try {
  val toml = Toml.read(tomlString)
  // ...
} catch {
  case e: TomlException => println(s"Error: invalid input - ${e.getMessage}")
}

Mistake 3: Not using the correct data type

// wrong
val port = database.getString("port")

// correct
val port = database.getLong("port")

Performance Tips

Here are a few performance tips to keep in mind when parsing TOML in Scala:

  • Use the Toml.read method with the chunkSize parameter to parse large input in chunks.
  • Use the Toml.read method with the unescape parameter set to true to handle Unicode characters and special characters.
  • Avoid using the Toml.read method with a large input string, instead use a streaming parser or parse the input in chunks.

FAQ

Q: What is the difference between Toml.read and Toml.parse?

A: Toml.read is used to parse a TOML string into a Toml object, while Toml.parse is used to parse a TOML string into a Map[String, Any].

Q: How do I handle nested tables in TOML?

A: You can access nested tables using the getTable method, for example: val nestedTable = toml.getTable("nested").getTable("table").

Q: Can I use TOML to store binary data?

A: No, TOML is a text-based format and is not suitable for storing binary data.

Q: How do I handle TOML files with multiple tables?

A: You can access multiple tables using the getTable method, for example: val table1 = toml.getTable("table1") and val table2 = toml.getTable("table2").

Q: Can I use TOML with Scala 2.12?

A: Yes, the toml-scala library supports Scala 2.12.

To use the toml-scala library, add the following dependency to your build.sbt file:

libraryDependencies += "com.moandjiezana.toml" %% "toml-scala" % "0.2.0"

Note: This guide assumes you are using Scala 2.12 or later. If you are using an earlier version of Scala, you may need to use a different version of the toml-scala library.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp