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.right.get.noSpaces 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))
syntax.configDecoder for how to map io.circe.Json to com.typesafe.config.Config
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}
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))
circe-config: A Typesafe config wrapper powered by circe.
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).