Util: Add Parser and Serializer to Value friends

This commit is contained in:
Riyyi
2022-07-12 12:07:49 +02:00
parent ad137576a8
commit b76223693b
3 changed files with 12 additions and 10 deletions
+4 -4
View File
@@ -338,7 +338,7 @@ Value Parser::getArray()
}
else if (token.type == Token::Type::BracketClose) {
// Trailing comma
if (array.asArray().size() > 0) {
if (array.m_value.asArray->size() > 0) {
reportError(m_tokens->at(m_index - 1), "invalid comma, expecting ']'");
break;
}
@@ -396,7 +396,7 @@ Value Parser::getObject()
token = consume();
if (token.type == Token::Type::BraceClose) {
// Trailing comma
if (object.asObject().size() > 0) {
if (object.m_value.asObject->size() > 0) {
reportError(m_tokens->at(m_index - 1), "invalid comma, expecting '}'");
}
// Empty object
@@ -410,13 +410,13 @@ Value Parser::getObject()
// Find member name
m_index--;
Value tmpName = getString();
if (tmpName.type() != Value::Type::String) {
if (tmpName.m_type != Value::Type::String) {
seekForward(Token::Type::BraceClose);
break;
}
// Check if name exists in hashmap
name = tmpName.asString();
name = *tmpName.m_value.asString;
if (unique.find(name) != unique.end()) {
reportError(token, "duplicate name '" + token.symbol + "', names should be unique");
break;
+6 -6
View File
@@ -38,21 +38,21 @@ std::string Serializer::dump()
std::string Serializer::dumpHelper(const Value& value, const uint32_t indentLevel)
{
switch (value.type()) {
switch (value.m_type) {
case Value::Type::Null:
return "null";
break;
case Value::Type::Bool:
return value.asBool() ? "true" : "false";
return value.m_value.asBool ? "true" : "false";
break;
case Value::Type::Number: {
std::ostringstream os;
os << value.asDouble();
os << value.m_value.asDouble;
return os.str();
break;
}
case Value::Type::String:
return "\"" + value.asString() + "\"";
return "\"" + *value.m_value.asString + "\"";
break;
case Value::Type::Array:
return dumpArray(value, indentLevel);
@@ -77,7 +77,7 @@ std::string Serializer::dumpArray(const Value& value, const uint32_t indentLevel
result += '\n';
}
auto values = value.asArray().elements();
auto values = value.m_value.asArray->elements();
for (auto it = values.begin(); it != values.end(); ++it) {
result += std::string(m_indent * (indentLevel + 1), m_indentCharacter);
result += dumpHelper(*it, indentLevel + 1);
@@ -110,7 +110,7 @@ std::string Serializer::dumpObject(const Value& value, const uint32_t indentLeve
result += '\n';
}
auto members = value.asObject().members();
auto members = value.m_value.asObject->members();
for (auto it = members.begin(); it != members.end(); ++it) {
result += std::string(m_indent * (indentLevel + 1), m_indentCharacter);
result += "\"" + it->first + "\":";
+2
View File
@@ -25,6 +25,8 @@ class Object;
class Value {
private:
friend Detail::jsonConstructor;
friend class Parser;
friend class Serializer;
public:
enum class Type {