openPMD-api
JSON.hpp
1 /* Copyright 2020 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 #include <memory> // std::shared_ptr
29 #include <utility> // std::forward
30 
31 namespace openPMD
32 {
33 namespace auxiliary
34 {
49  {
50  public:
51  TracingJSON();
52  TracingJSON( nlohmann::json );
53 
59  inline nlohmann::json &
60  json()
61  {
62  return *m_positionInOriginal;
63  }
64 
65  template< typename Key >
66  TracingJSON operator[]( Key && key );
67 
74  nlohmann::json const &
75  getShadow();
76 
83  nlohmann::json
84  invertShadow();
85 
90  void
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
126  invertShadow( nlohmann::json & result, nlohmann::json const & shadow );
127 
128  TracingJSON(
129  std::shared_ptr< nlohmann::json > originalJSON,
130  std::shared_ptr< nlohmann::json > shadow,
131  nlohmann::json * positionInOriginal,
132  nlohmann::json * positionInShadow,
133  bool trace );
134  };
135 
136  template< typename Key >
137  TracingJSON TracingJSON::operator[]( Key && key )
138  {
139  nlohmann::json * newPositionInOriginal =
140  &m_positionInOriginal->operator[]( key );
141  // If accessing a leaf in the JSON tree from an object (not an array!)
142  // erase the corresponding key
143  static nlohmann::json nullvalue;
144  nlohmann::json * newPositionInShadow = &nullvalue;
145  if( m_trace && m_positionInOriginal->is_object() )
146  {
147  newPositionInShadow = &m_positionInShadow->operator[]( key );
148  }
149  bool traceFurther = newPositionInOriginal->is_object();
150  return TracingJSON(
151  m_originalJSON,
152  m_shadow,
153  newPositionInOriginal,
154  newPositionInShadow,
155  traceFurther );
156  }
157 } // namespace auxiliary
158 } // namespace openPMD
159 
nlohmann::json invertShadow()
Invert the "shadow", i.e.
Definition: JSON.cpp:52
nlohmann::json const & getShadow()
Get the "shadow", i.e.
Definition: JSON.cpp:46
Public definitions of openPMD-api.
Definition: Date.cpp:29
void declareFullyRead()
Declare all keys of the current object read.
Definition: JSON.cpp:92
Extend nlohmann::json with tracing of which keys have been accessed by operator[]().
Definition: JSON.hpp:48
nlohmann::json & json()
Access the underlying JSON value.
Definition: JSON.hpp:60