mirror of
https://github.com/paboyle/Grid.git
synced 2025-04-09 21:50:45 +01:00
Fixed the JSON parsing error
This commit is contained in:
parent
d9593c4b81
commit
097c9637ee
9862
lib/json/json.hpp
9862
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;
|
or element<T>::is_number;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Vector flatening utility class ////////////////////////////////////////////
|
// Vector flattening utility class ////////////////////////////////////////////
|
||||||
// Class to flatten a multidimensional std::vector
|
// Class to flatten a multidimensional std::vector
|
||||||
template <typename V>
|
template <typename V>
|
||||||
class Flatten
|
class Flatten
|
||||||
|
@ -42,6 +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;
|
||||||
os << std::setw(2) << json::parse(ss_.str()) << 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)
|
void JSONWriter::pop(void)
|
||||||
{
|
{
|
||||||
|
std::cout << "JSONWriter::pop" << std::endl;
|
||||||
delete_comma();
|
delete_comma();
|
||||||
ss_ << "},";
|
ss_ << "},";
|
||||||
}
|
}
|
||||||
|
@ -106,7 +106,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 << "JSONReader::writeDefault(U) : " << s << std::endl;
|
//std::cout << "JSONWriter::writeDefault(U) : " << s << std::endl;
|
||||||
std::ostringstream os;
|
std::ostringstream os;
|
||||||
os << std::boolalpha << x;
|
os << std::boolalpha << x;
|
||||||
if (s.size())
|
if (s.size())
|
||||||
@ -115,10 +115,25 @@ namespace Grid
|
|||||||
ss_ << os.str() << " ," ;
|
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>
|
template <typename U>
|
||||||
void JSONWriter::writeDefault(const std::string &s, const std::complex<U> &x)
|
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;
|
std::ostringstream os;
|
||||||
os << "["<< std::boolalpha << x.real() << ", " << x.imag() << "]";
|
os << "["<< std::boolalpha << x.real() << ", " << x.imag() << "]";
|
||||||
if (s.size())
|
if (s.size())
|
||||||
@ -130,7 +145,7 @@ namespace Grid
|
|||||||
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)
|
||||||
{
|
{
|
||||||
//std::cout << "JSONReader::writeDefault(vec U) : " << s << std::endl;
|
//std::cout << "JSONWriter::writeDefault(vec U) : " << s << std::endl;
|
||||||
|
|
||||||
if (s.size())
|
if (s.size())
|
||||||
ss_ << " \""<<s<<"\" : [";
|
ss_ << " \""<<s<<"\" : [";
|
||||||
@ -146,12 +161,12 @@ namespace Grid
|
|||||||
|
|
||||||
template<std::size_t N>
|
template<std::size_t N>
|
||||||
void JSONWriter::writeDefault(const std::string &s, const char(&x)[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())
|
if (s.size())
|
||||||
ss_ << "\""<< s << "\" : \"" << x << "\" ," ;
|
ss_ << "\""<< s << "\" : \"" << x << "\" ," ;
|
||||||
else
|
else
|
||||||
ss_ << "\"" << x << "\" ," ;
|
ss_ << "\"" << x << "\" ," ;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reader template implementation ////////////////////////////////////////////
|
// Reader template implementation ////////////////////////////////////////////
|
||||||
@ -177,7 +192,7 @@ namespace Grid
|
|||||||
void JSONReader::readDefault(const std::string &s, std::complex<U> &output)
|
void JSONReader::readDefault(const std::string &s, std::complex<U> &output)
|
||||||
{
|
{
|
||||||
U tmp1, tmp2;
|
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 j = jcur_;
|
||||||
json::iterator it = j.begin();
|
json::iterator it = j.begin();
|
||||||
jcur_ = *it;
|
jcur_ = *it;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*************************************************************************************
|
/*************************************************************************************
|
||||||
|
|
||||||
Grid physics library, www.github.com/paboyle/Grid
|
Grid physics library, www.github.com/paboyle/Grid
|
||||||
|
|
||||||
Source file: ./tests/Test_serialisation.cc
|
Source file: ./tests/Test_serialisation.cc
|
||||||
|
|
||||||
@ -34,7 +34,7 @@ using namespace Grid;
|
|||||||
using namespace Grid::QCD;
|
using namespace Grid::QCD;
|
||||||
|
|
||||||
GRID_SERIALIZABLE_ENUM(myenum, undef, red, 1, blue, 2, green, 3);
|
GRID_SERIALIZABLE_ENUM(myenum, undef, red, 1, blue, 2, green, 3);
|
||||||
|
|
||||||
class myclass: Serializable {
|
class myclass: Serializable {
|
||||||
public:
|
public:
|
||||||
GRID_SERIALIZABLE_CLASS_MEMBERS(myclass,
|
GRID_SERIALIZABLE_CLASS_MEMBERS(myclass,
|
||||||
@ -79,14 +79,14 @@ void ioTest(const std::string &filename, const O &object, const std::string &nam
|
|||||||
// writer needs to be destroyed so that writing physically happens
|
// writer needs to be destroyed so that writing physically happens
|
||||||
{
|
{
|
||||||
W writer(filename);
|
W writer(filename);
|
||||||
|
|
||||||
write(writer, "testobject", object);
|
write(writer, "testobject", object);
|
||||||
}
|
}
|
||||||
|
|
||||||
R reader(filename);
|
R reader(filename);
|
||||||
O buf;
|
O buf;
|
||||||
bool good;
|
bool good;
|
||||||
|
|
||||||
read(reader, "testobject", buf);
|
read(reader, "testobject", buf);
|
||||||
good = (object == buf);
|
good = (object == buf);
|
||||||
std::cout << name << " IO test: " << (good ? "success" : "failure");
|
std::cout << name << " IO test: " << (good ? "success" : "failure");
|
||||||
@ -98,7 +98,7 @@ int main(int argc,char **argv)
|
|||||||
{
|
{
|
||||||
std::cout << "==== basic IO" << std::endl;
|
std::cout << "==== basic IO" << std::endl;
|
||||||
XmlWriter WR("bother.xml");
|
XmlWriter WR("bother.xml");
|
||||||
|
|
||||||
// test basic type writing
|
// test basic type writing
|
||||||
std::cout << "-- basic writing to 'bother.xml'..." << std::endl;
|
std::cout << "-- basic writing to 'bother.xml'..." << std::endl;
|
||||||
push(WR,"BasicTypes");
|
push(WR,"BasicTypes");
|
||||||
@ -112,12 +112,12 @@ int main(int argc,char **argv)
|
|||||||
write(WR,"d",d);
|
write(WR,"d",d);
|
||||||
write(WR,"b",b);
|
write(WR,"b",b);
|
||||||
pop(WR);
|
pop(WR);
|
||||||
|
|
||||||
// test serializable class writing
|
// test serializable class writing
|
||||||
myclass obj(1234); // non-trivial constructor
|
myclass obj(1234); // non-trivial constructor
|
||||||
std::vector<myclass> vec;
|
std::vector<myclass> vec;
|
||||||
std::pair<myenum, myenum> pair;
|
std::pair<myenum, myenum> pair;
|
||||||
|
|
||||||
std::cout << "-- serialisable class writing to 'bother.xml'..." << std::endl;
|
std::cout << "-- serialisable class writing to 'bother.xml'..." << std::endl;
|
||||||
write(WR,"obj",obj);
|
write(WR,"obj",obj);
|
||||||
WR.write("obj2", obj);
|
WR.write("obj2", obj);
|
||||||
@ -132,11 +132,11 @@ int main(int argc,char **argv)
|
|||||||
std::cout << "-- serialisable class comparison:" << std::endl;
|
std::cout << "-- serialisable class comparison:" << std::endl;
|
||||||
std::cout << "vec[0] == obj: " << ((vec[0] == obj) ? "true" : "false") << std::endl;
|
std::cout << "vec[0] == obj: " << ((vec[0] == obj) ? "true" : "false") << std::endl;
|
||||||
std::cout << "vec[1] == obj: " << ((vec[1] == obj) ? "true" : "false") << std::endl;
|
std::cout << "vec[1] == obj: " << ((vec[1] == obj) ? "true" : "false") << std::endl;
|
||||||
|
|
||||||
write(WR, "objpair", pair);
|
write(WR, "objpair", pair);
|
||||||
std::cout << "-- pair writing to std::cout:" << std::endl;
|
std::cout << "-- pair writing to std::cout:" << std::endl;
|
||||||
std::cout << pair << std::endl;
|
std::cout << pair << std::endl;
|
||||||
|
|
||||||
// read tests
|
// read tests
|
||||||
std::cout << "\n==== IO self-consistency tests" << std::endl;
|
std::cout << "\n==== IO self-consistency tests" << std::endl;
|
||||||
//// XML
|
//// XML
|
||||||
@ -154,7 +154,7 @@ int main(int argc,char **argv)
|
|||||||
//// 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
|
||||||
@ -163,13 +163,13 @@ int main(int argc,char **argv)
|
|||||||
ioTest<Hdf5Writer, Hdf5Reader>("iotest.h5", vec, "HDF5 (vector of objects)");
|
ioTest<Hdf5Writer, Hdf5Reader>("iotest.h5", vec, "HDF5 (vector of objects)");
|
||||||
ioTest<Hdf5Writer, Hdf5Reader>("iotest.h5", pair, "HDF5 (pair of objects)");
|
ioTest<Hdf5Writer, Hdf5Reader>("iotest.h5", pair, "HDF5 (pair of objects)");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
std::cout << "\n==== vector flattening/reconstruction" << std::endl;
|
std::cout << "\n==== vector flattening/reconstruction" << std::endl;
|
||||||
typedef std::vector<std::vector<std::vector<double>>> vec3d;
|
typedef std::vector<std::vector<std::vector<double>>> vec3d;
|
||||||
|
|
||||||
vec3d dv, buf;
|
vec3d dv, buf;
|
||||||
double d = 0.;
|
double d = 0.;
|
||||||
|
|
||||||
dv.resize(4);
|
dv.resize(4);
|
||||||
for (auto &v1: dv)
|
for (auto &v1: dv)
|
||||||
{
|
{
|
||||||
@ -185,66 +185,71 @@ int main(int argc,char **argv)
|
|||||||
}
|
}
|
||||||
std::cout << "original 3D vector:" << std::endl;
|
std::cout << "original 3D vector:" << std::endl;
|
||||||
std::cout << dv << std::endl;
|
std::cout << dv << std::endl;
|
||||||
|
|
||||||
Flatten<vec3d> flatdv(dv);
|
Flatten<vec3d> flatdv(dv);
|
||||||
|
|
||||||
std::cout << "\ndimensions:" << std::endl;
|
std::cout << "\ndimensions:" << std::endl;
|
||||||
std::cout << flatdv.getDim() << std::endl;
|
std::cout << flatdv.getDim() << std::endl;
|
||||||
std::cout << "\nflattened vector:" << std::endl;
|
std::cout << "\nflattened vector:" << std::endl;
|
||||||
std::cout << flatdv.getFlatVector() << std::endl;
|
std::cout << flatdv.getFlatVector() << std::endl;
|
||||||
|
|
||||||
Reconstruct<vec3d> rec(flatdv.getFlatVector(), flatdv.getDim());
|
Reconstruct<vec3d> rec(flatdv.getFlatVector(), flatdv.getDim());
|
||||||
std::cout << "\nreconstructed vector:" << std::endl;
|
std::cout << "\nreconstructed vector:" << std::endl;
|
||||||
std::cout << flatdv.getVector() << std::endl;
|
std::cout << flatdv.getVector() << std::endl;
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
|
||||||
// std::cout << ".:::::: Testing JSON classes "<< std::endl;
|
std::cout << ".:::::: Testing JSON classes "<< std::endl;
|
||||||
//
|
|
||||||
//
|
|
||||||
// {
|
{
|
||||||
// JSONWriter JW("bother.json");
|
JSONWriter JW("bother.json");
|
||||||
//
|
|
||||||
// // test basic type writing
|
// test basic type writing
|
||||||
// push(JW,"BasicTypes");
|
myenum a = myenum::red;
|
||||||
// write(JW,std::string("i16"),i16);
|
push(JW,"BasicTypes");
|
||||||
// write(JW,"u16",u16);
|
write(JW,std::string("i16"),i16);
|
||||||
// write(JW,"i32",i32);
|
write(JW,"myenum",a);
|
||||||
// write(JW,"u32",u32);
|
write(JW,"u16",u16);
|
||||||
// write(JW,"i64",i64);
|
write(JW,"i32",i32);
|
||||||
// write(JW,"u64",u64);
|
write(JW,"u32",u32);
|
||||||
// write(JW,"f",f);
|
write(JW,"i64",i64);
|
||||||
// write(JW,"d",d);
|
write(JW,"u64",u64);
|
||||||
// write(JW,"b",b);
|
write(JW,"f",f);
|
||||||
// pop(JW);
|
write(JW,"d",d);
|
||||||
//
|
write(JW,"b",b);
|
||||||
// // test serializable class writing
|
pop(JW);
|
||||||
// myclass obj(1234); // non-trivial constructor
|
|
||||||
// std::cout << "-- serialisable class writing to 'bother.json'..." << std::endl;
|
|
||||||
// write(JW,"obj",obj);
|
// test serializable class writing
|
||||||
// JW.write("obj2", obj);
|
myclass obj(1234); // non-trivial constructor
|
||||||
//
|
std::cout << obj << std::endl;
|
||||||
// std::cout << obj << std::endl;
|
std::cout << "-- serialisable class writing to 'bother.json'..." << std::endl;
|
||||||
//
|
write(JW,"obj",obj);
|
||||||
// std::vector<myclass> vec;
|
JW.write("obj2", obj);
|
||||||
// vec.push_back(myclass(1234));
|
|
||||||
// vec.push_back(myclass(5678));
|
|
||||||
// vec.push_back(myclass(3838));
|
std::vector<myclass> vec;
|
||||||
// write(JW, "objvec", 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);
|
JSONReader RD("bother.json");
|
||||||
// std::cout << "Loaded (JSON) -----------------" << std::endl;
|
myclass jcopy1;
|
||||||
// std::cout << jcopy1 << std::endl << jveccopy1 << std::endl;
|
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
|
// This is still work in progress
|
||||||
{
|
{
|
||||||
// Testing the next element function
|
// Testing the next element function
|
||||||
|
Loading…
x
Reference in New Issue
Block a user