OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | Friends | List of all members
ossim::JsonConfig Class Reference

Base class for maintaining parameters affecting the runtime configuration of OSSIM executables. More...

#include <JsonConfig.h>

Inheritance diagram for ossim::JsonConfig:
ossim::JsonInterface ATP::AtpConfig ossimDemToolConfig

Public Member Functions

 JsonConfig ()
 Default Ctor loads all default .json files in the share/ossim system dir. More...
 
 JsonConfig (const ossimFilename &configFile)
 
virtual ~JsonConfig ()
 Destructor. More...
 
bool open (const ossimFilename &configFile)
 Opens and parses JSON file. The "parameters" keyword is expected in the root node. More...
 
virtual void loadJSON (const Json::Value &params_json_node)
 Reads the params controlling the process from the JSON node named "parameters". More...
 
virtual void saveJSON (Json::Value &params_json_node) const
 Reads the params controlling the process from the JSON node named "parameters". More...
 
JsonParamgetParameter (const char *paramName)
 Returns a parameter (might be a null parameter if paramName not found in the configuration. More...
 
void setParameter (const JsonParam &p)
 Adds parameter to the configuration. More...
 
bool diagnosticLevel (unsigned int level) const
 Convenience method returns TRUE if the currently set diagnostic level is <= level. More...
 
bool paramExists (const char *paramName) const
 
- Public Member Functions inherited from ossim::JsonInterface
 JsonInterface ()
 
virtual ~JsonInterface ()
 

Protected Member Functions

 JsonConfig (const JsonConfig &)
 
bool getBoolValue (bool &rtn_val, const std::string &json_value) const
 

Protected Attributes

std::map< std::string, JsonParamm_paramsMap
 

Static Protected Attributes

static JsonParam s_nullParam
 

Friends

std::ostream & operator<< (std::ostream &out, const JsonConfig &obj)
 Outputs JSON to output stream provided. More...
 

Detailed Description

Base class for maintaining parameters affecting the runtime configuration of OSSIM executables.

The state is imported and exported via JSON. There are default configuration files that should be part of the install, that are accessed by this class. Custom settings can also be loaded.

There are two functionally equivalent forms for specifying parameters: long and short. Parameters are initially declared via the long form with descriptions and default values. These values must be supplied in default JSON files as part of the OSSIM install.

Once the parameters are declared via the long form, the short form can be used to supply runtime overrides.

The long form format is

{ "parameters": [ { "name": "<param_name>", "descr": "<param description", "type": "string"|"float"|"uint"|"int"|"bool", "value":

}, ... ] }

The short form is:

{ "parameters": [ "<param_name>":

, ... ] }

The short form parameter is only accepted if it has previously been loaded via the long form so that the data type is known.

Parameters are usually accessed knowing the data type ahead of time. For example, a string parameter is accessed as:

string paramVal = jsonConfig.getParameter("param_name").asSTring();

If the parameter is not found, the special null-parameter is returned from getParameter(), and casting to a tye will return 0, false, or empty string.

Definition at line 64 of file JsonConfig.h.

Constructor & Destructor Documentation

◆ JsonConfig() [1/3]

ossim::JsonConfig::JsonConfig ( )

Default Ctor loads all default .json files in the share/ossim system dir.

Definition at line 309 of file JsonConfig.cpp.

References ossimDirectory::findAllFilesThatMatch(), ossimPreferences::instance(), ossimFilename::isDir(), m_paramsMap, ossimDirectory::open(), open(), ossimNotify(), ossimNotifyLevel_WARN, and ossimException::what().

310 {
311  // This ctor could eventually curl a spring config server for the param JSON. For now it
312  // is reading the installed share/ossim system directory for config JSON files.
313 
314  // The previous parameters list is cleared first for a fresh start:
315  m_paramsMap.clear();
316  ossimFilename configFilename;
317  try
318  {
319  // First establish the directory location of the default config files:
321  preferencesKWL().findKey( std::string( "ossim_share_directory" )));
322  if (!shareName.isDir())
323  throw ossimException("Nonexistent share drive provided for config files.");
324 
325  // Fetch all JSON files:
326  ossimDirectory shareDir;
327  if (!shareDir.open(shareName))
328  throw ossimException("Share drive provided for config files is not readable.");
329  std::vector<ossimFilename> jsonFiles;
330  shareDir.findAllFilesThatMatch(jsonFiles, ".*\\.json");
331 
332  // Process those that contain the "parameters" JSON node:
333  for (unsigned int i=0; i<jsonFiles.size(); i++)
334  {
335  configFilename = jsonFiles[i];
336  if (!open(configFilename))
337  throw ossimException("Bad file open or parse.");
338  }
339 
340  }
341  catch (ossimException& e)
342  {
343  ossimNotify(ossimNotifyLevel_WARN)<<"JsonConfig::readConfig(): Could not open/parse "
344  "config file at <"<< configFilename << ">. Error: "<<e.what()<<endl;
345  }
346 }
void findAllFilesThatMatch(std::vector< ossimFilename > &result, const ossimString &regularExpressionPattern, int flags=OSSIM_DIR_DEFAULT)
std::map< std::string, JsonParam > m_paramsMap
Definition: JsonConfig.h:103
virtual const char * what() const
Returns the error message.
bool open(const ossimFilename &configFile)
Opens and parses JSON file. The "parameters" keyword is expected in the root node.
Definition: JsonConfig.cpp:359
static ossimPreferences * instance()
bool open(const ossimFilename &dir)
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)

