mirror of
				https://github.com/paboyle/Grid.git
				synced 2025-11-04 05:54:32 +00:00 
			
		
		
		
	Added JSON parser (without NextElement)
This commit is contained in:
		
							
								
								
									
										12275
									
								
								lib/json/json.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12275
									
								
								lib/json/json.hpp
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@@ -144,7 +144,6 @@ namespace Grid {
 | 
			
		||||
      //
 | 
			
		||||
      //////////////////////////////////////////////////////
 | 
			
		||||
      virtual void deriv(const GaugeField &U,GaugeField & dSdU) {
 | 
			
		||||
      	std::cout << GridLogDebug << "Calling deriv" << std::endl;
 | 
			
		||||
	FermOp.ImportGauge(U);
 | 
			
		||||
 | 
			
		||||
	FermionField X(FermOp.FermionRedBlackGrid());
 | 
			
		||||
@@ -158,16 +157,9 @@ namespace Grid {
 | 
			
		||||
 | 
			
		||||
	X=zero;
 | 
			
		||||
	DerivativeSolver(Mpc,PhiOdd,X);
 | 
			
		||||
        std::cout << GridLogDebug << "Calling deriv 2 " << std::endl;
 | 
			
		||||
	Mpc.Mpc(X,Y);
 | 
			
		||||
                std::cout << GridLogDebug << "Calling deriv 3 " << std::endl;
 | 
			
		||||
  	Mpc.MpcDeriv(tmp , Y, X );  
 | 
			
		||||
                   std::cout << GridLogDebug << "Calling deriv 4 " << std::endl;
 | 
			
		||||
          dSdU=tmp;
 | 
			
		||||
                std::cout << GridLogDebug << "Calling deriv 5 " << std::endl;
 | 
			
		||||
	Mpc.MpcDagDeriv(tmp , X, Y);  dSdU=dSdU+tmp;
 | 
			
		||||
 | 
			
		||||
        std::cout << GridLogDebug << "Calling deriv 6" << std::endl;
 | 
			
		||||
  	Mpc.MpcDeriv(tmp , Y, X );    dSdU=tmp;
 | 
			
		||||
        Mpc.MpcDagDeriv(tmp , X, Y);  dSdU=dSdU+tmp;
 | 
			
		||||
 | 
			
		||||
	// Treat the EE case. (MdagM)^-1 = Minv Minvdag
 | 
			
		||||
	// Deriv defaults to zero.
 | 
			
		||||
 
 | 
			
		||||
@@ -59,6 +59,12 @@ namespace Grid{
 | 
			
		||||
      
 | 
			
		||||
      virtual std::string action_name(){return "TwoFlavourRatioPseudoFermionAction";}
 | 
			
		||||
 | 
			
		||||
      virtual std::string LogParameters(){
 | 
			
		||||
	std::stringstream sstream;
 | 
			
		||||
	sstream << GridLogMessage << "["<<action_name()<<"] has no parameters" << std::endl;
 | 
			
		||||
	return sstream.str();
 | 
			
		||||
      }  
 | 
			
		||||
      
 | 
			
		||||
      virtual void refresh(const GaugeField &U, GridParallelRNG& pRNG) {
 | 
			
		||||
 | 
			
		||||
	// P(phi) = e^{- phi^dag V (MdagM)^-1 Vdag phi}
 | 
			
		||||
 
 | 
			
		||||
@@ -115,14 +115,18 @@ class HMCResourceManager {
 | 
			
		||||
      ObservablesList.emplace_back(ObsFactory.create(obs_type, Read));
 | 
			
		||||
      ObservablesList[ObservablesList.size() - 1]->print_parameters();
 | 
			
		||||
    } while (Read.nextElement(observable_string));
 | 
			
		||||
    std::cout << "Size of ObservablesList " << ObservablesList.size()
 | 
			
		||||
              << std::endl;
 | 
			
		||||
    Read.pop();
 | 
			
		||||
 | 
			
		||||
    // Loop on levels
 | 
			
		||||
    Read.push("Actions");
 | 
			
		||||
    if(!Read.push("Actions")){
 | 
			
		||||
      std::cout << "Actions not found" << std::endl; 
 | 
			
		||||
      exit(1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Read.push("Level");// push must check if the node exist
 | 
			
		||||
    if(!Read.push("Level")){// push must check if the node exist
 | 
			
		||||
         std::cout << "Level not found" << std::endl; 
 | 
			
		||||
      exit(1);
 | 
			
		||||
    }
 | 
			
		||||
    do
 | 
			
		||||
    {
 | 
			
		||||
      fill_ActionsLevel(Read); 
 | 
			
		||||
@@ -267,11 +271,12 @@ private:
 | 
			
		||||
      auto &ActionFactory = HMC_LGTActionModuleFactory<gauge_string, ReaderClass>::getInstance(); 
 | 
			
		||||
      std::string action_type;
 | 
			
		||||
      Read.readDefault("name", action_type);
 | 
			
		||||
      std::cout << ActionFactory.getBuilderList() << std::endl;  
 | 
			
		||||
      std::cout << ActionFactory.getBuilderList() << std::endl;  // temporary
 | 
			
		||||
      ActionsList.emplace(m, ActionFactory.create(action_type, Read));
 | 
			
		||||
    } while (Read.nextElement("Action"));
 | 
			
		||||
 | 
			
		||||
    ActionsList.find(m)->second->print_parameters();    
 | 
			
		||||
    Read.pop();
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -242,7 +242,6 @@ class TwoFlavourFModule: public PseudoFermionModuleBase<Impl, TwoFlavourPseudoFe
 | 
			
		||||
   TwoFlavourFModule(Reader<ReaderClass>& R): PseudoFermionModuleBase<Impl, TwoFlavourPseudoFermionAction>(R) {
 | 
			
		||||
    this->getSolverOperator(R, solver_mod, "Solver");
 | 
			
		||||
    this->getFermionOperator(R, fop_mod, "Operator");
 | 
			
		||||
    R.pop();
 | 
			
		||||
   } 
 | 
			
		||||
 | 
			
		||||
  // acquire resource
 | 
			
		||||
@@ -272,7 +271,6 @@ class TwoFlavourEOFModule: public PseudoFermionModuleBase<Impl, TwoFlavourEvenOd
 | 
			
		||||
   TwoFlavourEOFModule(Reader<ReaderClass>& R): PseudoFermionModuleBase<Impl, TwoFlavourEvenOddPseudoFermionAction>(R) {
 | 
			
		||||
    this->getSolverOperator(R, solver_mod, "Solver");
 | 
			
		||||
    this->getFermionOperator(R, fop_mod, "Operator");
 | 
			
		||||
    R.pop();
 | 
			
		||||
   } 
 | 
			
		||||
 | 
			
		||||
  // acquire resource
 | 
			
		||||
@@ -284,6 +282,39 @@ class TwoFlavourEOFModule: public PseudoFermionModuleBase<Impl, TwoFlavourEvenOd
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
template <class Impl >
 | 
			
		||||
class TwoFlavourRatioFModule: public PseudoFermionModuleBase<Impl, TwoFlavourRatioPseudoFermionAction>{
 | 
			
		||||
  typedef PseudoFermionModuleBase<Impl, TwoFlavourRatioPseudoFermionAction> Base;
 | 
			
		||||
  using Base::Base;
 | 
			
		||||
 | 
			
		||||
  typename Base::operator_type fop_numerator_mod;
 | 
			
		||||
  typename Base::operator_type fop_denominator_mod;
 | 
			
		||||
  typename Base::solver_type   solver_mod;
 | 
			
		||||
 | 
			
		||||
 public:
 | 
			
		||||
  virtual void acquireResource(typename Base::Resource& GridMod){
 | 
			
		||||
    fop_numerator_mod->AddGridPair(GridMod);
 | 
			
		||||
    fop_denominator_mod->AddGridPair(GridMod);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
   // constructor
 | 
			
		||||
   template <class ReaderClass>
 | 
			
		||||
   TwoFlavourRatioFModule(Reader<ReaderClass>& R): PseudoFermionModuleBase<Impl, TwoFlavourRatioPseudoFermionAction>(R) {
 | 
			
		||||
    this->getSolverOperator(R, solver_mod, "Solver");
 | 
			
		||||
    this->getFermionOperator(R, fop_numerator_mod, "Numerator");
 | 
			
		||||
    this->getFermionOperator(R, fop_denominator_mod, "Denominator");
 | 
			
		||||
   } 
 | 
			
		||||
 | 
			
		||||
  // acquire resource
 | 
			
		||||
  virtual void initialize() {
 | 
			
		||||
    // here temporarily assuming that the force and action solver are the same
 | 
			
		||||
    this->ActionPtr.reset(new TwoFlavourRatioPseudoFermionAction<Impl>(*(this->fop_numerator_mod->getPtr()), 
 | 
			
		||||
      *(this->fop_denominator_mod->getPtr()), *(this->solver_mod->getPtr()), *(this->solver_mod->getPtr())));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -346,6 +377,7 @@ static Registrar<QCD::PlaqPlusRectangleGMod, HMC_LGTActionModuleFactory<gauge_st
 | 
			
		||||
 | 
			
		||||
// FIXME more general implementation
 | 
			
		||||
static Registrar<QCD::TwoFlavourFModule<QCD::WilsonImplR> , HMC_LGTActionModuleFactory<gauge_string, XmlReader> > __TwoFlavourFmodXMLInit("TwoFlavours"); 
 | 
			
		||||
static Registrar<QCD::TwoFlavourRatioFModule<QCD::WilsonImplR> , HMC_LGTActionModuleFactory<gauge_string, XmlReader> > __TwoFlavourRatioFmodXMLInit("TwoFlavoursRatio"); 
 | 
			
		||||
static Registrar<QCD::TwoFlavourEOFModule<QCD::WilsonImplR> , HMC_LGTActionModuleFactory<gauge_string, XmlReader> > __TwoFlavourEOFmodXMLInit("TwoFlavoursEvenOdd"); 
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										165
									
								
								lib/serialisation/JSON_IO.cc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										165
									
								
								lib/serialisation/JSON_IO.cc
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,165 @@
 | 
			
		||||
    /*************************************************************************************
 | 
			
		||||
 | 
			
		||||
    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 */
 | 
			
		||||
#include <Grid.h>
 | 
			
		||||
 | 
			
		||||
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_ << "}";  
 | 
			
		||||
 | 
			
		||||
  cout  << ss_.str() << endl;
 | 
			
		||||
  // write prettified JSON to file
 | 
			
		||||
  std::ofstream os(fileName_);
 | 
			
		||||
  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)
 | 
			
		||||
{
 | 
			
		||||
  delete_comma();
 | 
			
		||||
  ss_ << "},";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void JSONWriter::delete_comma()
 | 
			
		||||
{
 | 
			
		||||
  std::string dlast = ss_.str();
 | 
			
		||||
  dlast.pop_back(); // deletes the last comma
 | 
			
		||||
  ss_.str(dlast);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
template<>
 | 
			
		||||
void JSONWriter::writeDefault(const std::string &s, const std::string &x){
 | 
			
		||||
  if (s.size())
 | 
			
		||||
  ss_ << "\""<< s << "\" : \"" << x << "\" ," ; 
 | 
			
		||||
  else
 | 
			
		||||
    ss_ << "\"" << x << "\" ," ;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// Reader implementation ///////////////////////////////////////////////////////
 | 
			
		||||
JSONReader::JSONReader(const string &fileName)
 | 
			
		||||
: fileName_(fileName)
 | 
			
		||||
{
 | 
			
		||||
  std::ifstream file(fileName_);
 | 
			
		||||
  file >> jobject_;
 | 
			
		||||
 | 
			
		||||
  // test
 | 
			
		||||
  // serialize to standard output
 | 
			
		||||
  std::cout << "JSONReader::JSONReader : " << jobject_ << endl; 
 | 
			
		||||
  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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cout << "JSONReader::push : " << s << " : "<< jcur_ << endl;
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
  else
 | 
			
		||||
  {
 | 
			
		||||
    do_pop.push_back(false);
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void JSONReader::pop(void)
 | 
			
		||||
{
 | 
			
		||||
  if (do_pop.back()){
 | 
			
		||||
    jcur_ = jold_.back();
 | 
			
		||||
    jold_.pop_back();
 | 
			
		||||
    do_pop.pop_back();
 | 
			
		||||
  }
 | 
			
		||||
  else
 | 
			
		||||
    do_pop.pop_back();
 | 
			
		||||
 | 
			
		||||
  cout << "JSONReader::pop : " << jcur_ << endl;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool JSONReader::nextElement(const std::string &s)
 | 
			
		||||
{
 | 
			
		||||
  // JSON dictionaries do not support multiple names 
 | 
			
		||||
  // Same name objects must be packed in vectors
 | 
			
		||||
  cout << "JSONReader::nextElement(string) : " << s << " : "<< jcur_ << endl;
 | 
			
		||||
  /*
 | 
			
		||||
  if (node_.next_sibling(s.c_str()))
 | 
			
		||||
    {
 | 
			
		||||
      node_ = node_.next_sibling(s.c_str());
 | 
			
		||||
      
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
  else
 | 
			
		||||
    {
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
*/
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template <>
 | 
			
		||||
void JSONReader::readDefault(const string &s, string &output)
 | 
			
		||||
{
 | 
			
		||||
  cout << "JSONReader::readDefault(string) : " << s<< " " << jcur_ << endl;
 | 
			
		||||
  if (s.size()){
 | 
			
		||||
    std::cout << "String: "<< jcur_[s] << std::endl;
 | 
			
		||||
    output = jcur_[s];
 | 
			
		||||
  }
 | 
			
		||||
  else
 | 
			
		||||
  {
 | 
			
		||||
    std::cout << "String: "<< jcur_ << std::endl;
 | 
			
		||||
    output = jcur_;    
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										174
									
								
								lib/serialisation/JSON_IO.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										174
									
								
								lib/serialisation/JSON_IO.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,174 @@
 | 
			
		||||
    /*************************************************************************************
 | 
			
		||||
 | 
			
		||||
    Grid physics library, www.github.com/paboyle/Grid 
 | 
			
		||||
 | 
			
		||||
    Source file: ./lib/serialisation/JSON_IO.h
 | 
			
		||||
 | 
			
		||||
    Copyright (C) 2015
 | 
			
		||||
 | 
			
		||||
		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 */
 | 
			
		||||
#ifndef GRID_SERIALISATION_JSON_IO_H
 | 
			
		||||
#define GRID_SERIALISATION_JSON_IO_H
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <iomanip>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <fstream>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
#include <vector>
 | 
			
		||||
#include <cassert>
 | 
			
		||||
 | 
			
		||||
#include <Grid/json/json.hpp>
 | 
			
		||||
 | 
			
		||||
// for convenience
 | 
			
		||||
using json = nlohmann::json;
 | 
			
		||||
 | 
			
		||||
namespace Grid
 | 
			
		||||
{
 | 
			
		||||
  
 | 
			
		||||
  class JSONWriter: public Writer<JSONWriter>
 | 
			
		||||
  {
 | 
			
		||||
    
 | 
			
		||||
  public:
 | 
			
		||||
    JSONWriter(const std::string &fileName);
 | 
			
		||||
    virtual ~JSONWriter(void);
 | 
			
		||||
    void push(const std::string &s);
 | 
			
		||||
    void pop(void);
 | 
			
		||||
    template <typename U>
 | 
			
		||||
    void writeDefault(const std::string &s, const U &x);
 | 
			
		||||
    template <typename U>
 | 
			
		||||
    void writeDefault(const std::string &s, const std::vector<U> &x);
 | 
			
		||||
 | 
			
		||||
    template<std::size_t N>
 | 
			
		||||
    void writeDefault(const std::string &s, const char(&x)[N]);
 | 
			
		||||
 | 
			
		||||
  private:
 | 
			
		||||
    void delete_comma();
 | 
			
		||||
    std::string         fileName_;
 | 
			
		||||
    std::ostringstream  ss_;
 | 
			
		||||
  };
 | 
			
		||||
  
 | 
			
		||||
  class JSONReader: public Reader<JSONReader>
 | 
			
		||||
  {
 | 
			
		||||
  public:
 | 
			
		||||
    JSONReader(const std::string &fileName);
 | 
			
		||||
    virtual ~JSONReader(void) = default;
 | 
			
		||||
    bool push(const std::string &s);
 | 
			
		||||
    void pop(void);
 | 
			
		||||
    bool nextElement(const std::string &s);
 | 
			
		||||
    template <typename U>
 | 
			
		||||
    void readDefault(const std::string &s, U &output);
 | 
			
		||||
    template <typename U>
 | 
			
		||||
    void readDefault(const std::string &s, std::vector<U> &output);
 | 
			
		||||
  private:
 | 
			
		||||
    json                jobject_; // main object
 | 
			
		||||
    json                jcur_;  // current json object
 | 
			
		||||
    std::vector<json>   jold_;  // previous json object
 | 
			
		||||
    std::string         fileName_;
 | 
			
		||||
    std::vector<bool>   do_pop;
 | 
			
		||||
  };
 | 
			
		||||
  
 | 
			
		||||
  // Writer template implementation ////////////////////////////////////////////
 | 
			
		||||
  template <typename U>
 | 
			
		||||
  void JSONWriter::writeDefault(const std::string &s, const U &x)
 | 
			
		||||
  {
 | 
			
		||||
    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::vector<U> &x)
 | 
			
		||||
  {
 | 
			
		||||
    if (s.size())
 | 
			
		||||
      ss_ << " \""<<s<<"\" : [";
 | 
			
		||||
    else
 | 
			
		||||
      ss_ << " [";
 | 
			
		||||
    for (auto &x_i: x)
 | 
			
		||||
    {
 | 
			
		||||
      write("", x_i);
 | 
			
		||||
    }
 | 
			
		||||
    delete_comma();
 | 
			
		||||
    ss_<< "],";
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  template<std::size_t N>
 | 
			
		||||
  void JSONWriter::writeDefault(const std::string &s, const char(&x)[N]){
 | 
			
		||||
    if (s.size())
 | 
			
		||||
    ss_ << "\""<< s << "\" : \"" << x << "\" ," ;
 | 
			
		||||
    else
 | 
			
		||||
    ss_ << "\"" << x << "\" ," ; 
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Reader template implementation ////////////////////////////////////////////
 | 
			
		||||
  template <typename U>
 | 
			
		||||
  void JSONReader::readDefault(const std::string &s, U &output)
 | 
			
		||||
  {
 | 
			
		||||
    //std::string buf;
 | 
			
		||||
    std::cout << "JSONReader::readDefault(U) : " << s << "  :  "<< jcur_ << std::endl;
 | 
			
		||||
    //readDefault(s, output);
 | 
			
		||||
 | 
			
		||||
    //std::cout << s << "   " << buf << std::endl;
 | 
			
		||||
    //fromString(output, buf);
 | 
			
		||||
  
 | 
			
		||||
    if (s.size()){
 | 
			
		||||
      std::cout << "String: "<< jcur_[s] << std::endl;
 | 
			
		||||
      output = jcur_[s];
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
      std::cout << "String: "<< jcur_ << std::endl;
 | 
			
		||||
      output = jcur_;    
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  template <>
 | 
			
		||||
  void JSONReader::readDefault(const std::string &s, std::string &output);
 | 
			
		||||
  
 | 
			
		||||
  template <typename U>
 | 
			
		||||
  void JSONReader::readDefault(const std::string &s, std::vector<U> &output)
 | 
			
		||||
  {
 | 
			
		||||
    std::string    buf;
 | 
			
		||||
    unsigned int   i = 0;
 | 
			
		||||
    std::cout << "JSONReader::readDefault(vec) : " << jcur_ << std::endl;
 | 
			
		||||
    if (s.size())
 | 
			
		||||
      push(s);
 | 
			
		||||
    
 | 
			
		||||
    json j = jcur_;
 | 
			
		||||
    for (json::iterator it = j.begin(); it != j.end(); ++it) {
 | 
			
		||||
      jcur_ = *it;
 | 
			
		||||
      std::cout << "Value: " << it.value() << "\n";
 | 
			
		||||
      output.resize(i + 1);
 | 
			
		||||
      read("", output[i++]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    jcur_ = j;
 | 
			
		||||
    if (s.size())
 | 
			
		||||
      pop();
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -36,10 +36,10 @@ Author: Peter Boyle <paboyle@ph.ed.ac.uk>
 | 
			
		||||
#include "BinaryIO.h"
 | 
			
		||||
#include "TextIO.h"
 | 
			
		||||
#include "XmlIO.h"
 | 
			
		||||
#include "JSON_IO.h"
 | 
			
		||||
//////////////////////////////////////////
 | 
			
		||||
// Todo:
 | 
			
		||||
//////////////////////////////////////////
 | 
			
		||||
//#include "JsonIO.h"
 | 
			
		||||
//#include "YamlIO.h"
 | 
			
		||||
 | 
			
		||||
//////////////////////////////////////////
 | 
			
		||||
 
 | 
			
		||||
@@ -72,11 +72,13 @@ XmlReader::XmlReader(const string &fileName)
 | 
			
		||||
 | 
			
		||||
bool XmlReader::push(const string &s)
 | 
			
		||||
{
 | 
			
		||||
  node_ = node_.child(s.c_str());
 | 
			
		||||
  if (node_ == NULL)
 | 
			
		||||
 | 
			
		||||
  if (node_.child(s.c_str()) == NULL )
 | 
			
		||||
    return false;
 | 
			
		||||
 | 
			
		||||
  node_ = node_.child(s.c_str());
 | 
			
		||||
  return true;
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void XmlReader::pop(void)
 | 
			
		||||
@@ -87,15 +89,16 @@ void XmlReader::pop(void)
 | 
			
		||||
bool XmlReader::nextElement(const std::string &s)
 | 
			
		||||
{
 | 
			
		||||
  if (node_.next_sibling(s.c_str()))
 | 
			
		||||
  {
 | 
			
		||||
    node_ = node_.next_sibling(s.c_str());
 | 
			
		||||
    {
 | 
			
		||||
      node_ = node_.next_sibling(s.c_str());
 | 
			
		||||
      
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
  else
 | 
			
		||||
  {
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
    {
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template <>
 | 
			
		||||
 
 | 
			
		||||
@@ -4,8 +4,9 @@
 | 
			
		||||
 | 
			
		||||
    Source file: ./tests/Test_serialisation.cc
 | 
			
		||||
 | 
			
		||||
    Copyright (C) 2015
 | 
			
		||||
    Copyright (C) 2015-2016
 | 
			
		||||
 | 
			
		||||
Author: Guido Cossu <guido.cossu@ed.ac.uk>
 | 
			
		||||
Author: Antonin Portelli <antonin.portelli@me.com>
 | 
			
		||||
Author: Peter Boyle <paboyle@ph.ed.ac.uk>
 | 
			
		||||
 | 
			
		||||
@@ -58,6 +59,7 @@ namespace Grid {
 | 
			
		||||
    }
 | 
			
		||||
  };
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
using namespace Grid;
 | 
			
		||||
@@ -154,4 +156,51 @@ int main(int argc,char **argv)
 | 
			
		||||
    std::cout << e << " ";
 | 
			
		||||
  }
 | 
			
		||||
  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
 | 
			
		||||
    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;
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user