12#define JSON_H_CVSID "$Id: json.h 5292 2022-01-06 17:13:25Z chrfranke $"
16#include <initializer_list>
29 {
return (value < (1ULL << 53)); }
32 static std::string
str2key(
const char * str);
36 static std::string
str2key(
const std::string & str)
37 {
return str2key(str.c_str()); }
85 std::initializer_list<initlist_key_value_pair>
object;
86 std::initializer_list<initlist_value>
array;
111 {
return ref(*
this, keystr); }
115 {
return ref(*
this, keystr.c_str()); }
119 {
return ref(*
this, index); }
129 void operator=(
unsigned long long value);
132 void operator=(
const std::string & value);
136 {
return ref(*
this,
"", key_suffix); }
138 void set_uint128(uint64_t value_hi, uint64_t value_lo);
151 void operator+=(std::initializer_list<initlist_key_value_pair> ilist);
153 void operator+=(std::initializer_list<initlist_value> ilist);
158 ref(
json & js,
const char * keystr);
159 ref(
const ref & base,
const char * keystr);
160 ref(
const ref & base,
int index);
161 ref(
const ref & base,
const char * ,
const char * key_suffix);
172 {
return ref(*
this, keystr); }
176 {
return ref(*
this, keystr.c_str()); }
179 void operator+=(std::initializer_list<initlist_key_value_pair> ilist)
180 {
ref(*
this) += ilist; }
213 explicit node(
const std::string & key_);
223 std::vector< std::unique_ptr<node> >
childs;
224 typedef std::map<std::string, unsigned>
keymap;
260 static void print_json(FILE * f,
bool pretty,
bool sorted,
const node * p,
int level);
261 static void print_yaml(FILE * f,
bool pretty,
bool sorted,
const node * p,
int level_o,
262 int level_a,
bool cont);
263 static void print_flat(FILE * f,
const char * assign,
bool sorted,
const node * p,
unsigned array_index() const
const node * operator*() const
keymap::const_iterator m_key_iter
Reference to a JSON element.
ref with_suffix(const char *key_suffix) const
Return reference to element with KEY_SUFFIX appended to last key.
void set_uint128(uint64_t value_hi, uint64_t value_lo)
ref operator[](const char *keystr) const
Return reference to object element.
void set_unsafe_uint128(uint64_t value_hi, uint64_t value_lo)
void operator=(const initlist_value &value)
void operator+=(std::initializer_list< initlist_key_value_pair > ilist)
Braced-init-list support for nested objects.
void set_unsafe_le128(const void *pvalue)
void operator=(bool value)
bool set_if_safe_le128(const void *pvalue)
void set_unsafe_uint64(uint64_t value)
bool set_if_safe_uint64(uint64_t value)
bool set_if_safe_uint128(uint64_t value_hi, uint64_t value_lo)
ref operator[](const std::string &keystr) const
Return reference to object element (std::string variant).
ref operator[](int index) const
Return reference to array element.
Create and print JSON output.
bool has_uint128_output() const
Return true if any 128-bit value has been output.
static void print_flat(FILE *f, const char *assign, bool sorted, const node *p, std::string &path)
std::vector< node_info > node_path
void set_uint64(const node_path &path, uint64_t value)
void set_int64(const node_path &path, int64_t value)
bool is_enabled() const
Return true if enabled.
void set_string(const node_path &path, const std::string &value)
void set_initlist_value(const node_path &path, const initlist_value &value)
static std::string str2key(const char *str)
Replace space and non-alphanumerics with '_', upper to lower case.
void set_bool(const node_path &path, bool value)
void set_cstring(const node_path &path, const char *value)
static void print_json(FILE *f, bool pretty, bool sorted, const node *p, int level)
static bool is_safe_uint(unsigned long long value)
Return true if value is a safe JSON integer.
ref operator[](const std::string &keystr)
Return reference to element of top level object (std::string variant).
static std::string str2key(const std::string &str)
Replace space and non-alphanumerics with '_', upper to lower case (std::string variant).
void set_verbose(bool yes=true)
Enable/disable extra string output for safe integers also.
static void print_yaml(FILE *f, bool pretty, bool sorted, const node *p, int level_o, int level_a, bool cont)
node * find_or_create_node(const node_path &path, node_type type)
ref operator[](const char *keystr)
Return reference to element of top level object.
void operator+=(std::initializer_list< initlist_key_value_pair > ilist)
Braced-init-list support for top level object.
void set_uint128(const node_path &path, uint64_t value_hi, uint64_t value_lo)
void enable(bool yes=true)
Enable/disable JSON output.
void print(FILE *f, const print_options &options) const
Print JSON tree to a file.
std::initializer_list< initlist_key_value_pair > object
initlist_key_value_pair(const char *k, const initlist_value &v)
initlist_key_value_pair(const char *k, const std::initializer_list< initlist_key_value_pair > &ilist)
initlist_key_value_pair(const std::string &k, const std::initializer_list< initlist_key_value_pair > &ilist)
initlist_key_value_pair(const char *k, const std::initializer_list< initlist_value > &ilist)
initlist_key_value_pair(const std::string &k, const std::initializer_list< initlist_value > &ilist)
initlist_key_value_pair(const std::string &k, const initlist_value &v)
std::initializer_list< initlist_value > array
initlist_value(unsigned v)
initlist_value(const char *v)
initlist_value(unsigned long v)
initlist_value(unsigned long long v)
initlist_value(const std::string &v)
initlist_value(long long v)
initlist_value(node_type t)
node_info(const char *keystr)
node(const node &)=delete
std::vector< std::unique_ptr< node > > childs
void operator=(const node &)=delete
std::map< std::string, unsigned > keymap