From b76223693bd54d098b3858ec960816d021242990 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Tue, 12 Jul 2022 12:07:49 +0200 Subject: [PATCH] Util: Add Parser and Serializer to Value friends --- src/util/json/parser.cpp | 8 ++++---- src/util/json/serializer.cpp | 12 ++++++------ src/util/json/value.h | 2 ++ 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/util/json/parser.cpp b/src/util/json/parser.cpp index 12ca396..5e8eccc 100644 --- a/src/util/json/parser.cpp +++ b/src/util/json/parser.cpp @@ -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; diff --git a/src/util/json/serializer.cpp b/src/util/json/serializer.cpp index 7e17c17..998704e 100644 --- a/src/util/json/serializer.cpp +++ b/src/util/json/serializer.cpp @@ -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 + "\":"; diff --git a/src/util/json/value.h b/src/util/json/value.h index 66f3674..21036f2 100644 --- a/src/util/json/value.h +++ b/src/util/json/value.h @@ -25,6 +25,8 @@ class Object; class Value { private: friend Detail::jsonConstructor; + friend class Parser; + friend class Serializer; public: enum class Type {