◆ JsonConfig() [2/3]

ossim::JsonConfig::JsonConfig ( const ossimFilename configFile)

Definition at line 348 of file JsonConfig.cpp.

References open().

349 {
350  if (!open(configFile))
351  throw ossimException("Bad file open or parse.");
352 }
bool open(const ossimFilename &configFile)
Opens and parses JSON file. The "parameters" keyword is expected in the root node.
Definition: JsonConfig.cpp:359

◆ ~JsonConfig()

ossim::JsonConfig::~JsonConfig ( )
virtual

Destructor.

Definition at line 354 of file JsonConfig.cpp.

References m_paramsMap.

355 {
356  m_paramsMap.clear();
357 }
std::map< std::string, JsonParam > m_paramsMap
Definition: JsonConfig.h:103

◆ JsonConfig() [3/3]

ossim::JsonConfig::JsonConfig ( const JsonConfig )
inlineprotected

Definition at line 99 of file JsonConfig.h.

99 {}

Member Function Documentation

◆ diagnosticLevel()

bool ossim::JsonConfig::diagnosticLevel ( unsigned int  level) const

Convenience method returns TRUE if the currently set diagnostic level is <= level.

Definition at line 461 of file JsonConfig.cpp.

References m_paramsMap.

Referenced by ATP::AtpGenerator::constructChain(), ATP::ossimCorrelationSource::correlate(), ATP::AtpTileSource::filterPoints(), ATP::AtpTileSource::filterWithoutParallax(), ATP::AtpTileSource::filterWithParallax(), ATP::AtpGenerator::generateTiePointList(), ATP::ossimCorrelationSource::getTile(), ATP::ossimDescriptorSource::getTile(), ATP::AtpGenerator::initialize(), ATP::AtpGenerator::layoutSearchTileRects(), ATP::ossimAtpTool::loadJSON(), main(), and ATP::ossimCorrelationSource::OpenCVCorrelation().

462 {
463  map<string, JsonParam>::const_iterator i = m_paramsMap.find(string("diagnosticLevel"));
464  if (i != m_paramsMap.end())
465  {
466  unsigned int levelSetting = i->second.asUint();
467  return (level <= levelSetting);
468  }
469  return false;
470 }
std::map< std::string, JsonParam > m_paramsMap
Definition: JsonConfig.h:103

◆ getBoolValue()

bool ossim::JsonConfig::getBoolValue ( bool &  rtn_val,
const std::string &  json_value 
) const
protected

◆ getParameter()

JsonParam & ossim::JsonConfig::getParameter ( const char *  paramName)

◆ loadJSON()

void ossim::JsonConfig::loadJSON ( const Json::Value &  params_json_node)
virtual

Reads the params controlling the process from the JSON node named "parameters".

Implements ossim::JsonInterface.

Definition at line 403 of file JsonConfig.cpp.

References ossimString::contains(), ossim::JsonParam::descr(), ossimString::empty(), getParameter(), ossim::JsonParam::loadJSON(), ossim::JsonParam::name(), ossimNotify(), ossimNotifyLevel_WARN, ossim::JsonParam::saveJSON(), setParameter(), and ossimString::string().

Referenced by ATP::ossimAtpTool::loadJSON(), main(), and open().

404 {
405  // Support two forms: long (with full param descriptions and types), or short (just name: value)
406  if (json_node.isArray())
407  {
408  // Long form:
409  for (const auto &i : json_node)
410  {
411  JsonParam p;
412  if (p.loadJSON(i))
413  setParameter(p);
414  }
415  }
416  else
417  {
418  // Short form expects a prior entry in the params map whose value will be overriden here:
419  Json::Value::Members members = json_node.getMemberNames();
420  for (auto &member : members)
421  {
422  JsonParam& p = getParameter(member.c_str());
423  if (p.name().empty())
424  {
425  ossimNotify(ossimNotifyLevel_WARN)<<"JsonConfig::loadJSON(): Attempted to override "
426  "nonexistent parameter <"<< member << ">. Ignoring request."<<endl;
427  continue;
428  }
429  if (p.descr().contains("DEPRECATED"))
430  {
431  ossimNotify(ossimNotifyLevel_WARN)<<"JsonConfig::loadJSON() Parameter "<<p.name()
432  <<" "<<p.descr()<<endl;
433  continue;
434  }
435 
436  // Create a full JSON representation of the named parameter from the default list, replace
437  // its value, and recreate the parameter from the updated full JSON:
438  Json::Value paramNode;
439  p.saveJSON(paramNode);
440  paramNode["value"] = json_node[p.name().string()];
441  p.loadJSON(paramNode);
442  }
443  }
444 }
JsonParam & getParameter(const char *paramName)
Returns a parameter (might be a null parameter if paramName not found in the configuration.
Definition: JsonConfig.cpp:377
void setParameter(const JsonParam &p)
Adds parameter to the configuration.
Definition: JsonConfig.cpp:385
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)

