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 &
64  json()
65  {
66  return *m_positionInOriginal;
67  }
68 
69  template< typename Key >
70  TracingJSON operator[]( Key && key );
71 
78  nlohmann::json const &
79  getShadow();
80 
87  nlohmann::json
88  invertShadow();
89 
94  void
96 
97  private:
104  std::shared_ptr< nlohmann::json > m_originalJSON;
114  std::shared_ptr< nlohmann::json > m_shadow;
120  nlohmann::json * m_positionInOriginal;
126  nlohmann::json * m_positionInShadow;
127  bool m_trace = true;
128 
129  void
130  invertShadow( nlohmann::json & result, nlohmann::json const & shadow );
131 
132  TracingJSON(
133  std::shared_ptr< nlohmann::json > originalJSON,
134  std::shared_ptr< nlohmann::json > shadow,
135  nlohmann::json * positionInOriginal,
136  nlohmann::json * positionInShadow,
137  bool trace );
138  };
139 
140  template< typename Key >
141  TracingJSON TracingJSON::operator[]( Key && key )
142  {
143  nlohmann::json * newPositionInOriginal =
144  &m_positionInOriginal->operator[]( key );
145  // If accessing a leaf in the JSON tree from an object (not an array!)
146  // erase the corresponding key
147  static nlohmann::json nullvalue;
148  nlohmann::json * newPositionInShadow = &nullvalue;
149  if( m_trace && m_positionInOriginal->is_object() )
150  {
151  newPositionInShadow = &m_positionInShadow->operator[]( key );
152  }
153  bool traceFurther = newPositionInOriginal->is_object();
154  return TracingJSON(
155  m_originalJSON,
156  m_shadow,
157  newPositionInOriginal,
158  newPositionInShadow,
159  traceFurther );
160  }
161 
169  nlohmann::json parseOptions( std::string const & options );
170 
171 #if openPMD_HAVE_MPI
172 
176  nlohmann::json parseOptions( std::string const & options, MPI_Comm comm );
177 
178 #endif
179 
180 } // namespace auxiliary
181 } // namespace openPMD
openPMD::auxiliary::TracingJSON::json
nlohmann::json & json()
Access the underlying JSON value.
Definition: JSON.hpp:64
openPMD::auxiliary::TracingJSON::getShadow
const nlohmann::json & getShadow()
Get the "shadow", i.e.
Definition: JSON.cpp:51
openPMD
Public definitions of openPMD-api.
Definition: Date.cpp:29
openPMD::auxiliary::TracingJSON::invertShadow
nlohmann::json invertShadow()
Invert the "shadow", i.e.
Definition: JSON.cpp:57
openPMD::auxiliary::TracingJSON
Extend nlohmann::json with tracing of which keys have been accessed by operator[]().
Definition: JSON.hpp:52
openPMD::auxiliary::TracingJSON::declareFullyRead
void declareFullyRead()
Declare all keys of the current object read.
Definition: JSON.cpp:97