【Scala】Scala ~ TOML ~

■ はじめに

https://dk521123.hatenablog.com/entry/2023/03/16/012034
https://dk521123.hatenablog.com/entry/2023/04/04/000733

の続き。

今回は、Scala で TOMLファイルの扱いについてまとめる

TOML
https://dk521123.hatenablog.com/entry/2023/04/25/103533

目次

【1】ライブラリ
 1)toml-scala
 2)stoml
 3)NightConfig
【2】サンプル
 例1:NightConfig

【1】ライブラリ

1)toml-scala

https://index.scala-lang.org/sparsetech/toml-scala
https://mvnrepository.com/artifact/tech.sparse/toml-scala

2)stoml

https://index.scala-lang.org/jvican/stoml
https://github.com/jvican/stoml

3)NightConfig

https://github.com/TheElectronWill/night-config
https://github.com/TheElectronWill/night-config/wiki/
サポート

JSON
YAML v1.1
TOML v1.0
HOCON

build.sbt
https://mvnrepository.com/artifact/com.electronwill.night-config/toml

    libraryDependencies ++= Seq (
      "com.electronwill.night-config" % "toml" % "3.6.6",

【2】サンプル

* 読み込むTOMLファイルは、以下の関連記事を参照のこと

https://dk521123.hatenablog.com/entry/2023/04/25/103533

例1:NightConfig

import com.electronwill.nightconfig.core.file.FileConfig

object  Hello {
  def main(args: Array[String]): Unit = {
    val config = FileConfig.of("config.toml")
    config.load()
    println("Config: " + config)
    println("title: " + config.get("title"))
    val server = config.getOptional("database.server")
    println("server: " + server.get())
    val data  = config.getOptional("clients.data")
    println("data: " + data.get())
    config.close()
    println("Done!")
  }

出力結果

Config: WriteAsyncFileConfig:SimpleCommentedConfig:{owner=SimpleCommentedConfig:{dob=1979-05-27T07:32-08:00, name=Tom Preston-Werner}, database=SimpleCommentedConfig:{server=127.0.0.1, connection_max=5000, ports=[8080, 8081, 8082], enabled=true}, title=TOML Example}
title: TOML Example
server: 127.0.0.1
data: [[gamma, delta], [1, 2]]
Done!

関連記事

Scala ~ 環境構築編 ~
https://dk521123.hatenablog.com/entry/2023/03/10/193805
Scala ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/03/12/184331
ScalaYAML
https://dk521123.hatenablog.com/entry/2023/03/16/012034
ScalaJSON
https://dk521123.hatenablog.com/entry/2023/04/04/000733
Scala ~ Resources ~
https://dk521123.hatenablog.com/entry/2024/05/08/233755
TOML
https://dk521123.hatenablog.com/entry/2023/04/25/103533