4 * Home page of code is: https://www.smartmontools.org
6 * Copyright (C) 2017-18 Christian Franke
8 * SPDX-License-Identifier: GPL-2.0-or-later
12 #define JSON_H_CVSID "$Id: json.h 4825 2018-10-25 19:47:35Z chrfranke $"
20 /// Create and print JSON output.
31 explicit node_info(const char * key_
)
32 : key(key_
), index(0) { }
33 explicit node_info(int index_
)
37 typedef std::vector
<node_info
> node_path
;
40 /// Return true if value is a safe JSON integer.
41 /// Same as Number.isSafeInteger(value) in JavaScript.
42 static bool is_safe_uint(unsigned long long value
)
43 { return (value
< (1ULL << 53)); }
47 /// Reference to a JSON element.
51 /// Return reference to object element.
52 ref
operator[](const char * key
) const
53 { return ref(*this, key
); };
55 /// Return reference to array element.
56 ref
operator[](int index
) const
57 { return ref(*this, index
); };
59 // Assignment operators create or change element.
60 void operator=(bool value
);
62 void operator=(int value
);
63 void operator=(unsigned value
);
64 void operator=(long value
);
65 void operator=(unsigned long value
);
66 void operator=(long long value
);
67 void operator=(unsigned long long value
);
69 void operator=(const char * value
);
70 void operator=(const std::string
& value
);
72 /// Return reference to element with KEY_SUFFIX appended to last key.
73 ref
with_suffix(const char * key_suffix
) const
74 { return ref(*this, "", key_suffix
); }
76 void set_uint128(uint64_t value_hi
, uint64_t value_lo
);
78 // Output only if safe integer.
79 bool set_if_safe_uint64(uint64_t value
);
80 bool set_if_safe_uint128(uint64_t value_hi
, uint64_t value_lo
);
81 bool set_if_safe_le128(const void * pvalue
);
83 // If unsafe integer, output also as string with key "NUMBER_s".
84 void set_unsafe_uint64(uint64_t value
);
85 void set_unsafe_uint128(uint64_t value_hi
, uint64_t value_lo
);
86 void set_unsafe_le128(const void * pvalue
);
90 ref(json
& js
, const char * key
);
91 ref(const ref
& base
, const char * key
);
92 ref(const ref
& base
, int index
);
93 ref(const ref
& base
, const char * /*dummy*/, const char * key_suffix
);
99 /// Return reference to element of top level object.
100 ref
operator[](const char * key
)
101 { return ref(*this, key
); };
103 /// Enable/disable JSON output.
104 void enable(bool yes
= true)
107 /// Return true if enabled.
108 bool is_enabled() const
109 { return m_enabled
; }
111 /// Enable/disable extra string output for safe integers also.
112 void set_verbose(bool yes
= true)
115 /// Return true if any 128-bit value has been output.
116 bool has_uint128_output() const
117 { return m_uint128_output
; }
119 /// Options for print().
120 struct print_options
{
121 bool pretty
; //< Pretty-print output.
122 bool sorted
; //< Sort object keys.
123 bool flat
; //< Print flat format.
125 : pretty(false), sorted(false), flat(false) { }
128 /// Print JSON tree to a file.
129 void print(FILE * f
, const print_options
& options
) const;
133 nt_unset
, nt_object
, nt_array
,
134 nt_bool
, nt_int
, nt_uint
, nt_uint128
, nt_string
140 explicit node(const std::string
& key_
);
145 uint64_t intval
, intval_hi
;
149 std::vector
<node
*> childs
;
150 typedef std::map
<std::string
, unsigned> keymap
;
156 const_iterator(const node
* node_p
, bool sorted
);
158 unsigned array_index() const;
160 const node
* operator*() const;
163 const node
* m_node_p
;
165 unsigned m_child_idx
;
166 keymap::const_iterator m_key_iter
;
169 #if __cplusplus >= 201103
170 node(const node
&) = delete;
171 void operator=(const node
&) = delete;
173 private: node(const node
&); void operator=(const node
&);
179 bool m_uint128_output
;
183 node
* find_or_create_node(const node_path
& path
, node_type type
);
185 void set_bool(const node_path
& path
, bool value
);
186 void set_int64(const node_path
& path
, int64_t value
);
187 void set_uint64(const node_path
& path
, uint64_t value
);
188 void set_uint128(const node_path
& path
, uint64_t value_hi
, uint64_t value_lo
);
189 void set_string(const node_path
& path
, const std::string
& value
);
191 static void print_json(FILE * f
, bool pretty
, bool sorted
, const node
* p
, int level
);
192 static void print_flat(FILE * f
, bool sorted
, const node
* p
, std::string
& path
);
195 #endif // JSON_H_CVSID