mirror of
https://github.com/paboyle/Grid.git
synced 2024-11-13 01:05:36 +00:00
Merge branch 'feature/json-fix' into develop
This commit is contained in:
commit
bbaf1ada91
16178
lib/json/json.hpp
16178
lib/json/json.hpp
File diff suppressed because it is too large
Load Diff
@ -86,7 +86,7 @@ namespace Grid {
|
||||
or element<T>::is_number;
|
||||
};
|
||||
|
||||
// Vector flatening utility class ////////////////////////////////////////////
|
||||
// Vector flattening utility class ////////////////////////////////////////////
|
||||
// Class to flatten a multidimensional std::vector
|
||||
template <typename V>
|
||||
class Flatten
|
||||
|
@ -42,6 +42,7 @@ JSONWriter::~JSONWriter(void)
|
||||
|
||||
// write prettified JSON to file
|
||||
std::ofstream os(fileName_);
|
||||
//std::cout << "JSONWriter::~JSONWriter" << std::endl;
|
||||
os << std::setw(2) << json::parse(ss_.str()) << std::endl;
|
||||
}
|
||||
|
||||
@ -56,6 +57,7 @@ void JSONWriter::push(const string &s)
|
||||
|
||||
void JSONWriter::pop(void)
|
||||
{
|
||||
//std::cout << "JSONWriter::pop" << std::endl;
|
||||
delete_comma();
|
||||
ss_ << "},";
|
||||
}
|
||||
@ -67,20 +69,22 @@ 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
|
||||
// annoying, but necessary for TravisCI
|
||||
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)
|
||||
{
|
||||
//std::cout << "JSONWriter::writeDefault(string) : " << s << std::endl;
|
||||
std::ostringstream os;
|
||||
os << std::boolalpha << x;
|
||||
if (s.size())
|
||||
ss_ << "\""<< s << "\" : \"" << x << "\" ," ;
|
||||
ss_ << "\""<< s << "\" : \"" << os.str() << "\" ," ;
|
||||
else
|
||||
ss_ << "\"" << x << "\" ," ;
|
||||
ss_ << os.str() << " ," ;
|
||||
}
|
||||
}// namespace Grid
|
||||
|
||||
@ -138,6 +142,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_;
|
||||
|
@ -58,10 +58,15 @@ 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]);
|
||||
|
||||
void writeDefault(const std::string &s, const std::string &x);
|
||||
|
||||
|
||||
private:
|
||||
void delete_comma();
|
||||
std::string fileName_;
|
||||
@ -82,6 +87,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 +113,7 @@ namespace Grid
|
||||
template <typename U>
|
||||
void JSONWriter::writeDefault(const std::string &s, const U &x)
|
||||
{
|
||||
//std::cout << "JSONReader::writeDefault(U) : " << s << std::endl;
|
||||
//std::cout << "JSONWriter::writeDefault(U) : " << s << " " << x <<std::endl;
|
||||
std::ostringstream os;
|
||||
os << std::boolalpha << x;
|
||||
if (s.size())
|
||||
@ -118,7 +125,7 @@ namespace Grid
|
||||
template <typename U>
|
||||
void JSONWriter::writeDefault(const std::string &s, const std::complex<U> &x)
|
||||
{
|
||||
//std::cout << "JSONReader::writeDefault(complex) : " << s << std::endl;
|
||||
//std::cout << "JSONWriter::writeDefault(complex) : " << s << " " << x << std::endl;
|
||||
std::ostringstream os;
|
||||
os << "["<< std::boolalpha << x.real() << ", " << x.imag() << "]";
|
||||
if (s.size())
|
||||
@ -127,10 +134,22 @@ 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)
|
||||
{
|
||||
//std::cout << "JSONReader::writeDefault(vec U) : " << s << std::endl;
|
||||
//std::cout << "JSONWriter::writeDefault(vec U) : " << s << std::endl;
|
||||
|
||||
if (s.size())
|
||||
ss_ << " \""<<s<<"\" : [";
|
||||
@ -146,7 +165,7 @@ namespace Grid
|
||||
|
||||
template<std::size_t N>
|
||||
void JSONWriter::writeDefault(const std::string &s, const char(&x)[N]){
|
||||
//std::cout << "JSONReader::writeDefault(char U) : " << s << std::endl;
|
||||
//std::cout << "JSONWriter::writeDefault(char U) : " << s << " " << x << std::endl;
|
||||
|
||||
if (s.size())
|
||||
ss_ << "\""<< s << "\" : \"" << x << "\" ," ;
|
||||
@ -173,6 +192,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)
|
||||
{
|
||||
|
@ -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;
|
||||
|
||||
@ -151,6 +150,11 @@ int main(int argc,char **argv)
|
||||
ioTest<TextWriter, TextReader>("iotest.dat", obj, "text (object) ");
|
||||
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)");
|
||||
|
||||
//// HDF5
|
||||
#undef HAVE_HDF5
|
||||
#ifdef HAVE_HDF5
|
||||
@ -201,8 +205,10 @@ int main(int argc,char **argv)
|
||||
JSONWriter JW("bother.json");
|
||||
|
||||
// test basic type writing
|
||||
myenum a = myenum::red;
|
||||
push(JW,"BasicTypes");
|
||||
write(JW,std::string("i16"),i16);
|
||||
write(JW,"myenum",a);
|
||||
write(JW,"u16",u16);
|
||||
write(JW,"i32",i32);
|
||||
write(JW,"u32",u32);
|
||||
@ -213,13 +219,14 @@ int main(int argc,char **argv)
|
||||
write(JW,"b",b);
|
||||
pop(JW);
|
||||
|
||||
|
||||
// test serializable class writing
|
||||
myclass obj(1234); // non-trivial constructor
|
||||
std::cout << obj << std::endl;
|
||||
std::cout << "-- serialisable class writing to 'bother.json'..." << std::endl;
|
||||
write(JW,"obj",obj);
|
||||
JW.write("obj2", obj);
|
||||
|
||||
std::cout << obj << std::endl;
|
||||
|
||||
std::vector<myclass> vec;
|
||||
vec.push_back(myclass(1234));
|
||||
@ -229,6 +236,7 @@ int main(int argc,char **argv)
|
||||
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
JSONReader RD("bother.json");
|
||||
myclass jcopy1;
|
||||
@ -239,6 +247,7 @@ int main(int argc,char **argv)
|
||||
std::cout << jcopy1 << std::endl << jveccopy1 << std::endl;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
// This is still work in progress
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user