#ifndef __JSON__JPRINTER__ #define __JSON__JPRINTER__ #include #include #include "JSon/JSon.hh" /** * \author mdejong */ namespace JSON {} namespace JPP { using namespace JSON; } namespace JSON { /** * Auxiliary data structure to print (part of) JSon data. * * The key can be used to specify which part of the JSon data to print.\n * For example: *
   *   "Error.Code+Message"
   * 
* will print both the error code and message of some specific JSon data. */ struct JPrinter { char SEPARATOR[2] = { '.', '+' }; int WIDTH = 2; /** * Defaut constructor. */ JPrinter() {} /** * Auxiliary method to print (part of) JSon data. * * \param out output stream * \param js JSon data * \param key key */ inline std::ostream& operator()(std::ostream& out, const JSON::json& js, const std::string& key) const { using namespace std; size_t pos = key.find(SEPARATOR[0]); if (pos != string::npos) { if (js[key.substr(0, pos)].is_array()) { for (const auto& element : js[key.substr(0, pos)]) { (*this)(out, element, key.substr(pos + 1)); } } else { (*this)(out, js[key.substr(0, pos)], key.substr(pos + 1)); } } else if (key != "") { pos = key.find(SEPARATOR[1]); out << setw(WIDTH) << js[key.substr(0, pos)] << ' '; if (pos != string::npos) (*this)(out, js, key.substr(pos + 1)); else out << endl; } else { out << setw(WIDTH) << js << endl; } return out; } }; } #endif