40 void getValue(
const std::string & value_name, T & value)
const 42 if (!
_root.contains(value_name))
45 "' but the JSON file does not contain this key directly at the root level");
56 void getValue(
const std::vector<std::string> & value_keys, T & value)
const 58 if (!value_keys.size())
59 mooseError(
"There should be at least one key to retrieve a value from the JSON");
62 auto * current_node = &
_root[value_keys[0]];
63 for (
const auto key_index :
index_range(value_keys))
65 if (key_index == value_keys.size() - 1)
67 value = current_node->get<T>();
70 current_node = &(*current_node)[value_keys[key_index + 1]];
80 void getVector(
const std::string & vector_name, std::vector<T> & vector_to_fill)
const 82 if (!
_root.contains(vector_name))
85 "' but the JSON file does not contain this key at the root level");
86 vector_to_fill.clear();
87 for (
const auto & item :
_root[vector_name])
88 vector_to_fill.push_back(item.get<T>());
97 void getVector(
const std::vector<std::string> & vector_keys,
98 std::vector<T> & vector_to_fill)
const 100 if (!vector_keys.size())
101 mooseError(
"There should be at least one key to retrieve a value from the JSON");
104 auto * current_node = &
_root[vector_keys[0]];
105 for (
const auto key_index :
index_range(vector_keys))
107 if (key_index == vector_keys.size() - 1)
109 if (!current_node->is_array())
110 mooseError(
"Cannot retrieve a vector from JSON node",
112 "obtained with last key",
113 vector_keys[key_index]);
114 vector_to_fill.clear();
115 for (
const auto & item : *current_node)
116 vector_to_fill.push_back(item.get<T>());
119 if (current_node->is_array())
120 mooseError(
"Cannot obtain nested JSON item with key",
121 vector_keys[key_index + 1],
122 "because the current item is an array:",
124 current_node = &(*current_node)[vector_keys[key_index + 1]];
133 void read(
const FileName & filename);
virtual void finalize() override
Required implementation of a pure virtual function (not used)
JSONFileReader(const InputParameters ¶meters)
virtual void initialize() override
Required implementation of a pure virtual function (not used)
nlohmann::json _root
JSON data.
User object that reads a JSON file and makes its data available to other objects. ...
void read(const FileName &filename)
Read the JSON file and load it into _root.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
void getValue(const std::string &value_name, T &value) const
Getters for single values.
void getVector(const std::string &vector_name, std::vector< T > &vector_to_fill) const
Getter for vector values.
virtual void execute() override
Read the file again.
const FileName & _filename
Database filename.
void getVector(const std::vector< std::string > &vector_keys, std::vector< T > &vector_to_fill) const
Get a vector in the JSON file/tree using the keys in the 'vector_keys' vector one by one to traverse ...
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
const InputParameters & parameters() const
Get the parameters of the object.
static InputParameters validParams()
auto index_range(const T &sizable)
void getValue(const std::vector< std::string > &value_keys, T &value) const
Get a value in the JSON file/tree using the keys in the 'value_keys' one by one to traverse the JSON ...