2017-01-23 14:57:38 +00:00
|
|
|
/*************************************************************************************
|
|
|
|
|
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
|
|
|
|
|
|
|
Source file: ./lib/serialisation/JSON_IO.cc
|
|
|
|
|
|
|
|
Copyright (C) 2016
|
|
|
|
|
|
|
|
Author: Guido Cossu<guido.cossu@ed.ac.uk>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
See the full license in the file "LICENSE" in the top level distribution directory
|
|
|
|
*************************************************************************************/
|
|
|
|
/* END LEGAL */
|
2017-12-01 19:44:07 +00:00
|
|
|
#include <Grid/Grid.h>
|
2017-01-23 14:57:38 +00:00
|
|
|
|
|
|
|
using namespace Grid;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
// Writer implementation ///////////////////////////////////////////////////////
|
|
|
|
JSONWriter::JSONWriter(const string &fileName)
|
|
|
|
: fileName_(fileName), ss_("{ ", ostringstream::ate){}
|
|
|
|
|
|
|
|
JSONWriter::~JSONWriter(void)
|
|
|
|
{
|
|
|
|
// close
|
|
|
|
delete_comma();
|
|
|
|
ss_ << "}";
|
|
|
|
|
|
|
|
// write prettified JSON to file
|
|
|
|
std::ofstream os(fileName_);
|
2017-07-12 14:44:53 +01:00
|
|
|
//std::cout << "JSONWriter::~JSONWriter" << std::endl;
|
2017-01-23 14:57:38 +00:00
|
|
|
os << std::setw(2) << json::parse(ss_.str()) << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSONWriter::push(const string &s)
|
|
|
|
{
|
|
|
|
// adding a nested object
|
|
|
|
if (s.size())
|
|
|
|
ss_ << " \""<<s<<"\" : {";
|
|
|
|
else
|
|
|
|
ss_ << " {";
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSONWriter::pop(void)
|
|
|
|
{
|
2017-07-12 14:44:53 +01:00
|
|
|
//std::cout << "JSONWriter::pop" << std::endl;
|
2017-01-23 14:57:38 +00:00
|
|
|
delete_comma();
|
|
|
|
ss_ << "},";
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSONWriter::delete_comma()
|
|
|
|
{
|
|
|
|
std::string dlast = ss_.str();
|
|
|
|
dlast.pop_back(); // deletes the last comma
|
|
|
|
ss_.str(dlast);
|
|
|
|
}
|
|
|
|
|
2017-07-12 14:55:42 +01:00
|
|
|
|
2017-01-25 12:11:58 +00:00
|
|
|
// 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
|
|
|
|
{
|
2017-07-12 14:44:53 +01:00
|
|
|
void JSONWriter::writeDefault(const std::string &s, const std::string &x)
|
2017-01-25 12:11:58 +00:00
|
|
|
{
|
2017-09-08 15:18:59 +01:00
|
|
|
//std::cout << "JSONWriter::writeDefault(string) : " << s << std::endl;
|
2017-07-12 14:55:42 +01:00
|
|
|
std::ostringstream os;
|
|
|
|
os << std::boolalpha << x;
|
2017-01-25 12:11:58 +00:00
|
|
|
if (s.size())
|
2017-07-12 14:55:42 +01:00
|
|
|
ss_ << "\""<< s << "\" : \"" << os.str() << "\" ," ;
|
2017-01-25 12:11:58 +00:00
|
|
|
else
|
2017-07-12 14:55:42 +01:00
|
|
|
ss_ << os.str() << " ," ;
|
2017-01-25 12:11:58 +00:00
|
|
|
}
|
|
|
|
}// namespace Grid
|
2017-07-12 14:55:42 +01:00
|
|
|
|
2017-01-23 14:57:38 +00:00
|
|
|
|
|
|
|
// Reader implementation ///////////////////////////////////////////////////////
|
|
|
|
JSONReader::JSONReader(const string &fileName)
|
|
|
|
: fileName_(fileName)
|
|
|
|
{
|
|
|
|
std::ifstream file(fileName_);
|
|
|
|
file >> jobject_;
|
|
|
|
|
|
|
|
// test
|
|
|
|
// serialize to standard output
|
2017-05-08 21:56:44 +01:00
|
|
|
//std::cout << "JSONReader::JSONReader : " << jobject_ << endl;
|
2017-01-23 14:57:38 +00:00
|
|
|
jcur_ = jobject_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JSONReader::push(const string &s)
|
|
|
|
{
|
|
|
|
if (s.size()){
|
|
|
|
jold_.push_back(jcur_);
|
|
|
|
do_pop.push_back(true);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
jcur_ = jcur_[s];
|
|
|
|
}
|
|
|
|
catch (std::out_of_range& e)
|
|
|
|
{
|
|
|
|
std::cout << "out of range: " << e.what() << '\n';
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-08 21:56:44 +01:00
|
|
|
//cout << "JSONReader::push : " << s << " : "<< jcur_ << endl;
|
2017-01-23 14:57:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
do_pop.push_back(false);
|
|
|
|
}
|
2017-01-24 13:57:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
return true;
|
2017-01-23 14:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JSONReader::pop(void)
|
|
|
|
{
|
|
|
|
if (do_pop.back()){
|
|
|
|
jcur_ = jold_.back();
|
|
|
|
jold_.pop_back();
|
|
|
|
do_pop.pop_back();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
do_pop.pop_back();
|
|
|
|
|
2017-05-08 21:56:44 +01:00
|
|
|
//cout << "JSONReader::pop : " << jcur_ << endl;
|
2017-01-23 14:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JSONReader::nextElement(const std::string &s)
|
|
|
|
{
|
2017-07-12 14:44:53 +01:00
|
|
|
// Work in progress
|
2017-01-23 14:57:38 +00:00
|
|
|
// JSON dictionaries do not support multiple names
|
|
|
|
// Same name objects must be packed in vectors
|
2017-01-24 13:57:32 +00:00
|
|
|
++it_;
|
|
|
|
|
|
|
|
//if (it_ == it_end_){
|
|
|
|
// return false;
|
|
|
|
//}
|
|
|
|
|
|
|
|
jcur_ = *it_;
|
|
|
|
//cout << "JSONReader::nextElement(string) : " << s << " : "<< jcur_ << endl;
|
|
|
|
//return true;
|
|
|
|
|
|
|
|
return false;
|
2017-01-23 14:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void JSONReader::readDefault(const string &s, string &output)
|
|
|
|
{
|
2017-05-08 21:56:44 +01:00
|
|
|
//cout << "JSONReader::readDefault(string) : " << s<< " " << jcur_ << endl;
|
2017-01-23 14:57:38 +00:00
|
|
|
if (s.size()){
|
2017-05-08 21:56:44 +01:00
|
|
|
//std::cout << "String: "<< jcur_[s] << std::endl;
|
2017-01-23 14:57:38 +00:00
|
|
|
output = jcur_[s];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-08 21:56:44 +01:00
|
|
|
//std::cout << "String: "<< jcur_ << std::endl;
|
2017-01-23 14:57:38 +00:00
|
|
|
output = jcur_;
|
|
|
|
}
|
|
|
|
}
|