28#ifndef GRID_SERIALISATION_JSON_IO_H
29#define GRID_SERIALISATION_JSON_IO_H
39#include <Grid/json/json.hpp>
42using json = nlohmann::json;
53 void push(
const std::string &s);
57#if defined(GRID_CUDA) || defined(GRID_HIP)
58 void writeDefault(
const std::string &s,
const Grid::ComplexD &x)
60 std::complex<double> z(
real(x),
imag(x));
63 void writeDefault(
const std::string &s,
const Grid::ComplexF &x)
65 std::complex<float> z(
real(x),
imag(x));
70 void writeDefault(
const std::string &s,
const std::complex<U> &x);
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);
76 template<std::
size_t N>
77 void writeDefault(
const std::string &s,
const char(&x)[N]);
79 void writeDefault(
const std::string &s,
const std::string &x);
85 std::ostringstream
ss_;
93 bool push(
const std::string &s);
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)
107 std::complex<double> z;
113 std::complex<float> z;
140 template <
typename U>
144 std::ostringstream os;
145 os << std::boolalpha << x;
147 ss_ <<
"\""<< s <<
"\" : " << os.str() <<
" ," ;
149 ss_ << os.str() <<
" ," ;
152 template <
typename U>
156 std::ostringstream os;
157 os <<
"["<< std::boolalpha << x.real() <<
", " << x.imag() <<
"]";
159 ss_ <<
"\""<< s <<
"\" : " << os.str() <<
" ," ;
161 ss_ << os.str() <<
" ," ;
164 template <
typename U,
typename P>
168 std::ostringstream os;
169 os <<
"["<< std::boolalpha <<
"\""<< x.first <<
"\" , \"" << x.second <<
"\" ]";
171 ss_ <<
"\""<< s <<
"\" : " << os.str() <<
" ," ;
173 ss_ << os.str() <<
" ," ;
176 template <
typename U>
182 ss_ <<
" \""<<s<<
"\" : [";
193 template<std::
size_t N>
198 ss_ <<
"\""<< s <<
"\" : \"" << x <<
"\" ," ;
200 ss_ <<
"\"" << x <<
"\" ," ;
204 template <
typename U>
223 template <
typename U,
typename P>
235 json::iterator it = j.begin();
241 output = std::pair<U,P>(first,second);
246 template <
typename U>
252 json::iterator it = j.begin();
258 output = std::complex<U>(tmp1,tmp2);
265 template <
typename U>
275 for (json::iterator it = j.begin(); it != j.end(); ++it) {
278 output.resize(i + 1);
279 read(
"", output[i++]);
Lattice< vobj > real(const Lattice< vobj > &lhs)
Lattice< vobj > imag(const Lattice< vobj > &lhs)
std::complex< RealF > ComplexF
std::complex< RealD > ComplexD
static INTERNAL_PRECISION U
std::vector< bool > do_pop
virtual ~JSONReader(void)=default
bool nextElement(const std::string &s)
JSONReader(const std::string &fileName)
void readDefault(const std::string &s, U &output)
bool push(const std::string &s)
std::vector< json > jold_
void writeDefault(const std::string &s, const U &x)
JSONWriter(const std::string &fileName)
virtual ~JSONWriter(void)
void push(const std::string &s)
std::enable_if< std::is_base_of< Serializable, U >::value, void >::type read(const std::string &s, U &output)
std::enable_if< std::is_base_of< Serializable, U >::value >::type write(const std::string &s, const U &output)