SSAGES  0.1
A MetaDynamics Package
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

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

Member Function Documentation

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.

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

69  {
70  std::string contents, path;
71  if(world.rank() == 0)
72  {
73  contents = GetFileContents(filename.c_str());
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::Reader reader;
85  Json::Value root;
86  if(!reader.parse(contents, root))
87  throw std::invalid_argument(
88  filename + ": " + reader.getFormattedErrorMessages()
89  );
90  return root;
91  }
std::string GetFilePath(const std::string &str)
Gets file path from filename.
Definition: FileContents.h:73
std::vector< std::unique_ptr< JSONLoaderPlugin > > plugins_
List of plugins.
Definition: JSONLoader.h:45
std::string GetFileContents(const char *filename)
Read contents from a file.
Definition: FileContents.h:47

Here is the call graph for this function:


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