◆ open()

bool ossim::JsonConfig::open ( const ossimFilename configFile)

Opens and parses JSON file. The "parameters" keyword is expected in the root node.

Definition at line 359 of file JsonConfig.cpp.

References loadJSON(), and ossimString::string().

Referenced by JsonConfig().

360 {
361  ifstream configFile (configFileName.string());
362  if (configFile.fail())
363  return false;
364  Json::Value jsonRoot;
365  configFile >> jsonRoot;
366  if (jsonRoot.empty())
367  return false;
368  if (jsonRoot.isMember("parameters"))
369  {
370  Json::Value& paramsNode = jsonRoot["parameters"];
371  loadJSON(paramsNode);
372  }
373  configFile.close();
374  return true;
375 }
std::basic_ifstream< char > ifstream
Class for char input file streams.
Definition: ossimIosFwd.h:44
virtual void loadJSON(const Json::Value &params_json_node)
Reads the params controlling the process from the JSON node named "parameters".
Definition: JsonConfig.cpp:403

◆ paramExists()

bool ossim::JsonConfig::paramExists ( const char *  paramName) const

Definition at line 395 of file JsonConfig.cpp.

References m_paramsMap.

Referenced by ATP::AtpTileSource::filterPoints(), ATP::ossimDescriptorSource::getTile(), ATP::ossimCorrelationSource::getTile(), ATP::AtpTileSource::initializeStaticMembers(), and ATP::ossimCorrelationSource::OpenCVCorrelation().

396 {
397  auto i = m_paramsMap.find(string(paramName));
398  if (i != m_paramsMap.end())
399  return true;
400  return false;
401 }
std::map< std::string, JsonParam > m_paramsMap
Definition: JsonConfig.h:103

◆ saveJSON()

void ossim::JsonConfig::saveJSON ( Json::Value &  params_json_node) const
virtual

Reads the params controlling the process from the JSON node named "parameters".

Implements ossim::JsonInterface.

Definition at line 447 of file JsonConfig.cpp.

References m_paramsMap.

Referenced by ATP::ossimAtpTool::getParameters(), ossimDemTool::getParameters(), and ossim::operator<<().

448 {
449 
450  map<string, JsonParam>::const_iterator param = m_paramsMap.begin();
451  int entry = 0;
452  while (param != m_paramsMap.end())
453  {
454  Json::Value paramNode;
455  param->second.saveJSON(paramNode);
456  json_node[entry++] = paramNode;
457  ++param;
458  }
459 }
std::map< std::string, JsonParam > m_paramsMap
Definition: JsonConfig.h:103

◆ setParameter()

void ossim::JsonConfig::setParameter ( const JsonParam p)

Adds parameter to the configuration.

Any previous parameter of the same name is replaced.

Definition at line 385 of file JsonConfig.cpp.

References m_paramsMap, ossim::JsonParam::name(), and ossimString::string().

Referenced by loadJSON().

386 {
387  // Tricky stuff to make sure it is a deep copy of the parameter:
388  string key = p.name().string();
389  std::map<std::string, JsonParam>::iterator iter = m_paramsMap.find(key);
390  if (iter != m_paramsMap.end())
391  m_paramsMap.erase(key);
392  m_paramsMap.emplace(key, p);
393 }
std::map< std::string, JsonParam > m_paramsMap
Definition: JsonConfig.h:103

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  out,
const JsonConfig obj 
)
friend

Outputs JSON to output stream provided.

Definition at line 472 of file JsonConfig.cpp.

473 {
474  Json::Value configJsonNode;
475  obj.saveJSON(configJsonNode);
476  out<<configJsonNode<<endl;
477  return out;
478 }

Member Data Documentation

◆ m_paramsMap

std::map<std::string, JsonParam> ossim::JsonConfig::m_paramsMap
protected

◆ s_nullParam

JsonParam ossim::JsonConfig::s_nullParam
staticprotected

Definition at line 104 of file JsonConfig.h.

Referenced by getParameter().


The documentation for this class was generated from the following files: