mirror of
https://github.com/paboyle/Grid.git
synced 2025-04-04 19:25:56 +01:00
Fixing JSON for complex numbers
This commit is contained in:
parent
4ec746d262
commit
8ba0494485
@ -43,6 +43,7 @@ JSONWriter::~JSONWriter(void)
|
|||||||
cout << ss_.str() << endl;
|
cout << ss_.str() << endl;
|
||||||
// write prettified JSON to file
|
// write prettified JSON to file
|
||||||
std::ofstream os(fileName_);
|
std::ofstream os(fileName_);
|
||||||
|
std::cout << "Writing on file" << std::endl;
|
||||||
os << std::setw(2) << json::parse(ss_.str()) << std::endl;
|
os << std::setw(2) << json::parse(ss_.str()) << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*************************************************************************************
|
/*************************************************************************************
|
||||||
|
|
||||||
Grid physics library, www.github.com/paboyle/Grid
|
Grid physics library, www.github.com/paboyle/Grid
|
||||||
|
|
||||||
Source file: ./lib/serialisation/JSON_IO.h
|
Source file: ./lib/serialisation/JSON_IO.h
|
||||||
|
|
||||||
@ -43,10 +43,10 @@ using json = nlohmann::json;
|
|||||||
|
|
||||||
namespace Grid
|
namespace Grid
|
||||||
{
|
{
|
||||||
|
|
||||||
class JSONWriter: public Writer<JSONWriter>
|
class JSONWriter: public Writer<JSONWriter>
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
JSONWriter(const std::string &fileName);
|
JSONWriter(const std::string &fileName);
|
||||||
virtual ~JSONWriter(void);
|
virtual ~JSONWriter(void);
|
||||||
@ -55,6 +55,8 @@ namespace Grid
|
|||||||
template <typename U>
|
template <typename U>
|
||||||
void writeDefault(const std::string &s, const U &x);
|
void writeDefault(const std::string &s, const U &x);
|
||||||
template <typename U>
|
template <typename U>
|
||||||
|
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);
|
void writeDefault(const std::string &s, const std::vector<U> &x);
|
||||||
|
|
||||||
template<std::size_t N>
|
template<std::size_t N>
|
||||||
@ -65,7 +67,7 @@ namespace Grid
|
|||||||
std::string fileName_;
|
std::string fileName_;
|
||||||
std::ostringstream ss_;
|
std::ostringstream ss_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class JSONReader: public Reader<JSONReader>
|
class JSONReader: public Reader<JSONReader>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -77,6 +79,8 @@ namespace Grid
|
|||||||
template <typename U>
|
template <typename U>
|
||||||
void readDefault(const std::string &s, U &output);
|
void readDefault(const std::string &s, U &output);
|
||||||
template <typename U>
|
template <typename U>
|
||||||
|
void readDefault(const std::string &s, std::complex<U> &output);
|
||||||
|
template <typename U>
|
||||||
void readDefault(const std::string &s, std::vector<U> &output);
|
void readDefault(const std::string &s, std::vector<U> &output);
|
||||||
private:
|
private:
|
||||||
json jobject_; // main object
|
json jobject_; // main object
|
||||||
@ -97,11 +101,12 @@ namespace Grid
|
|||||||
struct isWriter< JSONWriter > {
|
struct isWriter< JSONWriter > {
|
||||||
static const bool value = true;
|
static const bool value = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Writer template implementation ////////////////////////////////////////////
|
// Writer template implementation ////////////////////////////////////////////
|
||||||
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::ostringstream os;
|
std::ostringstream os;
|
||||||
os << std::boolalpha << x;
|
os << std::boolalpha << x;
|
||||||
if (s.size())
|
if (s.size())
|
||||||
@ -110,9 +115,23 @@ namespace Grid
|
|||||||
ss_ << os.str() << " ," ;
|
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::ostringstream os;
|
||||||
|
os << "["<< std::boolalpha << x.real() << ", " << x.imag() << "]";
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
|
std::cout << "JSONReader::writeDefault(vec U) : " << s << std::endl;
|
||||||
|
|
||||||
if (s.size())
|
if (s.size())
|
||||||
ss_ << " \""<<s<<"\" : [";
|
ss_ << " \""<<s<<"\" : [";
|
||||||
else
|
else
|
||||||
@ -124,13 +143,15 @@ namespace Grid
|
|||||||
delete_comma();
|
delete_comma();
|
||||||
ss_<< "],";
|
ss_<< "],";
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
if (s.size())
|
if (s.size())
|
||||||
ss_ << "\""<< s << "\" : \"" << x << "\" ," ;
|
ss_ << "\""<< s << "\" : \"" << x << "\" ," ;
|
||||||
else
|
else
|
||||||
ss_ << "\"" << x << "\" ," ;
|
ss_ << "\"" << x << "\" ," ;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reader template implementation ////////////////////////////////////////////
|
// Reader template implementation ////////////////////////////////////////////
|
||||||
@ -138,7 +159,7 @@ namespace Grid
|
|||||||
void JSONReader::readDefault(const std::string &s, U &output)
|
void JSONReader::readDefault(const std::string &s, U &output)
|
||||||
{
|
{
|
||||||
std::cout << "JSONReader::readDefault(U) : " << s << " : "<< jcur_ << std::endl;
|
std::cout << "JSONReader::readDefault(U) : " << s << " : "<< jcur_ << std::endl;
|
||||||
|
|
||||||
if (s.size()){
|
if (s.size()){
|
||||||
std::cout << "String: "<< jcur_[s] << std::endl;
|
std::cout << "String: "<< jcur_[s] << std::endl;
|
||||||
output = jcur_[s];
|
output = jcur_[s];
|
||||||
@ -146,15 +167,31 @@ namespace Grid
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
std::cout << "String: "<< jcur_ << std::endl;
|
std::cout << "String: "<< jcur_ << std::endl;
|
||||||
output = jcur_;
|
output = jcur_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename U>
|
||||||
|
void JSONReader::readDefault(const std::string &s, std::complex<U> &output)
|
||||||
|
{
|
||||||
|
U tmp1, tmp2;
|
||||||
|
std::cout << "JSONReader::readDefault( complex U) : " << s << " : "<< jcur_ << std::endl;
|
||||||
|
json j = jcur_;
|
||||||
|
json::iterator it = j.begin();
|
||||||
|
jcur_ = *it;
|
||||||
|
read("", tmp1);
|
||||||
|
it++;
|
||||||
|
jcur_ = *it;
|
||||||
|
read("", tmp2);
|
||||||
|
output = std::complex<U>(tmp1,tmp2);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void JSONReader::readDefault(const std::string &s, std::string &output);
|
void JSONReader::readDefault(const std::string &s, std::string &output);
|
||||||
|
|
||||||
template <typename U>
|
template <typename U>
|
||||||
void JSONReader::readDefault(const std::string &s, std::vector<U> &output)
|
void JSONReader::readDefault(const std::string &s, std::vector<U> &output)
|
||||||
{
|
{
|
||||||
@ -163,7 +200,7 @@ namespace Grid
|
|||||||
std::cout << "JSONReader::readDefault(vec) : " << jcur_ << std::endl;
|
std::cout << "JSONReader::readDefault(vec) : " << jcur_ << std::endl;
|
||||||
if (s.size())
|
if (s.size())
|
||||||
push(s);
|
push(s);
|
||||||
|
|
||||||
json j = jcur_;
|
json j = jcur_;
|
||||||
for (json::iterator it = j.begin(); it != j.end(); ++it) {
|
for (json::iterator it = j.begin(); it != j.end(); ++it) {
|
||||||
jcur_ = *it;
|
jcur_ = *it;
|
||||||
@ -171,12 +208,12 @@ namespace Grid
|
|||||||
output.resize(i + 1);
|
output.resize(i + 1);
|
||||||
read("", output[i++]);
|
read("", output[i++]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
jcur_ = j;
|
jcur_ = j;
|
||||||
if (s.size())
|
if (s.size())
|
||||||
pop();
|
pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -214,6 +214,7 @@ int main(int argc,char **argv)
|
|||||||
|
|
||||||
// test serializable class writing
|
// test serializable class writing
|
||||||
myclass obj(1234); // non-trivial constructor
|
myclass obj(1234); // non-trivial constructor
|
||||||
|
std::cout << "-- serialisable class writing to 'bother.json'..." << std::endl;
|
||||||
write(JW,"obj",obj);
|
write(JW,"obj",obj);
|
||||||
JW.write("obj2", obj);
|
JW.write("obj2", obj);
|
||||||
|
|
||||||
@ -232,11 +233,11 @@ int main(int argc,char **argv)
|
|||||||
myclass jcopy1;
|
myclass jcopy1;
|
||||||
std::vector<myclass> jveccopy1;
|
std::vector<myclass> jveccopy1;
|
||||||
read(RD,"obj",jcopy1);
|
read(RD,"obj",jcopy1);
|
||||||
read(RD,"objvec", jveccopy1);
|
//read(RD,"objvec", jveccopy1);
|
||||||
std::cout << "Loaded (JSON) -----------------" << std::endl;
|
//std::cout << "Loaded (JSON) -----------------" << std::endl;
|
||||||
std::cout << jcopy1 << std::endl << jveccopy1 << std::endl;
|
//std::cout << jcopy1 << std::endl << jveccopy1 << std::endl;
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
{
|
{
|
||||||
// Testing the next element function
|
// Testing the next element function
|
||||||
JSONReader RD("test.json");
|
JSONReader RD("test.json");
|
||||||
@ -245,7 +246,7 @@ int main(int argc,char **argv)
|
|||||||
std::string name;
|
std::string name;
|
||||||
read(RD,"name", name);
|
read(RD,"name", name);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user