kumuluz / kumuluzee

Lightweight open-source framework for developing microservices using standard Java EE technologies and migrating Java EE to cloud-native architecture.
https://ee.kumuluz.com
MIT License
291 stars 71 forks source link

Added JsonConfigurationUtil #143

Closed urbim closed 5 years ago

urbim commented 5 years ago

Enables retrieval of a part of a configuration tree as JSON-P object.

Example usage:

config.yml:

json-test:
  a: 1
  b: 2
  c:
    - 1
    - 2
    - 3
  d:
    - name: asd
      surname: cde
    - name: fgh
      surname: asd

The following code:

JsonConfigurationUtil jsonConfig = JsonConfigurationUtil.getInstance();
JsonObject fullObject = jsonConfig.getJsonObject("json-test").get();

Produces the following JsonObject:

{
    "a": 1,
    "b": 2,
    "c": [
        1,
        2,
        3
    ],
    "d": [
        {
            "surname": "cde",
            "name": "asd"
        },
        {
            "name": "fgh",
            "surname": "asd"
        }
    ]
}

Since our configuration framework does not support null values, empty objects and empty arrays a builder can be used to configure values which should be mapped to those values.

Example with mappings:

config.yml:

json-test:
  normal: ok
  empty-object: '{}'
  empty-array: '[]'
  null-value: 'null'

The following code:

JsonConfigurationUtil jsonConfig = JsonConfigurationUtil.getBuilder()
                .mapToEmptyArray("[]")
                .mapToEmptyObject("{}")
                .mapToNull("null")
                .build();

JsonObject withMappings = jsonConfig.getJsonObject("json-test").get();

Produces the following JsonObject:

{
    "null-value": null,
    "normal": "ok",
    "empty-array": [
    ],
    "empty-object": {
    }
}

If no mappings were defined the following JsonObject would be produced:

{
    "null-value": "null",
    "normal": "ok",
    "empty-array": "[]",
    "empty-object": "{}"
}