Util: Add JSON accessors
This commit is contained in:
@@ -29,6 +29,8 @@ public:
|
||||
m_values.emplace_back(std::move(value));
|
||||
}
|
||||
|
||||
const std::vector<Value>& values() const { return m_values; }
|
||||
|
||||
private:
|
||||
std::vector<Value> m_values;
|
||||
};
|
||||
|
||||
@@ -28,7 +28,7 @@ Parser::~Parser()
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
void Parser::parse()
|
||||
Value Parser::parse()
|
||||
{
|
||||
Lexer lexer(m_input);
|
||||
lexer.analyze();
|
||||
@@ -81,6 +81,8 @@ void Parser::parse()
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
@@ -24,7 +24,7 @@ public:
|
||||
Parser(const std::string& input);
|
||||
virtual ~Parser();
|
||||
|
||||
void parse();
|
||||
Value parse();
|
||||
|
||||
private:
|
||||
Token peek();
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#include <cassert> // assert
|
||||
#include <string>
|
||||
#include <utility> // move
|
||||
|
||||
@@ -64,6 +65,18 @@ Value::Value(const Object& value)
|
||||
m_value.asObject = new Object(value);
|
||||
}
|
||||
|
||||
Value Value::operator[](size_t index)
|
||||
{
|
||||
assert(m_type == Type::Array);
|
||||
return m_value.asArray->values().at(index);
|
||||
}
|
||||
|
||||
Value Value::operator[](const std::string& key)
|
||||
{
|
||||
assert(m_type == Type::Object);
|
||||
return m_value.asObject->members().at(key);
|
||||
}
|
||||
|
||||
// ------------------------------------------
|
||||
|
||||
void Value::clear()
|
||||
|
||||
@@ -40,6 +40,10 @@ public:
|
||||
Value(const Array& value);
|
||||
Value(const Object& value);
|
||||
|
||||
// Array index operator
|
||||
Value operator[](size_t index);
|
||||
Value operator[](const std::string& key);
|
||||
|
||||
bool asBool() const { return m_value.asBool; }
|
||||
double asDouble() const { return m_value.asDouble; }
|
||||
const std::string& asString() const { return *m_value.asString; }
|
||||
|
||||
Reference in New Issue
Block a user