Suppose I have a case scala class with serialization capability in json (using json4s or another library):
case class Weather(zip : String, temp : Double, isRaining : Boolean)
If I use the HOCON configuration file :
allWeather {
BeverlyHills {
zip : 90210
temp : 75.0
isRaining : false
}
Cambridge {
zip : 10013
temp : 32.0
isRainging : true
}
}
Is there a way to use config config to automatically create an object Weather?
I'm looking for something like a form
val config : Config = ConfigFactory.parseFile(new java.io.File("weather.conf"))
val bevHills : Weather = config.getObject("allWeather.BeverlyHills").as[Weather]
The solution could use the fact that the value referenced "allWeather.BeverlyHills"is "blob" json.
I obviously could write my own parser:
def configToWeather(config : Config) =
Weather(config.getString("zip"),
config.getDouble("temp"),
config.getBoolean("isRaining"))
val bevHills = configToWeather(config.getConfig("allWeather.BeverlyHills"))
But this seems inelegant, since any change in the definition of weather will also require a change in configToWeather.
Thank you in advance for your feedback and response.