1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-09-20 09:15:38 +01:00

Added support for std::pair in the JSON serialiser

This commit is contained in:
Guido Cossu 2017-07-12 14:44:53 +01:00
parent 097c9637ee
commit 184af5bd05
3 changed files with 52 additions and 11 deletions

View File

@ -42,7 +42,7 @@ JSONWriter::~JSONWriter(void)
// write prettified JSON to file
std::ofstream os(fileName_);
std::cout << "JSONWriter::~JSONWriter" << std::endl;
//std::cout << "JSONWriter::~JSONWriter" << std::endl;
os << std::setw(2) << json::parse(ss_.str()) << std::endl;
}
@ -57,7 +57,7 @@ void JSONWriter::push(const string &s)
void JSONWriter::pop(void)
{
std::cout << "JSONWriter::pop" << std::endl;
//std::cout << "JSONWriter::pop" << std::endl;
delete_comma();
ss_ << "},";
}
@ -69,6 +69,7 @@ void JSONWriter::delete_comma()
ss_.str(dlast);
}
/*
// here we are hitting a g++ bug (Bug 56480)
// compiles fine with clang
// have to wrap in the Grid namespace
@ -76,8 +77,7 @@ void JSONWriter::delete_comma()
namespace Grid
{
template<>
void JSONWriter::writeDefault(const std::string &s,
const std::string &x)
void JSONWriter::writeDefault(const std::string &s, const std::string &x)
{
if (s.size())
ss_ << "\""<< s << "\" : \"" << x << "\" ," ;
@ -85,7 +85,7 @@ namespace Grid
ss_ << "\"" << x << "\" ," ;
}
}// namespace Grid
*/
// Reader implementation ///////////////////////////////////////////////////////
JSONReader::JSONReader(const string &fileName)
@ -140,6 +140,7 @@ void JSONReader::pop(void)
bool JSONReader::nextElement(const std::string &s)
{
// Work in progress
// JSON dictionaries do not support multiple names
// Same name objects must be packed in vectors
++it_;

View File

@ -58,10 +58,13 @@ namespace Grid
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_;
@ -82,6 +85,8 @@ namespace Grid
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
@ -106,7 +111,7 @@ namespace Grid
template <typename U>
void JSONWriter::writeDefault(const std::string &s, const U &x)
{
//std::cout << "JSONWriter::writeDefault(U) : " << s << std::endl;
//std::cout << "JSONWriter::writeDefault(U) : " << s << " " << x <<std::endl;
std::ostringstream os;
os << std::boolalpha << x;
if (s.size())
@ -119,7 +124,7 @@ namespace Grid
template <>
void JSONWriter::writeDefault(const std::string &s, const std::string &x)
{
//std::cout << "JSONWriter::writeDefault(U) : " << s << std::endl;
//std::cout << "JSONWriter::writeDefault(string) : " << s << std::endl;
std::ostringstream os;
os << std::boolalpha << x;
if (s.size())
@ -142,6 +147,18 @@ namespace Grid
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)
{
@ -188,6 +205,30 @@ namespace Grid
}
// 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);
}
template <typename U>
void JSONReader::readDefault(const std::string &s, std::complex<U> &output)
{

View File

@ -29,7 +29,6 @@ Author: Peter Boyle <paboyle@ph.ed.ac.uk>
/* END LEGAL */
#include <Grid/Grid.h>
using namespace Grid;
using namespace Grid::QCD;
@ -152,9 +151,9 @@ int main(int argc,char **argv)
ioTest<TextWriter, TextReader>("iotest.dat", vec, "text (vector of objects)");
ioTest<TextWriter, TextReader>("iotest.dat", pair, "text (pair of objects)");
//// text
ioTest<JSONWriter, JSONReader>("iotest.json", obj, "JSON (object) ");
ioTest<JSONWriter, JSONReader>("iotest.json", vec, "JSON (vector of objects)");
//ioTest<JSONWriter, JSONReader>("iotest.json", pair, "JSON (pair of objects)");
ioTest<JSONWriter, JSONReader>("iotest.json", obj, "JSON (object) ");
ioTest<JSONWriter, JSONReader>("iotest.json", vec, "JSON (vector of objects)");
ioTest<JSONWriter, JSONReader>("iotest.json", pair, "JSON (pair of objects)");
//// HDF5
#undef HAVE_HDF5