This commit is contained in:
Alexey Pomogaev 2017-10-06 12:18:53 +00:00 committed by GitHub
commit 28b51a0a4f
5 changed files with 23 additions and 8 deletions

View File

@ -711,10 +711,11 @@ public final class ConfigFactory {
* with all known extensions and merges whatever is found.
*
* <p>
* In the current implementation, the extension ".conf" forces
* In the current implementation, the extension ".conf" or ".hocon" forces
* {@link ConfigSyntax#CONF}, ".json" forces {@link ConfigSyntax#JSON}, and
* ".properties" forces {@link ConfigSyntax#PROPERTIES}. When merging files,
* ".conf" falls back to ".json" falls back to ".properties".
* ".conf" falls back to ".hocon" fall back to ".json" falls back to
* ".properties".
*
* <p>
* Future versions of the implementation may add additional syntaxes or
@ -821,8 +822,9 @@ public final class ConfigFactory {
* classpath order. To keep it simple, the lists are simply concatenated,
* with the same syntax priorities as
* {@link #parseFileAnySyntax(File,ConfigParseOptions) parseFileAnySyntax()}
* - all ".conf" resources are ahead of all ".json" resources which are
* ahead of all ".properties" resources.
* - all ".conf" resources are ahead of all ".hocon" resources which are
* ahead of all ".json" resources which are ahead of all ".properties"
* resources.
*
* @param klass
* class which determines the <code>ClassLoader</code> and the

View File

@ -6,7 +6,7 @@ package com.typesafe.config;
/**
* The syntax of a character stream (<a href="http://json.org">JSON</a>, <a
* href="https://github.com/typesafehub/config/blob/master/HOCON.md">HOCON</a>
* aka ".conf", or <a href=
* aka ".conf" and ".hocon", or <a href=
* "http://download.oracle.com/javase/7/docs/api/java/util/Properties.html#load%28java.io.Reader%29"
* >Java properties</a>).
*

View File

@ -327,7 +327,7 @@ public abstract class Parseable implements ConfigParseable {
private static ConfigSyntax syntaxFromExtension(String name) {
if (name.endsWith(".json"))
return ConfigSyntax.JSON;
else if (name.endsWith(".conf"))
else if (name.endsWith(".conf") || name.endsWith(".hocon"))
return ConfigSyntax.CONF;
else if (name.endsWith(".properties"))
return ConfigSyntax.PROPERTIES;

View File

@ -166,12 +166,14 @@ class SimpleIncluder implements FullIncluder {
// loading app.{conf,json,properties} from the filesystem.
static ConfigObject fromBasename(NameSource source, String name, ConfigParseOptions options) {
ConfigObject obj;
if (name.endsWith(".conf") || name.endsWith(".json") || name.endsWith(".properties")) {
if (name.endsWith(".conf") || name.endsWith(".hocon") || name.endsWith(".json")
|| name.endsWith(".properties")) {
ConfigParseable p = source.nameToParseable(name, options);
obj = p.parse(p.options().setAllowMissing(options.getAllowMissing()));
} else {
ConfigParseable confHandle = source.nameToParseable(name + ".conf", options);
ConfigParseable hoconHandle = source.nameToParseable(name + ".hocon", options);
ConfigParseable jsonHandle = source.nameToParseable(name + ".json", options);
ConfigParseable propsHandle = source.nameToParseable(name + ".properties", options);
boolean gotSomething = false;
@ -190,6 +192,16 @@ class SimpleIncluder implements FullIncluder {
}
}
if (syntax == null || syntax == ConfigSyntax.CONF) {
try {
obj = hoconHandle.parse(hoconHandle.options().setAllowMissing(false)
.setSyntax(ConfigSyntax.CONF));
gotSomething = true;
} catch (ConfigException.IO e) {
failMessages.add(e.getMessage());
}
}
if (syntax == null || syntax == ConfigSyntax.JSON) {
try {
ConfigObject parsed = jsonHandle.parse(jsonHandle.options()

View File

@ -24,7 +24,8 @@ class EquivalentsTest extends TestUtils {
private def filesForEquiv(equiv: File) = {
val rawFiles = equiv.listFiles()
val files = rawFiles.filter({ f => f.getName().endsWith(".json") || f.getName().endsWith(".conf") })
val files = rawFiles.filter({ f => f.getName().endsWith(".json") || f.getName().endsWith(".conf")
|| f.getName().endsWith(".hocon") })
files
}