#ifndef __JDB_JSONSUPPORTKIT__ #define __JDB_JSONSUPPORTKIT__ #ifndef __ROOTCLING__ #include #include #include #include #include "TDictionary.h" #include "JROOT/JRootClass.hh" #include "JLang/JException.hh" #include "JLang/JTypeList.hh" #include "JLang/JPrimitiveTypes.hh" #include "JLang/JSingleton.hh" #include "JSon/JSon.hh" #include "JDB/JDBString.hh" /** * \author mdejong */ namespace JDATABASE {} namespace JPP { using namespace JDATABASE; } namespace JDATABASE { using JSON::json; using JROOT::JRootWritableClass; using JROOT::JRootReadableClass; using JLANG::JException; using JLANG::JParserException; using JLANG::JSingleton; /** * Convert database string to JSon. * * \param js json * \param object database string */ inline void to_json(json& js, const JDBString& object) { js = json(static_cast(object)); } /** * Convert JSon to database string. * * \param js json * \param object database string */ inline void from_json(const json& js, JDBString& object) { static_cast(object) = js.get(); } /** * Assigment interface. */ struct JSonIO { virtual ~JSonIO() {} virtual void to_json(json& js, const void* p) const = 0; virtual void from_json(const json& js, void* p) const = 0; }; /** * Assigment implementation. */ template struct JSonIO_t : public JSonIO { virtual void to_json(json& js, const void* p) const override { js = * (const T*) p; } virtual void from_json(const json& js, void* p) const override { * (T*) p = js; } }; /** * Assignment. */ struct JSonDictionary : public std::map< std::string, std::shared_ptr >, public JSingleton { /** * Default constructor. * * Removed types: * - long double (not supported by ROOT TDictionary) * - char (not supported by JSon) * * Added STL type definitions: * - std::string * - size_t * - std::size_t * - JDATABASE::JDBString */ JSonDictionary() { using namespace JPP; for_each >::typelist>(*this); (*this)(JType()); (*this)(JType()); (*this)(JType()); (*this)(JType()); } /** * Get typename for given template class. * * This method uses the TDictionary class to get the name of the given class. * * \return type name */ template static const char* getTypename() { const TDictionary* pDictionary = TDictionary::GetDictionary(typeid(T)); if (pDictionary != NULL) return pDictionary->GetName(); else THROW(JException, "Data type not implemented."); } /** * Add data type. * * \param type data type */ template void operator()(const JLANG::JType& type) { (*this)[getTypename()] = std::make_shared< JSonIO_t >(); } }; /** * Auxiliary base class for JSon I/O based on ROOT dictionary. */ template struct JSonHelper { /** * Convert object to JSon. * * \param js json * \param object object */ friend inline void to_json(json& js, const T& object) { using namespace std; using namespace JPP; JRootWritableClass cls(object); for (unique_ptr i(cls.getClass()->GetListOfDataMembers()->MakeIterator()); const TDataMember* p = (const TDataMember*) i->Next(); ) { if (!JRootClass::is_static(*p)) { JRootWritableClass abc = cls.get(*p); JSonDictionary::getInstance()[abc.getTypename()]->to_json(js[p->GetName()], abc.getAddress()); } } } /** * Convert JSon to object/. * * \param js json * \param object object */ friend inline void from_json(const json& js, T& object) { using namespace std; using namespace JPP; JRootReadableClass cls(object); for (unique_ptr i(cls.getClass()->GetListOfDataMembers()->MakeIterator()); const TDataMember* p = (const TDataMember*) i->Next(); ) { if (!JRootClass::is_static(*p)) { JRootReadableClass abc = cls.get(*p); if (js.contains(p->GetName())) JSonDictionary::getInstance()[abc.getTypename()]->from_json(js[p->GetName()], abc.getAddress()); else THROW(JParserException, "No key word <" << p->GetName() << "> in " << js); } } } }; } #else namespace JDATABASE {} namespace JPP { using namespace JDATABASE; } namespace JDATABASE { template struct JSonHelper {}; } #endif #endif