openPMD-api
JSON.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 
28 #if openPMD_HAVE_MPI
29 #include <mpi.h>
30 #endif
31 
32 #include <memory> // std::shared_ptr
33 #include <utility> // std::forward
34 
35 namespace openPMD
36 {
37 namespace auxiliary
38 {
53  {
54  public:
55  TracingJSON();
56  TracingJSON(nlohmann::json);
57 
63  inline nlohmann::json &json()
64  {
65  return *m_positionInOriginal;
66  }
67 
68  template <typename Key>
69  TracingJSON operator[](Key &&key);
70 
77  nlohmann::json const &getShadow();
78 
85  nlohmann::json invertShadow();
86 
91  void declareFullyRead();
92 
93  private:
100  std::shared_ptr<nlohmann::json> m_originalJSON;
110  std::shared_ptr<nlohmann::json> m_shadow;
116  nlohmann::json *m_positionInOriginal;
122  nlohmann::json *m_positionInShadow;
123  bool m_trace = true;
124 
125  void invertShadow(nlohmann::json &result, nlohmann::json const &shadow);
126 
127  TracingJSON(
128  std::shared_ptr<nlohmann::json> originalJSON,
129  std::shared_ptr<nlohmann::json> shadow,
130  nlohmann::json *positionInOriginal,
131  nlohmann::json *positionInShadow,
132  bool trace);
133  };
134 
135  template <typename Key>
136  TracingJSON TracingJSON::operator[](Key &&key)
137  {
138  nlohmann::json *newPositionInOriginal =
139  &m_positionInOriginal->operator[](key);
140  // If accessing a leaf in the JSON tree from an object (not an array!)
141  // erase the corresponding key
142  static nlohmann::json nullvalue;
143  nlohmann::json *newPositionInShadow = &nullvalue;
144  if (m_trace && m_positionInOriginal->is_object())
145  {
146  newPositionInShadow = &m_positionInShadow->operator[](key);
147  }
148  bool traceFurther = newPositionInOriginal->is_object();
149  return TracingJSON(
150  m_originalJSON,
151  m_shadow,
152  newPositionInOriginal,
153  newPositionInShadow,
154  traceFurther);
155  }
156 
164  nlohmann::json parseOptions(std::string const &options);
165 
166 #if openPMD_HAVE_MPI
167 
171  nlohmann::json parseOptions(std::string const &options, MPI_Comm comm);
172 
173 #endif
174 
175 } // namespace auxiliary
176 } // namespace openPMD
nlohmann::json invertShadow()
Invert the "shadow", i.e.
Definition: JSON.cpp:53
nlohmann::json const & getShadow()
Get the "shadow", i.e.
Definition: JSON.cpp:48
Public definitions of openPMD-api.
Definition: Date.cpp:28
void declareFullyRead()
Declare all keys of the current object read.
Definition: JSON.cpp:90
Extend nlohmann::json with tracing of which keys have been accessed by operator[]().
Definition: JSON.hpp:52
nlohmann::json & json()
Access the underlying JSON value.
Definition: JSON.hpp:63