Class JsonConfigReader

All Implemented Interfaces:
org.pipservices3.commons.config.IConfigurable, IConfigReader

public class JsonConfigReader extends FileConfigReader
Config reader that reads configuration from JSON file.

The reader supports parameterization using Handlebars template engine.

### Configuration parameters ###

  • path: path to configuration file
  • parameters: this entire section is used as template parameters
  • ...

### Example ###

 
 ======== config.json ======
 { "key1": "{{KEY1_VALUE}}", "key2": "{{KEY2_VALUE}}" }
 ===========================
 
 JsonConfigReader configReader = new JsonConfigReader("config.json");
 
 ConfigParams parameters = ConfigParams.fromTuples("KEY1_VALUE", 123, "KEY2_VALUE", "ABC");
 configReader.readConfig("123", parameters);
 
 
See Also:
  • Field Summary

    Fields inherited from class org.pipservices3.components.config.FileConfigReader

    _path
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new instance of the config reader.
    Creates a new instance of the config reader.
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.pipservices3.commons.config.ConfigParams
    readConfig(String correlationId, String path, org.pipservices3.commons.config.ConfigParams parameters)
    Reads configuration from a file, parameterize it with given values and returns a new ConfigParams object.
    org.pipservices3.commons.config.ConfigParams
    readConfig(String correlationId, org.pipservices3.commons.config.ConfigParams parameters)
    Reads configuration and parameterize it with given values.
    static Object
    readObject(String correlationId, String path, org.pipservices3.commons.config.ConfigParams parameters)
    Reads configuration file, parameterizes its content and converts it into JSON object.
    readObject(String correlationId, org.pipservices3.commons.config.ConfigParams parameters)
    Reads configuration file, parameterizes its content and converts it into JSON object.

    Methods inherited from class org.pipservices3.components.config.FileConfigReader

    configure, getPath, setPath

    Methods inherited from class org.pipservices3.components.config.ConfigReader

    addChangeListener, parameterize, removeChangeListener

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JsonConfigReader

      public JsonConfigReader()
      Creates a new instance of the config reader.
    • JsonConfigReader

      public JsonConfigReader(String path)
      Creates a new instance of the config reader.
      Parameters:
      path - (optional) a path to configuration file.
  • Method Details

    • readObject

      public Object readObject(String correlationId, org.pipservices3.commons.config.ConfigParams parameters) throws org.pipservices3.commons.errors.ApplicationException
      Reads configuration file, parameterizes its content and converts it into JSON object.
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      parameters - values to parameters the configuration.
      Returns:
      a JSON object with configuration.
      Throws:
      org.pipservices3.commons.errors.ApplicationException - when error occured.
    • readConfig

      public org.pipservices3.commons.config.ConfigParams readConfig(String correlationId, org.pipservices3.commons.config.ConfigParams parameters) throws org.pipservices3.commons.errors.ApplicationException
      Reads configuration and parameterize it with given values.
      Specified by:
      readConfig in interface IConfigReader
      Specified by:
      readConfig in class ConfigReader
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      parameters - values to parameters the configuration
      Returns:
      ConfigParams configuration.
      Throws:
      org.pipservices3.commons.errors.ApplicationException - when error occured.
    • readObject

      public static Object readObject(String correlationId, String path, org.pipservices3.commons.config.ConfigParams parameters) throws org.pipservices3.commons.errors.ApplicationException
      Reads configuration file, parameterizes its content and converts it into JSON object.
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      path - a path to configuration file.
      parameters - values to parameters the configuration.
      Returns:
      a JSON object with configuration.
      Throws:
      org.pipservices3.commons.errors.ApplicationException - when error occured.
    • readConfig

      public static org.pipservices3.commons.config.ConfigParams readConfig(String correlationId, String path, org.pipservices3.commons.config.ConfigParams parameters) throws org.pipservices3.commons.errors.ApplicationException
      Reads configuration from a file, parameterize it with given values and returns a new ConfigParams object.
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      path - a path to configuration file.
      parameters - values to parameters the configuration.
      Returns:
      ConfigParams configuration.
      Throws:
      org.pipservices3.commons.errors.ApplicationException - when error occured.