39 std::vector<Value>
arr;
41 std::map<std::string, Value>
obj;
61 auto it =
obj.find(key);
62 return it ==
obj.end() ? nullptr : &it->second;
66 double as_num(
double dflt = 0.0)
const
68 return type == NUM ?
num : dflt;
84 std::string
as_str(
const std::string & dflt =
"")
const
86 return type == STR ?
str : dflt;
90 double num_at(
const std::string & k,
double dflt = 0.0)
const
93 return v ? v->
as_num(dflt) : dflt;
97 bool bool_at(
const std::string & k,
bool dflt =
false)
const
100 return v ? v->
as_bool(dflt) : dflt;
104 std::string
str_at(
const std::string & k,
const std::string & dflt =
"")
const
107 return v ? v->
as_str(dflt) : dflt;
127 if (!value(out, 0)) {
136 const std::string & s_;
140 static constexpr int kMaxDepth = 32;
144 while (i_ < s_.size() && std::isspace(
static_cast<unsigned char>(s_[i_]))) {
151 return i_ < s_.size() ? s_[i_] :
'\0';
154 bool value(Value & v,
int depth)
156 if (depth > kMaxDepth) {
163 return object(v, depth);
165 return array(v, depth);
167 return string_val(v);
174 if (c ==
'-' || std::isdigit(
static_cast<unsigned char>(c))) {
181 bool object(Value & v,
int depth)
197 if (!string_val(key)) {
206 if (!value(val, depth + 1)) {
209 v.obj[key.str] = std::move(val);
224 bool array(Value & v,
int depth)
236 if (!value(item, depth + 1)) {
239 v.arr.push_back(std::move(item));
254 bool string_val(Value & v)
260 while (i_ < s_.size()) {
263 v.str = std::move(out);
266 if (c ==
'\\' && i_ < s_.size()) {
291 for (
int k = 0; k < 4 && i_ < s_.size(); ++k) {
292 out.push_back(s_[i_++]);
307 bool number(Value & v)
313 while (i_ < s_.size() && (std::isdigit(
static_cast<unsigned char>(s_[i_])) || s_[i_] ==
'.' || s_[i_] ==
'e' ||
314 s_[i_] ==
'E' || s_[i_] ==
'+' || s_[i_] ==
'-'))
319 v.num = std::strtod(s_.substr(start, i_ - start).c_str(),
nullptr);
323 bool boolean(Value & v)
325 if (s_.compare(i_, 4,
"true") == 0) {
326 v.type = Value::BOOL;
331 if (s_.compare(i_, 5,
"false") == 0) {
332 v.type = Value::BOOL;
340 bool null_val(Value & v)
342 if (s_.compare(i_, 4,
"null") == 0) {
351inline bool parse(
const std::string & text,
Value & out)
Recursive-descent parser for the flat JSON schema of the ESP32 /state.json.
Definition json_lite.hpp:114
Parser(const std::string &s)
Parses s; the string must outlive the parser.
Definition json_lite.hpp:117
bool parse(Value &out)
Parses the document into out.
Definition json_lite.hpp:124
A parsed JSON value: null, bool, number, string, array or object.
Definition json_lite.hpp:18
bool as_bool(bool dflt=false) const
Bool value, a NUM compared against zero, or dflt for other types.
Definition json_lite.hpp:72
double num
Value when type is NUM.
Definition json_lite.hpp:35
std::vector< Value > arr
Elements when type is ARR.
Definition json_lite.hpp:39
bool bool_at(const std::string &k, bool dflt=false) const
as_bool() of member k, or dflt if the member is absent.
Definition json_lite.hpp:97
double as_num(double dflt=0.0) const
Number value, or dflt unless type is NUM.
Definition json_lite.hpp:66
std::string as_str(const std::string &dflt="") const
String value, or dflt unless type is STR.
Definition json_lite.hpp:84
bool b
Value when type is BOOL.
Definition json_lite.hpp:33
Type type
Active type; selects which of the members below is meaningful.
Definition json_lite.hpp:31
bool is_arr() const
True when type is ARR.
Definition json_lite.hpp:50
bool is_obj() const
True when type is OBJ.
Definition json_lite.hpp:44
double num_at(const std::string &k, double dflt=0.0) const
as_num() of member k, or dflt if the member is absent.
Definition json_lite.hpp:90
Type
JSON type held by a Value.
Definition json_lite.hpp:21
std::map< std::string, Value > obj
Members when type is OBJ.
Definition json_lite.hpp:41
std::string str_at(const std::string &k, const std::string &dflt="") const
as_str() of member k, or dflt if the member is absent.
Definition json_lite.hpp:104
std::string str
Value when type is STR.
Definition json_lite.hpp:37
const Value * find(const std::string &key) const
Returns the member named key, or nullptr if absent or not an object.
Definition json_lite.hpp:56