package config
circe-config: A Typesafe config wrapper powered by circe.
- Source
- package.scala
scala> import com.typesafe.config.ConfigFactory scala> import io.circe.generic.auto._ scala> import io.circe.config.syntax._ scala> case class ServerSettings(host: String, port: Int, ssl: Option[String]) scala> case class HttpSettings(server: ServerSettings, version: Double) scala> case class AppSettings(http: HttpSettings) scala> val config = ConfigFactory.parseString("http { version = 1.1, server { host = localhost, port = 8080 } }") scala> config.as[ServerSettings]("http.server") res0: Either[io.circe.Error, ServerSettings] = Right(ServerSettings(localhost,8080,None)) scala> config.as[HttpSettings]("http") res1: Either[io.circe.Error, HttpSettings] = Right(HttpSettings(ServerSettings(localhost,8080,None),1.1)) scala> config.as[AppSettings] res2: Either[io.circe.Error, AppSettings] = Right(AppSettings(HttpSettings(ServerSettings(localhost,8080,None),1.1))) scala> import cats.effect.IO scala> config.asF[IO, AppSettings] res3: IO[AppSettings] = IO(AppSettings(HttpSettings(ServerSettings(localhost,8080,None),1.1))) scala> import io.circe.config.parser scala> val settings = parser.decodeF[IO, AppSettings]() scala> settings.unsafeRunSync() res4: AppSettings = AppSettings(HttpSettings(ServerSettings(localhost,8080,None),1.1))
- Note
Limitations for numerical types: Typesafe config uses Java's int, long and double types to represent numbers. In some cases, double values may be represented internally as long after a roundtrip since the HOCON formatting is not stable. Also, precision may be lost when converting from circe's JsonNumber to Typesafe config's number representation (as can be seen in the test for the printer laws).
- Alphabetic
- By Inheritance
- config
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Value Members
- object parser extends Parser
Utilities for parsing com.typesafe.config.Config sources to io.circe.Json as well as decoding to a specific type.
Utilities for parsing com.typesafe.config.Config sources to io.circe.Json as well as decoding to a specific type.
If you are working in something like cats.effect.IO, or some other type
F[_]
that provides a cats.ApplicativeError, there're also decoders for loading such types.scala> import com.typesafe.config.ConfigFactory scala> import io.circe.config.parser scala> val config = ConfigFactory.parseString("server { host = localhost, port = 8080 }") scala> val json: Either[io.circe.ParsingFailure, io.circe.Json] = parser.parse(config) scala> json.map(_.noSpaces).getOrElse("Parse failure") res0: String = {"server":{"port":8080,"host":"localhost"}} scala> import io.circe.generic.auto._ scala> case class ServerSettings(host: String, port: Int) scala> case class AppSettings(server: ServerSettings) scala> parser.decode[AppSettings](config) res1: Either[io.circe.Error, AppSettings] = Right(AppSettings(ServerSettings(localhost,8080))) scala> parser.decodePath[ServerSettings](config, "server") res2: Either[io.circe.Error, ServerSettings] = Right(ServerSettings(localhost,8080)) scala> import cats.effect.IO scala> parser.decodePathF[IO, ServerSettings](config, "server") res3: cats.effect.IO[ServerSettings] = IO(ServerSettings(localhost,8080))
- See also
syntax.configDecoder for how to map io.circe.Json to com.typesafe.config.Config
Example: - object printer
Print io.circe.Json to a Typesafe Config string.
Print io.circe.Json to a Typesafe Config string.
scala> import io.circe.Json scala> import io.circe.config.printer scala> val options = printer.DefaultOptions.setFormatted(false) scala> val json = Json.obj("server" -> Json.obj("host" -> Json.fromString("localhost"), "port" -> Json.fromInt(8080))) scala> printer.print(json, options) res0: String = server{host=localhost,port=8080}
Example: - object syntax
Implicits for decoding Typesafe Config values and instances using circe decoders.
Implicits for decoding Typesafe Config values and instances using circe decoders.
In addition to syntax.durationDecoder and syntax.memorySizeDecoder for reading Typesafe Config specific value formats, this module also provides syntax.CirceConfigOps for decoding loaded configurations.
scala> import io.circe.generic.auto._ scala> import io.circe.config.syntax._ scala> import scala.concurrent.duration.FiniteDuration scala> case class ServerSettings(port: Int, host: String, timeout: FiniteDuration) scala> val config = com.typesafe.config.ConfigFactory.parseString("port = 7357, host = localhost, timeout = 5 s") scala> config.as[ServerSettings] res0: Either[io.circe.Error, ServerSettings] = Right(ServerSettings(7357,localhost,5 seconds))
Example: