I have successfully used the include directive with HOCON before. However, my current project requires that our config files be defined in JSON for client specified reasons. This is fine, but I also need to factor out common elements of these configs into separate 'base' config files that contain the common definitions.
I have tried the syntax
{ "include": "some-common-file.json"}
But this did not work out for me.
A sample program is shown below. The result of looking at the variable 'config' in the debugger showed that the key 'paste' had the expected value 'monkey'.
However, there was also a key 'include' with the value of 'foo.json'. The contents of foo.json were not brought in.
There must be some way to do this (I hope!)
If you can help shed light on 'how to', I'd be most grateful !
Sample Program
import com.typesafe.config.{Config, ConfigBeanFactory, ConfigFactory}
import com.typesafe.scalalogging.LazyLogging
import org.testng.annotations.Test
class ConfigTest extends LazyLogging {
@Test def testJson(): Unit = {
val config: Config = ConfigFactory.load("bar.json")
System.out.println("config:" + config)
}
}
// bar.json
{
"include": "foo.json",
"paste": "monkey"
}
// foo.json
{"testRules":
{
"foo": "Steve",
"bar": 30
}
}
Note: i tried one of the suggested answers -- much appreciated -- but it didn't do the trick. Namely, this approach:
#include "foo.json",
recommended by @newt -- for me this resulted in the following:
bar.json @ file:/project/foo/build/resources/test/bar.json: 2: Expecting close brace } or a field name here, got '#' (Reserved character '#' is not allowed outside quotes)
com.typesafe.config.ConfigException$Parse: bar.json @ file:/project/foo/build/resources/test/bar.json: 2: Expecting close brace } or a field name here, got '#' (Reserved character '#' is not allowed outside quotes)
at com.typesafe.config.impl.ConfigDocumentParser$ParseContext.parseError(ConfigDocumentParser.java:201)
Note2: This syntax:
{ "include": "some-common-file.json"}
resulted in this error:
Token not allowed in valid JSON: 'include'
Thanks to newt for the revision to the original answer, but I think the documentation referenced in the revised answer is for HOCON, not for Json. At this point, I'd give my left leg for a good working example, and I will up the bounty.