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

View File

@ -58,10 +58,13 @@ namespace Grid
void writeDefault(const std::string &s, const std::complex<U> &x); void writeDefault(const std::string &s, const std::complex<U> &x);
template <typename U> template <typename U>
void writeDefault(const std::string &s, const std::vector<U> &x); 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> template<std::size_t N>
void writeDefault(const std::string &s, const char(&x)[N]); void writeDefault(const std::string &s, const char(&x)[N]);
private: private:
void delete_comma(); void delete_comma();
std::string fileName_; std::string fileName_;
@ -82,6 +85,8 @@ namespace Grid
void readDefault(const std::string &s, std::complex<U> &output); void readDefault(const std::string &s, std::complex<U> &output);
template <typename U> template <typename U>
void readDefault(const std::string &s, std::vector<U> &output); 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: private:
json jobject_; // main object json jobject_; // main object
json jcur_; // current json object json jcur_; // current json object
@ -106,7 +111,7 @@ namespace Grid
template <typename U> template <typename U>
void JSONWriter::writeDefault(const std::string &s, const U &x) 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; std::ostringstream os;
os << std::boolalpha << x; os << std::boolalpha << x;
if (s.size()) if (s.size())
@ -119,7 +124,7 @@ namespace Grid
template <> template <>
void JSONWriter::writeDefault(const std::string &s, const std::string &x) 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; std::ostringstream os;
os << std::boolalpha << x; os << std::boolalpha << x;
if (s.size()) if (s.size())
@ -142,6 +147,18 @@ namespace Grid
ss_ << os.str() << " ," ; 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> template <typename U>
void JSONWriter::writeDefault(const std::string &s, const std::vector<U> &x) 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> template <typename U>
void JSONReader::readDefault(const std::string &s, std::complex<U> &output) 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 */ /* END LEGAL */
#include <Grid/Grid.h> #include <Grid/Grid.h>
using namespace Grid; using namespace Grid;
using namespace Grid::QCD; 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", vec, "text (vector of objects)");
ioTest<TextWriter, TextReader>("iotest.dat", pair, "text (pair of objects)"); ioTest<TextWriter, TextReader>("iotest.dat", pair, "text (pair of objects)");
//// text //// text
ioTest<JSONWriter, JSONReader>("iotest.json", obj, "JSON (object) "); ioTest<JSONWriter, JSONReader>("iotest.json", obj, "JSON (object) ");
ioTest<JSONWriter, JSONReader>("iotest.json", vec, "JSON (vector of objects)"); 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", pair, "JSON (pair of objects)");
//// HDF5 //// HDF5
#undef HAVE_HDF5 #undef HAVE_HDF5