1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-10 15:55:37 +00:00
Grid/lib/serialisation/JSON_IO.h

277 lines
7.6 KiB
C
Raw Normal View History

/*************************************************************************************
2017-05-08 21:41:39 +01:00
Grid physics library, www.github.com/paboyle/Grid
Source file: ./lib/serialisation/JSON_IO.h
Copyright (C) 2015
Author: Guido Cossu<guido.cossu@ed.ac.uk>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
See the full license in the file "LICENSE" in the top level distribution directory
*************************************************************************************/
/* END LEGAL */
#ifndef GRID_SERIALISATION_JSON_IO_H
#define GRID_SERIALISATION_JSON_IO_H
#include <iostream>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <cassert>
#include <Grid/json/json.hpp>
// for convenience
using json = nlohmann::json;
namespace Grid
{
2017-05-08 21:41:39 +01:00
class JSONWriter: public Writer<JSONWriter>
{
2017-05-08 21:41:39 +01:00
public:
JSONWriter(const std::string &fileName);
virtual ~JSONWriter(void);
void push(const std::string &s);
void pop(void);
template <typename U>
void writeDefault(const std::string &s, const U &x);
template <typename U>
2017-05-08 21:41:39 +01:00
void writeDefault(const std::string &s, const std::complex<U> &x);
template <typename U>
void writeDefault(const std::string &s, const std::vector<U> &x);
template <typename U, typename P>
void writeDefault(const std::string &s, const std::pair<U,P> &x);
template<std::size_t N>
void writeDefault(const std::string &s, const char(&x)[N]);
private:
void delete_comma();
std::string fileName_;
std::ostringstream ss_;
};
2017-05-08 21:41:39 +01:00
class JSONReader: public Reader<JSONReader>
{
public:
JSONReader(const std::string &fileName);
virtual ~JSONReader(void) = default;
bool push(const std::string &s);
void pop(void);
bool nextElement(const std::string &s);
template <typename U>
void readDefault(const std::string &s, U &output);
template <typename U>
2017-05-08 21:41:39 +01:00
void readDefault(const std::string &s, std::complex<U> &output);
template <typename U>
void readDefault(const std::string &s, std::vector<U> &output);
template <typename U, typename P>
void readDefault(const std::string &s, std::pair<U,P> &output);
private:
json jobject_; // main object
json jcur_; // current json object
std::vector<json> jold_; // previous json object
std::string fileName_;
std::vector<bool> do_pop;
json::iterator it_;
json::iterator it_end_;
};
2017-01-25 12:11:58 +00:00
template <>
struct isReader< JSONReader > {
static const bool value = true;
};
template <>
struct isWriter< JSONWriter > {
static const bool value = true;
};
2017-05-08 21:41:39 +01:00
// Writer template implementation ////////////////////////////////////////////
template <typename U>
void JSONWriter::writeDefault(const std::string &s, const U &x)
{
//std::cout << "JSONWriter::writeDefault(U) : " << s << " " << x <<std::endl;
std::ostringstream os;
os << std::boolalpha << x;
if (s.size())
ss_ << "\""<< s << "\" : " << os.str() << " ," ;
else
ss_ << os.str() << " ," ;
}
2017-07-12 14:55:42 +01:00
/*
2017-07-11 14:31:57 +01:00
// specialize for string
template <>
void JSONWriter::writeDefault(const std::string &s, const std::string &x)
{
//std::cout << "JSONWriter::writeDefault(string) : " << s << std::endl;
2017-07-11 14:31:57 +01:00
std::ostringstream os;
os << std::boolalpha << x;
if (s.size())
ss_ << "\""<< s << "\" : \"" << os.str() << "\" ," ;
else
ss_ << os.str() << " ," ;
}
2017-07-12 14:55:42 +01:00
*/
2017-07-11 14:31:57 +01:00
2017-05-08 21:41:39 +01:00
template <typename U>
void JSONWriter::writeDefault(const std::string &s, const std::complex<U> &x)
{
2017-07-11 14:31:57 +01:00
//std::cout << "JSONWriter::writeDefault(complex) : " << s << " " << x << std::endl;
2017-05-08 21:41:39 +01:00
std::ostringstream os;
os << "["<< std::boolalpha << x.real() << ", " << x.imag() << "]";
if (s.size())
ss_ << "\""<< s << "\" : " << os.str() << " ," ;
else
ss_ << os.str() << " ," ;
}
template <typename U, typename P>
void JSONWriter::writeDefault(const std::string &s, const std::pair<U,P> &x)
{
//std::cout << "JSONWriter::writeDefault(pair) : " << s << " " << x << std::endl;
std::ostringstream os;
os << "["<< std::boolalpha << "\""<< x.first << "\" , \"" << x.second << "\" ]";
if (s.size())
ss_ << "\""<< s << "\" : " << os.str() << " ," ;
else
ss_ << os.str() << " ," ;
}
template <typename U>
void JSONWriter::writeDefault(const std::string &s, const std::vector<U> &x)
{
2017-07-11 14:31:57 +01:00
//std::cout << "JSONWriter::writeDefault(vec U) : " << s << std::endl;
2017-05-08 21:41:39 +01:00
if (s.size())
ss_ << " \""<<s<<"\" : [";
else
ss_ << " [";
for (auto &x_i: x)
{
write("", x_i);
}
delete_comma();
ss_<< "],";
}
2017-05-08 21:41:39 +01:00
template<std::size_t N>
void JSONWriter::writeDefault(const std::string &s, const char(&x)[N]){
2017-07-11 14:31:57 +01:00
//std::cout << "JSONWriter::writeDefault(char U) : " << s << " " << x << std::endl;
2017-05-08 21:41:39 +01:00
if (s.size())
2017-07-11 14:31:57 +01:00
ss_ << "\""<< s << "\" : \"" << x << "\" ," ;
else
2017-07-11 14:31:57 +01:00
ss_ << "\"" << x << "\" ," ;
}
// Reader template implementation ////////////////////////////////////////////
template <typename U>
void JSONReader::readDefault(const std::string &s, U &output)
{
2017-05-08 21:56:44 +01:00
//std::cout << "JSONReader::readDefault(U) : " << s << " : "<< jcur_ << std::endl;
2017-05-08 21:41:39 +01:00
if (s.size()){
2017-05-08 21:56:44 +01:00
//std::cout << "String: "<< jcur_[s] << std::endl;
output = jcur_[s];
}
else
{
2017-05-08 21:56:44 +01:00
//std::cout << "String: "<< jcur_ << std::endl;
2017-05-08 21:41:39 +01:00
output = jcur_;
}
}
2017-05-08 21:41:39 +01:00
// Reader template implementation ////////////////////////////////////////////
template <typename U, typename P>
void JSONReader::readDefault(const std::string &s, std::pair<U,P> &output)
{
U first;
P second;
json j;
if (s.size()){
//std::cout << "JSONReader::readDefault(pair) : " << s << " | "<< jcur_[s] << std::endl;
j = jcur_[s];
} else {
j = jcur_;
}
json::iterator it = j.begin();
jcur_ = *it;
read("", first);
it++;
jcur_ = *it;
read("", second);
output = std::pair<U,P>(first,second);
}
2017-05-08 21:41:39 +01:00
template <typename U>
void JSONReader::readDefault(const std::string &s, std::complex<U> &output)
{
U tmp1, tmp2;
2017-07-11 14:31:57 +01:00
//std::cout << "JSONReader::readDefault(complex U) : " << s << " : "<< jcur_ << std::endl;
2017-05-08 21:41:39 +01:00
json j = jcur_;
json::iterator it = j.begin();
jcur_ = *it;
read("", tmp1);
it++;
jcur_ = *it;
read("", tmp2);
output = std::complex<U>(tmp1,tmp2);
}
template <>
void JSONReader::readDefault(const std::string &s, std::string &output);
2017-05-08 21:41:39 +01:00
template <typename U>
void JSONReader::readDefault(const std::string &s, std::vector<U> &output)
{
std::string buf;
unsigned int i = 0;
2017-05-08 21:56:44 +01:00
//std::cout << "JSONReader::readDefault(vec) : " << jcur_ << std::endl;
if (s.size())
push(s);
2017-05-08 21:41:39 +01:00
json j = jcur_;
for (json::iterator it = j.begin(); it != j.end(); ++it) {
jcur_ = *it;
2017-05-08 21:56:44 +01:00
//std::cout << "Value: " << it.value() << "\n";
output.resize(i + 1);
read("", output[i++]);
}
2017-05-08 21:41:39 +01:00
jcur_ = j;
if (s.size())
pop();
}
2017-05-08 21:41:39 +01:00
}
#endif