1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-13 01:05:36 +00:00

Fixed the JSON parsing error

This commit is contained in:
Guido Cossu 2017-07-11 14:31:57 +01:00
parent d9593c4b81
commit 097c9637ee
5 changed files with 5815 additions and 4211 deletions

File diff suppressed because it is too large Load Diff

View File

@ -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

View File

@ -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_ << "},";
}

View File

@ -106,7 +106,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 << std::endl;
std::ostringstream os;
os << std::boolalpha << x;
if (s.size())
@ -115,10 +115,25 @@ namespace Grid
ss_ << os.str() << " ," ;
}
// specialize for string
template <>
void JSONWriter::writeDefault(const std::string &s, const std::string &x)
{
//std::cout << "JSONWriter::writeDefault(U) : " << s << std::endl;
std::ostringstream os;
os << std::boolalpha << x;
if (s.size())
ss_ << "\""<< s << "\" : \"" << os.str() << "\" ," ;
else
ss_ << os.str() << " ," ;
}
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())
@ -130,7 +145,7 @@ namespace Grid
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 +161,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 << "\" ," ;
@ -177,7 +192,7 @@ namespace Grid
void JSONReader::readDefault(const std::string &s, std::complex<U> &output)
{
U tmp1, tmp2;
//std::cout << "JSONReader::readDefault( complex U) : " << s << " : "<< jcur_ << std::endl;
//std::cout << "JSONReader::readDefault(complex U) : " << s << " : "<< jcur_ << std::endl;
json j = jcur_;
json::iterator it = j.begin();
jcur_ = *it;

View File

@ -154,7 +154,7 @@ int main(int argc,char **argv)
//// 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", pair, "JSON (pair of objects)");
//// HDF5
#undef HAVE_HDF5
@ -199,50 +199,55 @@ int main(int argc,char **argv)
std::cout << std::endl;
// std::cout << ".:::::: Testing JSON classes "<< std::endl;
//
//
// {
// JSONWriter JW("bother.json");
//
// // test basic type writing
// push(JW,"BasicTypes");
// write(JW,std::string("i16"),i16);
// write(JW,"u16",u16);
// write(JW,"i32",i32);
// write(JW,"u32",u32);
// write(JW,"i64",i64);
// write(JW,"u64",u64);
// write(JW,"f",f);
// write(JW,"d",d);
// write(JW,"b",b);
// pop(JW);
//
// // test serializable class writing
// myclass obj(1234); // non-trivial constructor
// 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));
// vec.push_back(myclass(5678));
// vec.push_back(myclass(3838));
// write(JW, "objvec", vec);
//
// }
//
// {
// JSONReader RD("bother.json");
// myclass jcopy1;
// std::vector<myclass> jveccopy1;
// read(RD,"obj",jcopy1);
// read(RD,"objvec", jveccopy1);
// std::cout << "Loaded (JSON) -----------------" << std::endl;
// std::cout << jcopy1 << std::endl << jveccopy1 << std::endl;
// }
std::cout << ".:::::: Testing JSON classes "<< std::endl;
{
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);
write(JW,"i64",i64);
write(JW,"u64",u64);
write(JW,"f",f);
write(JW,"d",d);
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::vector<myclass> vec;
vec.push_back(myclass(1234));
vec.push_back(myclass(5678));
vec.push_back(myclass(3838));
write(JW, "objvec", vec);
}
{
JSONReader RD("bother.json");
myclass jcopy1;
std::vector<myclass> jveccopy1;
read(RD,"obj",jcopy1);
read(RD,"objvec", jveccopy1);
std::cout << "Loaded (JSON) -----------------" << std::endl;
std::cout << jcopy1 << std::endl << jveccopy1 << std::endl;
}
/*
// This is still work in progress