Class JsonSource

  • All Implemented Interfaces:
    ConfigSource

    public class JsonSource
    extends Object
    implements ConfigSource
    Configuration source that reads from JSON files.

    This source loads configuration from JSON files using Gson for parsing. Supports both primitive values and nested objects.

    Supports environment-specific configuration sections. When an environment is specified, only configuration from that section is loaded. For example:

     {
       "uat": {
         "database.host": "uat-db.example.com",
         "api.url": "https://uat-api.example.com"
       },
       "prod": {
         "database.host": "prod-db.example.com",
         "api.url": "https://prod-api.example.com"
       }
     }
     
    Since:
    1.0
    Author:
    Bharat Kumar Malviya, GitHub: github.com/imBharatMalviya
    See Also:
    ConfigSource
    • Constructor Detail

      • JsonSource

        public JsonSource​(String filePath)
        Creates a new JsonSource from the given file path (supports both filesystem and classpath). Loads all top-level keys (not nested in environment sections).
        Parameters:
        filePath - path to the JSON file (filesystem or classpath)
        Throws:
        IllegalStateException - if the file cannot be loaded
      • JsonSource

        public JsonSource​(String filePath,
                          String environment)
        Creates a new JsonSource from the given file path for a specific environment (supports both filesystem and classpath). When an environment is specified, only configuration from that section is loaded.
        Parameters:
        filePath - path to the JSON file (filesystem or classpath)
        environment - the environment section to load (e.g., "uat", "prod"), or null for all top-level keys
        Throws:
        IllegalStateException - if the file cannot be loaded or the environment section doesn't exist
      • JsonSource

        public JsonSource​(Path file)
        Creates a new JsonSource from the given file. Loads all top-level keys (not nested in environment sections).
        Parameters:
        file - path to the JSON file
        Throws:
        IllegalStateException - if the file cannot be loaded
      • JsonSource

        public JsonSource​(Path file,
                          String environment)
        Creates a new JsonSource from the given file for a specific environment. When an environment is specified, only configuration from that section is loaded.
        Parameters:
        file - path to the JSON file
        environment - the environment section to load (e.g., "env1", "env2"), or null for all top-level keys
        Throws:
        IllegalStateException - if the file cannot be loaded or the environment section doesn't exist
    • Method Detail

      • getName

        public String getName()
        Description copied from interface: ConfigSource
        Returns the name of this configuration source.
        Specified by:
        getName in interface ConfigSource
        Returns:
        the source name, never null
      • get

        public Optional<String> get​(String key)
        Description copied from interface: ConfigSource
        Retrieves a configuration value for the given key.

        If the key is not found in this source, an empty Optional should be returned. This allows the ConfNG system to try other sources in the resolution chain.

        Specified by:
        get in interface ConfigSource
        Parameters:
        key - the configuration key to look up
        Returns:
        an Optional containing the value if found, empty otherwise
      • getPriority

        public int getPriority()
        Description copied from interface: ConfigSource
        Returns the priority of this configuration source.

        Sources with higher priority values are consulted first in the resolution chain. Default priorities:

        • 100+ - Secret managers and secure sources
        • 80-99 - TestNG parameters and test-specific sources
        • 60-79 - Environment variables
        • 40-59 - System properties
        • 20-39 - Configuration files (JSON, Properties)
        • 0-19 - Default and fallback sources
        Specified by:
        getPriority in interface ConfigSource
        Returns:
        the priority value, higher values have higher priority