openPMD-api
JSON_internal.hpp
1 /* Copyright 2020-2021 Franz Poeschel
2  *
3  * This file is part of openPMD-api.
4  *
5  * openPMD-api is free software: you can redistribute it and/or modify
6  * it under the terms of of either the GNU General Public License or
7  * the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * openPMD-api is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License and the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * and the GNU Lesser General Public License along with openPMD-api.
19  * If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #pragma once
23 
24 #include "openPMD/config.hpp"
25 
26 #include <nlohmann/json.hpp>
27 #include <toml.hpp>
28 
29 #include <optional>
30 
31 #if openPMD_HAVE_MPI
32 #include <mpi.h>
33 #endif
34 
35 #include <memory> // std::shared_ptr
36 #include <utility> // std::forward
37 
38 namespace openPMD
39 {
40 namespace json
41 {
42  enum class SupportedLanguages
43  {
44  JSON,
45  TOML
46  };
47 
48  struct ParsedConfig
49  {
50  nlohmann::json config;
51  SupportedLanguages originallySpecifiedAs{SupportedLanguages::JSON};
52  };
53 
68  {
69  public:
70  TracingJSON();
71  TracingJSON(nlohmann::json, SupportedLanguages);
73 
79  inline nlohmann::json &json()
80  {
81  return *m_positionInOriginal;
82  }
83 
84  template <typename Key>
85  TracingJSON operator[](Key &&key);
86 
93  nlohmann::json const &getShadow() const;
94 
101  nlohmann::json invertShadow() const;
102 
110  void declareFullyRead();
111 
112  SupportedLanguages originallySpecifiedAs{SupportedLanguages::JSON};
113 
114  private:
121  std::shared_ptr<nlohmann::json> m_originalJSON;
131  std::shared_ptr<nlohmann::json> m_shadow;
137  nlohmann::json *m_positionInOriginal;
143  nlohmann::json *m_positionInShadow;
144  bool m_trace = true;
145 
146  void invertShadow(
147  nlohmann::json &result, nlohmann::json const &shadow) const;
148 
149  TracingJSON(
150  std::shared_ptr<nlohmann::json> originalJSON,
151  std::shared_ptr<nlohmann::json> shadow,
152  nlohmann::json *positionInOriginal,
153  nlohmann::json *positionInShadow,
154  SupportedLanguages originallySpecifiedAs,
155  bool trace);
156  };
157 
158  template <typename Key>
159  TracingJSON TracingJSON::operator[](Key &&key)
160  {
161  nlohmann::json *newPositionInOriginal =
162  &m_positionInOriginal->operator[](key);
163  // If accessing a leaf in the JSON tree from an object (not an array!)
164  // erase the corresponding key
165  static nlohmann::json nullvalue;
166  nlohmann::json *newPositionInShadow = &nullvalue;
167  if (m_trace && m_positionInOriginal->is_object())
168  {
169  newPositionInShadow = &m_positionInShadow->operator[](key);
170  }
171  bool traceFurther = newPositionInOriginal->is_object();
172  return TracingJSON(
173  m_originalJSON,
174  m_shadow,
175  newPositionInOriginal,
176  newPositionInShadow,
177  originallySpecifiedAs,
178  traceFurther);
179  }
180 
181  nlohmann::json tomlToJson(toml::value const &val);
182  toml::value jsonToToml(nlohmann::json const &val);
183 
193  ParsedConfig parseOptions(std::string const &options, bool considerFiles);
194 
195 #if openPMD_HAVE_MPI
196 
201  parseOptions(std::string const &options, MPI_Comm comm, bool considerFiles);
202 
203 #endif
204 
214  nlohmann::json &lowerCase(nlohmann::json &);
215 
223  std::optional<std::string> asStringDynamic(nlohmann::json const &);
224 
228  std::optional<std::string> asLowerCaseStringDynamic(nlohmann::json const &);
229 
234  extern std::vector<std::string> backendKeys;
235 
242  void warnGlobalUnusedOptions(TracingJSON const &config);
243 
248  nlohmann::json &
249  merge(nlohmann::json &defaultVal, nlohmann::json const &overwrite);
250 } // namespace json
251 } // namespace openPMD
Extend nlohmann::json with tracing of which keys have been accessed by operator[]().
Definition: JSON_internal.hpp:67
nlohmann::json & json()
Access the underlying JSON value.
Definition: JSON_internal.hpp:79
Public definitions of openPMD-api.
Definition: JSON_internal.hpp:48