smartmontools SVN Rev 5640
Utility to control and monitor storage systems with "S.M.A.R.T."
Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
json Class Reference

Create and print JSON output. More...

#include <json.h>

Collaboration diagram for json:
Collaboration graph
[legend]

Classes

struct  initlist_key_value_pair
 
struct  initlist_value
 
struct  node
 
struct  node_info
 
struct  print_options
 Options for print(). More...
 
class  ref
 Reference to a JSON element. More...
 

Public Types

enum  node_type {
  nt_unset , nt_object , nt_array , nt_bool ,
  nt_int , nt_uint , nt_uint128 , nt_string
}
 

Public Member Functions

ref operator[] (const char *keystr)
 Return reference to element of top level object.
 
ref operator[] (const std::string &keystr)
 Return reference to element of top level object (std::string variant).
 
void operator+= (std::initializer_list< initlist_key_value_pair > ilist)
 Braced-init-list support for top level object.
 
void enable (bool yes=true)
 Enable/disable JSON output.
 
bool is_enabled () const
 Return true if enabled.
 
void set_verbose (bool yes=true)
 Enable/disable extra string output for safe integers also.
 
bool has_uint128_output () const
 Return true if any 128-bit value has been output.
 
void print (FILE *f, const print_options &options) const
 Print JSON tree to a file.
 

Static Public Member Functions

static bool is_safe_uint (unsigned long long value)
 Return true if value is a safe JSON integer.
 
static std::string str2key (const char *str)
 Replace space and non-alphanumerics with '_', upper to lower case.
 
static std::string str2key (const std::string &str)
 Replace space and non-alphanumerics with '_', upper to lower case (std::string variant).
 

Private Types

typedef std::vector< node_infonode_path
 

Private Member Functions

nodefind_or_create_node (const node_path &path, node_type type)
 
void set_bool (const node_path &path, bool value)
 
void set_int64 (const node_path &path, int64_t value)
 
void set_uint64 (const node_path &path, uint64_t value)
 
void set_uint128 (const node_path &path, uint64_t value_hi, uint64_t value_lo)
 
void set_cstring (const node_path &path, const char *value)
 
void set_string (const node_path &path, const std::string &value)
 
void set_initlist_value (const node_path &path, const initlist_value &value)
 

Static Private Member Functions

static void print_json (FILE *f, bool pretty, bool sorted, const node *p, int level)
 
static void print_yaml (FILE *f, bool pretty, bool sorted, const node *p, int level_o, int level_a, bool cont)
 
static void print_flat (FILE *f, const char *assign, bool sorted, const node *p, std::string &path)
 

Private Attributes

bool m_enabled = false
 
bool m_verbose = false
 
bool m_uint128_output = false
 
node m_root_node
 

Detailed Description

Create and print JSON output.

Definition at line 23 of file json.h.

Member Typedef Documentation

◆ node_path

typedef std::vector<node_info> json::node_path
private

Definition at line 100 of file json.h.

Member Enumeration Documentation

◆ node_type

Enumerator
nt_unset 
nt_object 
nt_array 
nt_bool 
nt_int 
nt_uint 
nt_uint128 
nt_string 

Definition at line 39 of file json.h.

Member Function Documentation

◆ enable()

void json::enable ( bool  yes = true)
inline

Enable/disable JSON output.

Definition at line 183 of file json.h.

◆ find_or_create_node()

json::node * json::find_or_create_node ( const node_path path,
node_type  type 
)
private

Definition at line 307 of file json.cpp.

◆ has_uint128_output()

bool json::has_uint128_output ( ) const
inline

Return true if any 128-bit value has been output.

Definition at line 195 of file json.h.

◆ is_enabled()

bool json::is_enabled ( ) const
inline

Return true if enabled.

Definition at line 187 of file json.h.

◆ is_safe_uint()

static bool json::is_safe_uint ( unsigned long long  value)
inlinestatic

Return true if value is a safe JSON integer.

Same as Number.isSafeInteger(value) in JavaScript.

Definition at line 28 of file json.h.

◆ operator+=()

void json::operator+= ( std::initializer_list< initlist_key_value_pair ilist)
inline

Braced-init-list support for top level object.

Definition at line 179 of file json.h.

◆ operator[]() [1/2]

ref json::operator[] ( const char *  keystr)
inline

Return reference to element of top level object.

Definition at line 171 of file json.h.

◆ operator[]() [2/2]

ref json::operator[] ( const std::string &  keystr)
inline

Return reference to element of top level object (std::string variant).

Definition at line 175 of file json.h.

◆ print()

void json::print ( FILE *  f,
const print_options options 
) const

Print JSON tree to a file.

Definition at line 710 of file json.cpp.

◆ print_flat()

void json::print_flat ( FILE *  f,
const char *  assign,
bool  sorted,
const node p,
std::string &  path 
)
staticprivate

Definition at line 647 of file json.cpp.

◆ print_json()

void json::print_json ( FILE *  f,
bool  pretty,
bool  sorted,
const node p,
int  level 
)
staticprivate

Definition at line 514 of file json.cpp.

◆ print_yaml()

void json::print_yaml ( FILE *  f,
bool  pretty,
bool  sorted,
const node p,
int  level_o,
int  level_a,
bool  cont 
)
staticprivate

Definition at line 576 of file json.cpp.

◆ set_bool()

void json::set_bool ( const node_path path,
bool  value 
)
private

Definition at line 367 of file json.cpp.

◆ set_cstring()

void json::set_cstring ( const node_path path,
const char *  value 
)
private

Definition at line 397 of file json.cpp.

◆ set_initlist_value()

void json::set_initlist_value ( const node_path path,
const initlist_value value 
)
private

Definition at line 412 of file json.cpp.

◆ set_int64()

void json::set_int64 ( const node_path path,
int64_t  value 
)
private

Definition at line 374 of file json.cpp.

◆ set_string()

void json::set_string ( const node_path path,
const std::string &  value 
)
private

Definition at line 405 of file json.cpp.

◆ set_uint128()

void json::set_uint128 ( const node_path path,
uint64_t  value_hi,
uint64_t  value_lo 
)
private

Definition at line 388 of file json.cpp.

◆ set_uint64()

void json::set_uint64 ( const node_path path,
uint64_t  value 
)
private

Definition at line 381 of file json.cpp.

◆ set_verbose()

void json::set_verbose ( bool  yes = true)
inline

Enable/disable extra string output for safe integers also.

Definition at line 191 of file json.h.

◆ str2key() [1/2]

std::string json::str2key ( const char *  str)
static

Replace space and non-alphanumerics with '_', upper to lower case.

Definition at line 41 of file json.cpp.

◆ str2key() [2/2]

static std::string json::str2key ( const std::string &  str)
inlinestatic

Replace space and non-alphanumerics with '_', upper to lower case (std::string variant).

Definition at line 36 of file json.h.

Member Data Documentation

◆ m_enabled

bool json::m_enabled = false
private

Definition at line 244 of file json.h.

◆ m_root_node

node json::m_root_node
private

Definition at line 248 of file json.h.

◆ m_uint128_output

bool json::m_uint128_output = false
private

Definition at line 246 of file json.h.

◆ m_verbose

bool json::m_verbose = false
private

Definition at line 245 of file json.h.


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