Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added JsonConfigurationUtil #143

Merged
merged 1 commit into from Apr 12, 2019
Merged

Added JsonConfigurationUtil #143

merged 1 commit into from Apr 12, 2019

Conversation

@urbim
Copy link
Member

@urbim urbim commented Apr 12, 2019

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": "{}"
}
@urbim urbim added the Feature label Apr 12, 2019
@urbim urbim merged commit 7cbb0a7 into master Apr 12, 2019
2 checks passed
2 checks passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details
@urbim urbim deleted the feature/jsonConfigurationUtil branch Apr 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

1 participant