SSAGES  0.9.3
Software Suite for Advanced General Ensemble Simulations
Public Member Functions | Private Attributes | List of all members
SSAGES::JSONLoader Class Reference

Class for loading JSON content from files. More...

#include <JSONLoader.h>

Public Member Functions

 JSONLoader ()
 Constructor. More...
 
Json::Value LoadFile (const std::string &filename, const mxx::comm &world)
 Load file and return JSON tree. More...
 

Private Attributes

std::vector< std::unique_ptr< JSONLoaderPlugin > > plugins_
 List of plugins.
 

Detailed Description

Class for loading JSON content from files.

Definition at line 41 of file JSONLoader.h.

Constructor & Destructor Documentation

◆ JSONLoader()

SSAGES::JSONLoader::JSONLoader ( )
inline

Constructor.

The constructer automatically inserts the IncludePlugin into the list of plugins.

Definition at line 53 of file JSONLoader.h.

53  : plugins_(0)
54  {
55  plugins_.push_back(std::unique_ptr<JSONLoaderPlugin>(new IncludePlugin()));
56  }
std::vector< std::unique_ptr< JSONLoaderPlugin > > plugins_
List of plugins.
Definition: JSONLoader.h:45

References plugins_.

Member Function Documentation

◆ LoadFile()

Json::Value SSAGES::JSONLoader::LoadFile ( const std::string &  filename,
const mxx::comm &  world 
)
inline

Load file and return JSON tree.

Parameters
filenameName of the JSON file to load.
worldMPI global communicator.
Returns
JSON Value containing the contents of the file.

This function loads JSON content from a given file. For each plugin in plugins_ the filter defined with this plugin will be applied to the contents loaded from the file.

Definition at line 68 of file JSONLoader.h.

69  {
70  std::string contents, path;
71  if(world.rank() == 0)
72  {
73  contents = GetFileContents(filename);
74  path = GetFilePath(filename);
75  }
76 
77  mxx::bcast(contents, 0, world);
78  mxx::bcast(path, 0, world);
79 
80  for(auto& plugin : plugins_)
81  plugin->ApplyFilter(contents, path);
82 
83  // Read JSON.
84  Json::Value root;
85  std::string errors;
86  Json::CharReaderBuilder rbuilder;
87  Json::CharReader* reader = rbuilder.newCharReader();
88  if(!reader->parse(contents.c_str(), contents.c_str() + contents.size(), &root, &errors))
89  {
90  throw std::invalid_argument(
91  filename + ": " + errors
92  );
93  }
94  delete reader;
95  return root;
96  }
std::string GetFileContents(const std::string filename)
Read contents from a file.
Definition: FileContents.h:46
std::string GetFilePath(const std::string filename)
Gets file path from filename.
Definition: FileContents.h:62

References SSAGES::GetFileContents(), SSAGES::GetFilePath(), and plugins_.

Here is the call graph for this function:

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