#ifndef __JROOT__JSONOBJECTOUTPUT__ #define __JROOT__JSONOBJECTOUTPUT__ #include #include #include "TBufferJSON.h" #include "JLang/JObjectOutput.hh" #include "JLang/JTypeList.hh" #include "JLang/JNullType.hh" #include "JLang/JConversion.hh" /** * \author mdejong */ namespace JROOT {} namespace JPP { using namespace JROOT; } namespace JROOT { using JLANG::JObjectOutput; /** * Template implementation of stream output for single data type. * * This class implements the JLANG::JObjectOutput interface. */ template class JSonObjectOutput : public virtual JObjectOutput { public: /** * Constructor. * * \param out output stream * \param sep token separator */ JSonObjectOutput(std::ostream& out, const std::string& sep = "\n") : __out(out), __sep(sep) {} /** * Object output. * * \param object object * \return true if OK; else false */ virtual bool put(const T& object) override { using namespace JPP; if (JConversion::is_derived) return (bool) (__out << (const char*) TBufferJSON::ConvertToJSON(&object) << __sep); else return false; } /** * Get token separator. * * \return separator */ const std::string& getSeparator() const { return this->__sep; } /** * Set token separator. * * \param sep separator */ void setSeparator(const std::string& sep) { this->__sep = sep; } private: std::ostream& __out; std::string __sep; }; /** * Template specialisationimplementation of stream object output for multiple data types. * * This class recursively implements the JLANG::JObjectOutput interface * for all data types by deriving from: * - JSonObjectOutput; and * - JSonObjectOutput. */ template class JSonObjectOutput< JTypeList > : public virtual JObjectOutput< JTypeList >, public JSonObjectOutput, public JSonObjectOutput { public: using JSonObjectOutput::put; using JSonObjectOutput::put; /** * Constructor. * * \param out output stream * \param sep token separator */ JSonObjectOutput(std::ostream& out, const std::string& sep = "") : JSonObjectOutput(out, sep), JSonObjectOutput(out, sep) {} /** * Set token separator. * * \param sep separator */ void setSeparator(const std::string& sep) { JSonObjectOutput::setSeparator(sep); JSonObjectOutput::setSeparator(sep); } }; /** * Terminator class of recursive JSonObjectOutput class. */ template class JSonObjectOutput< JTypeList > : public JSonObjectOutput { public: using JSonObjectOutput::put; /** * Constructor. * * \param out output stream * \param sep token separator */ JSonObjectOutput(std::ostream& out, const std::string& sep = "") : JSonObjectOutput(out, sep) {} }; } #endif