Belle II Software light-2406-ragdoll
KeyValuePrinter Class Reference

create human-readable or JSON output for key value pairs. More...

#include <KeyValuePrinter.h>

Collaboration diagram for KeyValuePrinter:

Public Member Functions

 KeyValuePrinter (bool use_json, unsigned key_max_length=10)
 Ctor.
 
std::string string () const
 Return completed string.
 
template<class T >
void put (const std::string &key, const T &value)
 Add one key-value pair.
 
template<class T >
void put (const std::string &key, const std::map< std::string, T > &value)
 Specialization for map<>
 

Private Member Functions

std::string escape (const std::string &value) const
 escape string.
 
std::string escape (const char *value) const
 escape string literals.
 
template<class T >
std::string escape (const T &value) const
 escape numeric value.
 
template<class T >
std::string escape (const std::vector< T > &value) const
 escape vector<T>.
 
template<class T >
std::string escape (const std::map< std::string, T > &value) const
 escape map<string, T>.
 

Private Attributes

bool m_json
 create JSON output?
 
unsigned m_maxpad
 for human-readable output: how much padding after key?
 
std::string m_delim
 for JSON: comma to print after prev entry.
 
std::stringstream m_stream
 output being built.
 

Detailed Description

create human-readable or JSON output for key value pairs.

Usage example for human-readable output:

KeyValuePrinter pr(false); //JSON=false
pr.put("a", 123);
pr.put("bbbbb", "string");
pr.put("ccccccc", std::vector<std::string>({"string", "abc"}));
std::cout << pr.string();
//which prints
// a: 123
// bbbbb: string
// ccccccc: [string, abc]
create human-readable or JSON output for key value pairs.

std::string, numeric types, std::vector<SUPPORTED TYPE>, and std::map<std::string, SUPPORTED TYPE> * are supported as value types for put().

JSON output is suitable for reading via e.g. json.loads() in Python, and performs the necessary escaping for strings. Compared to boost::property_tree::json_parser::write_json, KeyValuePrinter handles non-string types correctly.

Definition at line 46 of file KeyValuePrinter.h.

Constructor & Destructor Documentation

◆ KeyValuePrinter()

KeyValuePrinter ( bool  use_json,
unsigned  key_max_length = 10 
)
inlineexplicit

Ctor.

Parameters
use_jsontrue for JSON output, false for human-readable output.
key_max_lengthfor human-readable output, this should be equal to strlen(longest_key) for aligning values in output.

Definition at line 53 of file KeyValuePrinter.h.

53 :
54 m_json(use_json),
55 m_maxpad(key_max_length + 2),
56 m_delim("")
57 { }
unsigned m_maxpad
for human-readable output: how much padding after key?
bool m_json
create JSON output?
std::string m_delim
for JSON: comma to print after prev entry.

◆ ~KeyValuePrinter()

~KeyValuePrinter ( )
inline

Definition at line 58 of file KeyValuePrinter.h.

58{ }

Member Function Documentation

◆ escape() [1/5]

std::string escape ( const char *  value) const
inlineprivate

escape string literals.

Definition at line 102 of file KeyValuePrinter.h.

102{ return escape(std::string(value)); }
std::string escape(const std::string &value) const
escape string.

◆ escape() [2/5]

std::string escape ( const std::map< std::string, T > &  value) const
inlineprivate

escape map<string, T>.

Definition at line 118 of file KeyValuePrinter.h.

119 {
120 //well, it's valid json, but not pretty
121 KeyValuePrinter printer(m_json, m_maxpad - 2);
122 for (auto pair : value)
123 printer.put(pair.first, pair.second);
124 return printer.string();
125 }
KeyValuePrinter(bool use_json, unsigned key_max_length=10)
Ctor.

◆ escape() [3/5]

std::string escape ( const std::string &  value) const
private

escape string.

Definition at line 14 of file KeyValuePrinter.cc.

15{
16 using namespace boost::property_tree::json_parser;
17
18 if (m_json)
19 return "\"" + create_escapes(value) + "\"";
20 else
21 return value;
22}

◆ escape() [4/5]

std::string escape ( const std::vector< T > &  value) const
inlineprivate

escape vector<T>.

Definition at line 106 of file KeyValuePrinter.h.

107 {
108 std::string delim = "";
109 std::string s = "[";
110 for (auto el : value) {
111 s += delim + escape(el);
112 delim = ", ";
113 }
114 s += "]";
115 return s;
116 }

◆ escape() [5/5]

std::string escape ( const T &  value) const
inlineprivate

escape numeric value.

Definition at line 104 of file KeyValuePrinter.h.

104{ return std::to_string(value); }

◆ put() [1/2]

void put ( const std::string &  key,
const std::map< std::string, T > &  value 
)
inline

Specialization for map<>

Definition at line 79 of file KeyValuePrinter.h.

80 {
81 if (m_json) {
82 m_stream << m_delim << "\t" << escape(key) << ": " << escape(value) << "";
83 } else {
84 m_stream << "\n" << key << "\n";
85 for (char unused : key) {
86 (void)unused;
87 m_stream << '-';
88 }
89 m_stream << "\n" << escape(value) << "\n";
90 }
91 m_delim = ",\n";
92 }
std::stringstream m_stream
output being built.

◆ put() [2/2]

void put ( const std::string &  key,
const T &  value 
)
inline

Add one key-value pair.

Definition at line 69 of file KeyValuePrinter.h.

70 {
71 if (m_json) {
72 m_stream << m_delim << "\t" << escape(key) << ": " << escape(value) << "";
73 } else {
74 m_stream << std::left << std::setw(m_maxpad) << key + ": " << escape(value) << "\n";
75 }
76 m_delim = ",\n";
77 }

◆ string()

std::string string ( ) const
inline

Return completed string.

Definition at line 61 of file KeyValuePrinter.h.

62 {
63 if (m_json)
64 return "{\n" + m_stream.str() + "\n}\n";
65 else
66 return m_stream.str();
67 }

Member Data Documentation

◆ m_delim

std::string m_delim
private

for JSON: comma to print after prev entry.

Definition at line 96 of file KeyValuePrinter.h.

◆ m_json

bool m_json
private

create JSON output?

Definition at line 94 of file KeyValuePrinter.h.

◆ m_maxpad

unsigned m_maxpad
private

for human-readable output: how much padding after key?

Definition at line 95 of file KeyValuePrinter.h.

◆ m_stream

std::stringstream m_stream
private

output being built.

Definition at line 97 of file KeyValuePrinter.h.


The documentation for this class was generated from the following files: