Cristiano Gavião created FELIX-5981:
---------------------------------------
Summary: Include some converter rules to turn easier to deal with
complex json configurations
Key: FELIX-5981
URL: https://issues.apache.org/jira/browse/FELIX-5981
Project: Felix
Issue Type: Improvement
Components: Configurator
Reporter: Cristiano Gavião
When using a json configuration containing a complex type with Configurator
like the one below:
{code:java}
{
"my.pid": {
"port:Integer" : 300,
"an_int_array:int[]" : [2, 3, 4],
"an_Integer_collection:Collection<Integer>" : [2, 3, 4],
"complex": {
"a" : 1,
"b" : "two"
}
}
}
{code}
Then the complex field will become a string:
{code:java}
String complex = "{ \"1\" : 1, \"b\" : \"two\" }"
{code}
In order to convert such json complex string into a DTO, Map or a java bean
then it is necessary a Json Parser and some transforming code.
Since Configurator uses the excellent OSGi Converter and already uses a json
parser then Configurator could provide the necessary Converter Rules that will
turn developer's life easier when working with complex json configurations.
The Converter works in a recursive way so we just need the root json object and
we can convert it and all its inner objects with a single move. For example, in
order to transform a configuration string field into a DTO we just need to:
{code:java}
// get the configuration field from the properties map:
String config = (String) pComponentContext.getProperties()
.get("configuration");
// convert the string into a json object (that used the json parser):
JsonObject configjson = CONVERTER.convert(config).to(JsonObject.class);
// convert the json object into a DTO:
configurationDTO = CONVERTER.convert(configjson).targetAsDTO()
.to(ConfigurationDTO.class);
{code}
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)