blob: a7bb7e57429f9dbeffa6cec63e0b952f11f45346 [file] [log] [blame]
:jbake-type: page
:jbake-status: published
= Apache Tamaya - Extension: Builder
toc::[]
[[JSON]]
== Tamaya JSON (Extension Module)
Tamaya _JSON_ is an extension module. Refer to the link:../extensions.html[extensions documentation] for further details.
=== What functionality this module provides ?
Tamaya _JSON_ provides support for reading configuration using JSON
format:
[source, json]
-----------------------------------------------
{
"a.b"{
"key1": "blabla",
"key2": true,
}
}
-----------------------------------------------
Hereby the hierarchical structure of the JSON document will be mapped to a
flat key-value pairs of type `String`, e.g. the bove will be mapped to
[source, properties]
-----------------------------------------------
a.b.key1=blabla
a.b.key2=true
-----------------------------------------------
This extension uses SPI defined by the +tamaya.formats+ extension module.
=== Compatibility
The module is based on Java 8.
=== Installation
To use the JSON extension module you only must add the corresponding dependency to your module:
[source, xml, subs=attributes+]
-----------------------------------------------
<dependency>
<groupId>org.apache.tamaya.ext</groupId>
<artifactId>tamaya-json</artifactId>
<version>{tamaya_version}</version>
</dependency>
-----------------------------------------------
This extension also transitively requires the +tamaya.formats+ module.
=== Reading configuration in JSON
For reading JSON based onfiguration most easily a +JSONFormat+ can be
used:
[source, java]
-----------------------------------------------
ConfigurationData dataRead = ConfigurationFormats.getInstance().readConfig(
getClassLoader().getResource("myFileConfig.json"), new JSONFormat()));
-----------------------------------------------
Or, if you are fine with the _default_ mapping you can directly create a
+PropertySource+ using the _formats_ API (this works since this module
registers the _json_ format automatically using the `ServiceContext`):
[source, java]
-----------------------------------------------
PropertySource ps = ConfigurationFormats.getInstance().createPropertySource(
getClassLoader().getResource("myFileConfig.json"));
-----------------------------------------------