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  nlohmann::json &getShadow();
95 
102  nlohmann::json invertShadow() const;
103 
111  void declareFullyRead();
112 
113  SupportedLanguages originallySpecifiedAs{SupportedLanguages::JSON};
114 
115  private:
122  std::shared_ptr<nlohmann::json> m_originalJSON;
132  std::shared_ptr<nlohmann::json> m_shadow;
138  nlohmann::json *m_positionInOriginal;
144  nlohmann::json *m_positionInShadow;
145  bool m_trace = true;
146 
147  void invertShadow(
148  nlohmann::json &result, nlohmann::json const &shadow) const;
149 
150  TracingJSON(
151  std::shared_ptr<nlohmann::json> originalJSON,
152  std::shared_ptr<nlohmann::json> shadow,
153  nlohmann::json *positionInOriginal,
154  nlohmann::json *positionInShadow,
155  SupportedLanguages originallySpecifiedAs,
156  bool trace);
157  };
158 
159  template <typename Key>
160  TracingJSON TracingJSON::operator[](Key &&key)
161  {
162  nlohmann::json *newPositionInOriginal =
163  &m_positionInOriginal->operator[](key);
164  // If accessing a leaf in the JSON tree from an object (not an array!)
165  // erase the corresponding key
166  static nlohmann::json nullvalue;
167  nlohmann::json *newPositionInShadow = &nullvalue;
168  if (m_trace && m_positionInOriginal->is_object())
169  {
170  newPositionInShadow = &m_positionInShadow->operator[](key);
171  }
172  bool traceFurther = newPositionInOriginal->is_object();
173  return TracingJSON(
174  m_originalJSON,
175  m_shadow,
176  newPositionInOriginal,
177  newPositionInShadow,
178  originallySpecifiedAs,
179  traceFurther);
180  }
181 
182  nlohmann::json tomlToJson(toml::value const &val);
183  toml::value jsonToToml(nlohmann::json const &val);
184 
194  ParsedConfig parseOptions(std::string const &options, bool considerFiles);
195 
196 #if openPMD_HAVE_MPI
197 
201  ParsedConfig
202  parseOptions(std::string const &options, MPI_Comm comm, bool considerFiles);
203 
204 #endif
205 
215  nlohmann::json &lowerCase(nlohmann::json &);
216 
224  std::optional<std::string> asStringDynamic(nlohmann::json const &);
225 
229  std::optional<std::string> asLowerCaseStringDynamic(nlohmann::json const &);
230 
235  extern std::vector<std::string> backendKeys();
236 
243  void warnGlobalUnusedOptions(TracingJSON const &config);
244 
249  nlohmann::json &
250  merge(nlohmann::json &defaultVal, nlohmann::json const &overwrite);
251 
252  nlohmann::json &filterByTemplate(
253  nlohmann::json &defaultVal, nlohmann::json const &positiveMask);
254 } // namespace json
255 } // namespace openPMD
Extend nlohmann::json with tracing of which keys have been accessed by operator[]().
Definition: JSON_internal.hpp:68
nlohmann::json & json()
Access the underlying JSON value.
Definition: JSON_internal.hpp:79
void declareFullyRead()
Declare all keys of the current object read.
Definition: JSON.cpp:114
nlohmann::json invertShadow() const
Invert the "shadow", i.e.
Definition: JSON.cpp:71
nlohmann::json const & getShadow() const
Get the "shadow", i.e.
Definition: JSON.cpp:61
Public definitions of openPMD-api.
Definition: Date.cpp:29
Definition: JSON_internal.hpp:49