Loading [MathJax]/extensions/tex2jax.js
https://mooseframework.inl.gov
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
JSONFileReader.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include "GeneralUserObject.h"
13 
18 {
19 public:
21 
23 
25  virtual void initialize() override {}
26 
28  virtual void finalize() override {}
29 
31  virtual void execute() override { read(_filename); }
32 
34 
39  template <typename T>
40  void getValue(const std::string & value_name, T & value) const
41  {
42  if (!_root.contains(value_name))
43  mooseError("Attempted to get '",
44  value_name,
45  "' but the JSON file does not contain this key directly at the root level");
46  value = _root[value_name].get<T>();
47  }
48 
55  template <typename T>
56  void getValue(const std::vector<std::string> & value_keys, T & value) const
57  {
58  if (!value_keys.size())
59  mooseError("There should be at least one key to retrieve a value from the JSON");
60 
61  // traverse the JSON tree
62  auto * current_node = &_root[value_keys[0]];
63  for (const auto key_index : index_range(value_keys))
64  {
65  if (key_index == value_keys.size() - 1)
66  {
67  value = current_node->get<T>();
68  break;
69  }
70  current_node = &(*current_node)[value_keys[key_index + 1]];
71  }
72  }
74 
79  template <typename T>
80  void getVector(const std::string & vector_name, std::vector<T> & vector_to_fill) const
81  {
82  if (!_root.contains(vector_name))
83  mooseError("Attempted to get '",
84  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>());
89  }
96  template <typename T>
97  void getVector(const std::vector<std::string> & vector_keys,
98  std::vector<T> & vector_to_fill) const
99  {
100  if (!vector_keys.size())
101  mooseError("There should be at least one key to retrieve a value from the JSON");
102 
103  // traverse the JSON tree
104  auto * current_node = &_root[vector_keys[0]];
105  for (const auto key_index : index_range(vector_keys))
106  {
107  if (key_index == vector_keys.size() - 1)
108  {
109  if (!current_node->is_array())
110  mooseError("Cannot retrieve a vector from JSON node",
111  *current_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>());
117  return;
118  }
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:",
123  *current_node);
124  current_node = &(*current_node)[vector_keys[key_index + 1]];
125  }
126  }
127 
128 private:
133  void read(const FileName & filename);
134 
136  const FileName & _filename;
138  nlohmann::json _root;
139 };
virtual void finalize() override
Required implementation of a pure virtual function (not used)
JSONFileReader(const InputParameters &parameters)
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.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
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 &#39;vector_keys&#39; 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 &#39;value_keys&#39; one by one to traverse the JSON ...