Jpp  17.3.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JPrinter.hh
Go to the documentation of this file.
1 #ifndef __JSON__JPRINTER__
2 #define __JSON__JPRINTER__
3 
4 #include <ostream>
5 #include <iomanip>
6 
7 #include "JSon/JSon.hh"
8 
9 
10 /**
11  * \author mdejong
12  */
13 
14 namespace JSON {}
15 namespace JPP { using namespace JSON; }
16 
17 namespace JSON {
18 
19  /**
20  * Auxiliary data structure to print (part of) JSon data.
21  *
22  * The key can be used to specify which part of the JSon data to print.\n
23  * For example:
24  * <pre>
25  * "Error.Code+Message"
26  * </pre>
27  * will print both the error code and message of some specific JSon data.
28  */
29  struct JPrinter {
30 
31  char SEPARATOR[2] = { '.', '+' };
32  int WIDTH = 2;
33 
34  /**
35  * Defaut constructor.
36  */
38  {}
39 
40 
41  /**
42  * Auxiliary method to print (part of) JSon data.
43  *
44  * \param out output stream
45  * \param js JSon data
46  * \param key key
47  */
48  inline std::ostream& operator()(std::ostream& out, const JSON::json& js, const std::string& key) const
49  {
50  using namespace std;
51 
52  size_t pos = key.find(SEPARATOR[0]);
53 
54  if (pos != string::npos) {
55 
56  if (js[key.substr(0, pos)].is_array()) {
57 
58  for (const auto& element : js[key.substr(0, pos)]) {
59  (*this)(out, element, key.substr(pos + 1));
60  }
61 
62  } else {
63 
64  (*this)(out, js[key.substr(0, pos)], key.substr(pos + 1));
65  }
66 
67  } else if (key != "") {
68 
69  pos = key.find(SEPARATOR[1]);
70 
71  out << setw(WIDTH) << js[key.substr(0, pos)] << endl;
72 
73  if (pos != string::npos) {
74  (*this)(out, js, key.substr(pos + 1));
75  }
76 
77  } else {
78 
79  out << setw(WIDTH) << js << endl;
80  }
81 
82  return out;
83  }
84  };
85 }
86 
87 #endif
Auxiliary data structure for alignment of data.
Definition: JManip.hh:231
then awk string
std::ostream & operator()(std::ostream &out, const JSON::json &js, const std::string &key) const
Auxiliary method to print (part of) JSon data.
Definition: JPrinter.hh:48
JPrinter()
Defaut constructor.
Definition: JPrinter.hh:37
nlohmann::json json
char SEPARATOR[2]
Definition: JPrinter.hh:31
Auxiliary data structure to print (part of) JSon data.
Definition: JPrinter.hh:29