From ef07ba39a78a4e47546eef75030e84ead3d57fb1 Mon Sep 17 00:00:00 2001 From: Markus Frank <Markus.Frank@cern.ch> Date: Fri, 17 Mar 2017 21:14:15 +0100 Subject: [PATCH] Cleanup and extending the data parsers for XML/JSON add example ClientTests/MiniTel_json --- DDCore/include/JSON/Elements.h | 10 ++++--- DDCore/src/JSON/DocumentHandler.cpp | 6 ++--- DDCore/src/JSON/Elements.cpp | 21 +++++++++++++++ examples/ClientTests/compact/MiniTel.json | 12 --------- examples/ClientTests/src_boost/JsonTest.cpp | 29 +++------------------ 5 files changed, 34 insertions(+), 44 deletions(-) diff --git a/DDCore/include/JSON/Elements.h b/DDCore/include/JSON/Elements.h index 1c86b7582..1734f2b96 100644 --- a/DDCore/include/JSON/Elements.h +++ b/DDCore/include/JSON/Elements.h @@ -422,11 +422,13 @@ namespace DD4hep { }; /// Forward declarations - class DocumentHandler; - - + class DocumentHandler; #undef INLINE + + void dumpTree(Handle_t elt); + void dumpTree(Element elt); + void dumpTree(const JsonElement* elt); - } /* End namespace XML */ + } /* End namespace JSON */ } /* End namespace DD4hep */ #endif /* DDCORE_DD4HEP_JSON_ELEMENTS_H */ diff --git a/DDCore/src/JSON/DocumentHandler.cpp b/DDCore/src/JSON/DocumentHandler.cpp index b38aa4eb8..1dffafcb6 100644 --- a/DDCore/src/JSON/DocumentHandler.cpp +++ b/DDCore/src/JSON/DocumentHandler.cpp @@ -34,9 +34,9 @@ DocumentHandler::~DocumentHandler() { Document DocumentHandler::load(const string& fname) const { string fn = fname; if ( fname.find("://") != string::npos ) fn = fname.substr(fname.find("://")+3); - string cmd = "cat "+fn; - ::printf("\n\n+++++ Dump json file: %s\n\n\n",fn.c_str()); - ::system(cmd.c_str()); + //string cmd = "cat "+fn; + //::printf("\n\n+++++ Dump json file: %s\n\n\n",fn.c_str()); + //::system(cmd.c_str()); unique_ptr<JsonElement> doc(new JsonElement(fn, ptree())); boost::property_tree::read_json(fn,doc->second); return doc.release(); diff --git a/DDCore/src/JSON/Elements.cpp b/DDCore/src/JSON/Elements.cpp index 8b740290a..24079d656 100644 --- a/DDCore/src/JSON/Elements.cpp +++ b/DDCore/src/JSON/Elements.cpp @@ -518,3 +518,24 @@ void Collection_t::operator++(int) const { void Collection_t::operator--(int) const { --(*this); } + +void DD4hep::JSON::dumpTree(Handle_t elt) { + dumpTree(elt.ptr()); +} + +void DD4hep::JSON::dumpTree(Element elt) { + dumpTree(elt.ptr()); +} + +void DD4hep::JSON::dumpTree(const JsonElement* elt) { + struct Dump { + void operator()(const JsonElement* elt, const string& tag) const { + string t = tag+" "; + printout(INFO,"DumpTree","+++ %s %s: %s",tag.c_str(), elt->first.c_str(), elt->second.data().c_str()); + for(auto i=elt->second.begin(); i!=elt->second.end(); ++i) + (*this)(&(*i), t); + } + } _dmp; + _dmp(elt," "); +} + diff --git a/examples/ClientTests/compact/MiniTel.json b/examples/ClientTests/compact/MiniTel.json index d0746cffa..d56a9c81c 100644 --- a/examples/ClientTests/compact/MiniTel.json +++ b/examples/ClientTests/compact/MiniTel.json @@ -1,15 +1,3 @@ -//========================================================================== -// AIDA Detector description implementation for LCD -//-------------------------------------------------------------------------- -// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN) -// All rights reserved. -// -// For the licensing terms see $DD4hepINSTALL/LICENSE. -// For the list of contributors see $DD4hepINSTALL/doc/CREDITS. -// -// Author : M.Frank -// -//========================================================================== { "detector": { "id": 1, diff --git a/examples/ClientTests/src_boost/JsonTest.cpp b/examples/ClientTests/src_boost/JsonTest.cpp index 456940aa1..13cb089d9 100644 --- a/examples/ClientTests/src_boost/JsonTest.cpp +++ b/examples/ClientTests/src_boost/JsonTest.cpp @@ -22,34 +22,13 @@ using namespace DD4hep; using namespace DD4hep::JSON; using namespace DD4hep::Geometry; -static long json_dump(LCDD& lcdd, int argc, char** argv) { +static long json_dump(LCDD& /* lcdd */, int argc, char** argv) { if ( argc < 1 ) { - return 0; + ::printf("DD4hep_JsonDumper <file> \n"); + exit(EINVAL); } DocumentHolder doc(JSON::DocumentHandler().load(argv[0])); - Element elt = doc.root(); - - for( boost::property_tree::ptree::value_type const& rowPair : elt.ptr()->second.get_child( "" ) ) - { - cout << "1--" << rowPair.first << ": " << endl; - cout << rowPair.second.get_value<string>() << " " << endl; - - for( boost::property_tree::ptree::value_type const& itemPair : rowPair.second ) - { - cout << "\t2--" << itemPair.first << " " << endl; - - for( boost::property_tree::ptree::value_type const& node : itemPair.second ) - { - cout << "\t\t3--" << node.first << "=" << node.second.get_value<string>() << endl; - } - - cout << endl; - - } - - cout << endl; - } - + dumpTree(doc.root()); return 1; } -- GitLab