hocon
is a superset of JSON file type, So JSON syntax almost works in the hocon configuration file.
So Every syntax of JSON objects works with hocon configuration.
Hocon Configuration Syntax
Configuration can be defined with key
and value
.
Here is a simple configuration defined in the hocon .conf file
Here is a syntax
key:value
key=value
the key is a string value
value can be string, boolean, array or map, or number
key and value are separated by a colon(:
) or equal (=
) symbol
The same syntax can be declared and rewritten as follows.
{
key:value
key=value
}
Root element braces({}
) are omitted.
The below configuration is also valid.
key:value
The same can be rewritten in JSON.
{
"key":"value"
}
Let’s see some examples of different keys and values.
- String value example in hocon
{
keystring:"keyvalue"
keystring1:"keyvalue1"
}
- Number value example in hocon
{
keyinteger:12
keyfloat:11.0
}
- boolean value example
{
key boolean:true
keyboolean1:false
keyboolean2:yes
keyboolean3:no
}
- Array values example
The array contains a group of values enclosed in square brackets
[]
. The values in the array can be strings, booleans, integers
emptyArray: []
intArray: [11, 12, 13]
stringArray: ["one","two","three"]
booleanArray: [true,false,yes,no]
The object can also be declared as values in hocon configuration
dev: {
hostname: dev.site.com,
username: username,
password: password,
}
Comments
Comments are useful text to describe each line of text. This will be ignored by the processor or compiler during reading.
It supports inline comments and these always stats with ‘//’ and ‘#’
// this is an inline comment
\# this is another inline comment
Includes
This is one of the important features which is not available in JSON. This is used to include multiple files
let’s declare database.conf which contains common properties database.conf
database.port=5712
database.ssenabled=true
dev.conf
include "database.conf"
hostname: dev.site.com,
username: username,
password: password,
Substitution variable
Hocon supports Substitution variables using ${}
syntax
It replaces the variable with a variable as seen below
devprofile {
hostname: dev
}
devpath: ${devprofile.hostname}:8080
Items | Status | Example |
---|---|---|
Comments | ✅ | # |
Omit root braces | ✅ | |
Key-value separator | ✅ | |
Commas | ✅ | |
Whitespace | ✅ | |
Duplicate keys and object merging | ✅ | |
Unquoted strings | ✅ | |
Multi-line strings | ✅ | |
String value concatenation | ✅ | |
Array concatenation | ✅ | |
Object concatenation | ✅ | |
Arrays without commas | ✅ | |
Path expressions | ✅ | |
Paths as keys | ✅ | |
Substitutions | ✅ | |
Self-referential substitutions | ✅ | |
The += separator | ✅ | |
Includes | ✅ | |
Include semantics: merging | ✅ | |
Include semantics: substitution | ✅ | |
Include semantics: missing files | ❌ | |
Include semantics: file formats and extensions | ❌ | |
Include semantics: locating resources | ❌ | |
Include semantics: preventing cycles | ❌ | |
Conversion of numerically-index objects to arrays | ✅ |
API Recommendations | Status |
---|---|
Conversion of numerically-index objects to arrays | ❌ |
Automatic type conversions | ❌ |
Units format | ❌ |
Duration format | ❌ |
Size in bytes format | ❌ |
Config object merging and file merging | ❌ |
Java properties mapping | ❌ |