Grid 0.7.0
JSON_IO.h
Go to the documentation of this file.
1 /*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/serialisation/JSON_IO.h
6
7 Copyright (C) 2015
8
9 Author: Guido Cossu<guido.cossu@ed.ac.uk>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License along
22 with this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
25 See the full license in the file "LICENSE" in the top level distribution directory
26 *************************************************************************************/
27 /* END LEGAL */
28#ifndef GRID_SERIALISATION_JSON_IO_H
29#define GRID_SERIALISATION_JSON_IO_H
30
31#include <iostream>
32#include <iomanip>
33#include <iostream>
34#include <fstream>
35#include <sstream>
36#include <vector>
37#include <cassert>
38
39#include <Grid/json/json.hpp>
40
41// for convenience
42using json = nlohmann::json;
43
44namespace Grid
45{
46
47 class JSONWriter: public Writer<JSONWriter>
48 {
49
50 public:
51 JSONWriter(const std::string &fileName);
52 virtual ~JSONWriter(void);
53 void push(const std::string &s);
54 void pop(void);
55 template <typename U>
56 void writeDefault(const std::string &s, const U &x);
57#if defined(GRID_CUDA) || defined(GRID_HIP)
58 void writeDefault(const std::string &s, const Grid::ComplexD &x)
59 {
60 std::complex<double> z(real(x),imag(x));
61 writeDefault(s,z);
62 }
63 void writeDefault(const std::string &s, const Grid::ComplexF &x)
64 {
65 std::complex<float> z(real(x),imag(x));
66 writeDefault(s,z);
67 }
68#endif
69 template <typename U>
70 void writeDefault(const std::string &s, const std::complex<U> &x);
71 template <typename U>
72 void writeDefault(const std::string &s, const std::vector<U> &x);
73 template <typename U, typename P>
74 void writeDefault(const std::string &s, const std::pair<U,P> &x);
75
76 template<std::size_t N>
77 void writeDefault(const std::string &s, const char(&x)[N]);
78
79 void writeDefault(const std::string &s, const std::string &x);
80
81
82 private:
83 void delete_comma();
84 std::string fileName_;
85 std::ostringstream ss_;
86 };
87
88 class JSONReader: public Reader<JSONReader>
89 {
90 public:
91 JSONReader(const std::string &fileName);
92 virtual ~JSONReader(void) = default;
93 bool push(const std::string &s);
94 void pop(void);
95 bool nextElement(const std::string &s);
96 template <typename U>
97 void readDefault(const std::string &s, U &output);
98 template <typename U>
99 void readDefault(const std::string &s, std::complex<U> &output);
100 template <typename U>
101 void readDefault(const std::string &s, std::vector<U> &output);
102 template <typename U, typename P>
103 void readDefault(const std::string &s, std::pair<U,P> &output);
104#if defined(GRID_CUDA) || defined(GRID_HIP)
105 void readDefault(const std::string &s, ComplexD &output)
106 {
107 std::complex<double> z;
108 readDefault(s,z);
109 output = ComplexD(real(z),imag(z));
110 }
111 void readDefault(const std::string &s, ComplexF &output)
112 {
113 std::complex<float> z;
114 readDefault(s,z);
115 output = ComplexD(real(z),imag(z));
116 }
117#endif
118
119 private:
120 json jobject_; // main object
121 json jcur_; // current json object
122 std::vector<json> jold_; // previous json object
123 std::string fileName_;
124 std::vector<bool> do_pop;
125 json::iterator it_;
126 json::iterator it_end_;
127 };
128
129 template <>
131 static const bool value = true;
132 };
133
134 template <>
136 static const bool value = true;
137 };
138
139 // Writer template implementation ////////////////////////////////////////////
140 template <typename U>
141 void JSONWriter::writeDefault(const std::string &s, const U &x)
142 {
143 //std::cout << "JSONWriter::writeDefault(U) : " << s << " " << x <<std::endl;
144 std::ostringstream os;
145 os << std::boolalpha << x;
146 if (s.size())
147 ss_ << "\""<< s << "\" : " << os.str() << " ," ;
148 else
149 ss_ << os.str() << " ," ;
150 }
151
152 template <typename U>
153 void JSONWriter::writeDefault(const std::string &s, const std::complex<U> &x)
154 {
155 //std::cout << "JSONWriter::writeDefault(complex) : " << s << " " << x << std::endl;
156 std::ostringstream os;
157 os << "["<< std::boolalpha << x.real() << ", " << x.imag() << "]";
158 if (s.size())
159 ss_ << "\""<< s << "\" : " << os.str() << " ," ;
160 else
161 ss_ << os.str() << " ," ;
162 }
163
164 template <typename U, typename P>
165 void JSONWriter::writeDefault(const std::string &s, const std::pair<U,P> &x)
166 {
167 //std::cout << "JSONWriter::writeDefault(pair) : " << s << " " << x << std::endl;
168 std::ostringstream os;
169 os << "["<< std::boolalpha << "\""<< x.first << "\" , \"" << x.second << "\" ]";
170 if (s.size())
171 ss_ << "\""<< s << "\" : " << os.str() << " ," ;
172 else
173 ss_ << os.str() << " ," ;
174 }
175
176 template <typename U>
177 void JSONWriter::writeDefault(const std::string &s, const std::vector<U> &x)
178 {
179 //std::cout << "JSONWriter::writeDefault(vec U) : " << s << std::endl;
180
181 if (s.size())
182 ss_ << " \""<<s<<"\" : [";
183 else
184 ss_ << " [";
185 for (auto &x_i: x)
186 {
187 write("", x_i);
188 }
189 delete_comma();
190 ss_<< "],";
191 }
192
193 template<std::size_t N>
194 void JSONWriter::writeDefault(const std::string &s, const char(&x)[N]){
195 //std::cout << "JSONWriter::writeDefault(char U) : " << s << " " << x << std::endl;
196
197 if (s.size())
198 ss_ << "\""<< s << "\" : \"" << x << "\" ," ;
199 else
200 ss_ << "\"" << x << "\" ," ;
201 }
202
203 // Reader template implementation ////////////////////////////////////////////
204 template <typename U>
205 void JSONReader::readDefault(const std::string &s, U &output)
206 {
207 //std::cout << "JSONReader::readDefault(U) : " << s << " : "<< jcur_ << std::endl;
208
209 if (s.size()){
210 //std::cout << "String: "<< jcur_[s] << std::endl;
211 output = jcur_[s];
212 }
213 else
214 {
215 //std::cout << "String: "<< jcur_ << std::endl;
216 output = jcur_;
217 }
218
219
220 }
221
222 // Reader template implementation ////////////////////////////////////////////
223 template <typename U, typename P>
224 void JSONReader::readDefault(const std::string &s, std::pair<U,P> &output)
225 {
226 U first;
227 P second;
228 json j;
229 if (s.size()){
230 //std::cout << "JSONReader::readDefault(pair) : " << s << " | "<< jcur_[s] << std::endl;
231 j = jcur_[s];
232 } else {
233 j = jcur_;
234 }
235 json::iterator it = j.begin();
236 jcur_ = *it;
237 read("", first);
238 it++;
239 jcur_ = *it;
240 read("", second);
241 output = std::pair<U,P>(first,second);
242 }
243
244
245
246 template <typename U>
247 void JSONReader::readDefault(const std::string &s, std::complex<U> &output)
248 {
249 U tmp1, tmp2;
250 //std::cout << "JSONReader::readDefault(complex U) : " << s << " : "<< jcur_ << std::endl;
251 json j = jcur_;
252 json::iterator it = j.begin();
253 jcur_ = *it;
254 read("", tmp1);
255 it++;
256 jcur_ = *it;
257 read("", tmp2);
258 output = std::complex<U>(tmp1,tmp2);
259 }
260
261
262 template <>
263 void JSONReader::readDefault(const std::string &s, std::string &output);
264
265 template <typename U>
266 void JSONReader::readDefault(const std::string &s, std::vector<U> &output)
267 {
268 std::string buf;
269 unsigned int i = 0;
270 //std::cout << "JSONReader::readDefault(vec) : " << jcur_ << std::endl;
271 if (s.size())
272 push(s);
273
274 json j = jcur_;
275 for (json::iterator it = j.begin(); it != j.end(); ++it) {
276 jcur_ = *it;
277 //std::cout << "Value: " << it.value() << "\n";
278 output.resize(i + 1);
279 read("", output[i++]);
280 }
281
282
283 jcur_ = j;
284 if (s.size())
285 pop();
286 }
287
288}
289#endif
nlohmann::json json
Definition JSON_IO.h:42
Lattice< vobj > real(const Lattice< vobj > &lhs)
Lattice< vobj > imag(const Lattice< vobj > &lhs)
std::complex< RealF > ComplexF
Definition Simd.h:78
std::complex< RealD > ComplexD
Definition Simd.h:79
static INTERNAL_PRECISION U
Definition Zolotarev.cc:230
std::vector< bool > do_pop
Definition JSON_IO.h:124
virtual ~JSONReader(void)=default
bool nextElement(const std::string &s)
Definition JSON_IO.cc:139
JSONReader(const std::string &fileName)
Definition JSON_IO.cc:89
void readDefault(const std::string &s, U &output)
Definition JSON_IO.h:205
bool push(const std::string &s)
Definition JSON_IO.cc:101
void pop(void)
Definition JSON_IO.cc:126
json::iterator it_end_
Definition JSON_IO.h:126
std::string fileName_
Definition JSON_IO.h:123
std::vector< json > jold_
Definition JSON_IO.h:122
json::iterator it_
Definition JSON_IO.h:125
void pop(void)
Definition JSON_IO.cc:58
void writeDefault(const std::string &s, const U &x)
Definition JSON_IO.h:141
std::string fileName_
Definition JSON_IO.h:84
std::ostringstream ss_
Definition JSON_IO.h:85
JSONWriter(const std::string &fileName)
Definition JSON_IO.cc:34
void delete_comma()
Definition JSON_IO.cc:65
virtual ~JSONWriter(void)
Definition JSON_IO.cc:37
void push(const std::string &s)
Definition JSON_IO.cc:49
std::enable_if< std::is_base_of< Serializable, U >::value, void >::type read(const std::string &s, U &output)
Definition BaseIO.h:393
std::enable_if< std::is_base_of< Serializable, U >::value >::type write(const std::string &s, const U &output)
Definition BaseIO.h:260
static const bool value
Definition JSON_IO.h:131
static const bool value
Definition JSON_IO.h:136