This can cause errors based on the current Java version, because at some point between Java 11 and Java 18, the content-type of a local .json file started coming back as application/json instead of content/unknown:
Java version: 11.0.10
path: C:\Users\bcimino\dev\tqa\target\classes\has_comments.json
content type: content/unknown
parse syntax: CONF
== via ConfigFactory.parseFile(C:\Users\bcimino\dev\tqa\target\classes\has_comments.json) ==
{
# C:\Users\bcimino\dev\tqa\target\classes\has_comments.json: 3
# this is a comment
"a" : 5
}
== via ConfigFactory.parseResources(has_comments.json) ==
{
# has_comments.json @ file:/C:/Users/bcimino/dev/tqa/target/classes/has_comments.json: 3
# this is a comment
"a" : 5
}
Java 18
Java version: 18.0.1
path: C:\Users\bcimino\dev\tqa\target\classes\has_comments.json
content type: application/json
parse syntax: CONF
== via ConfigFactory.parseFile(C:\Users\bcimino\dev\tqa\target\classes\has_comments.json) ==
{
# C:\Users\bcimino\dev\tqa\target\classes\has_comments.json: 3
# this is a comment
"a" : 5
}
== via ConfigFactory.parseResources(has_comments.json) ==
com.typesafe.config.ConfigException$Parse: has_comments.json @ file:/C:/Users/bcimino/dev/tqa/target/classes/has_comments.json: 2: Token not allowed in valid JSON: '//'
Workaround
This can be worked around by loading the file "by hand" into a String and using ConfigFactory.parseString().
If Java's
URLConnection.getContentType()
returns one of theapplication/json
,application/hocon
, ortext/x-java-properties
, then that value will override the providedConfigParseOptions.syntax
:https://github.com/lightbend/config/blob/4458ea947a7a2a668bb811a122455f1f05975172/config/src/main/java/com/typesafe/config/impl/Parseable.java#L236-L244
This can cause errors based on the current Java version, because at some point between Java 11 and Java 18, the
content-type
of a local.json
file started coming back asapplication/json
instead ofcontent/unknown
:Example
Java 11
Java 18
Workaround
This can be worked around by loading the file "by hand" into a
String
and usingConfigFactory.parseString()
.