Browse Source

Util: Add Parser and Serializer to Value friends

master
Riyyi 2 years ago
parent
commit
b76223693b
  1. 8
      src/util/json/parser.cpp
  2. 12
      src/util/json/serializer.cpp
  3. 2
      src/util/json/value.h

8
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;

12
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 + "\":";

2
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 {

Loading…
Cancel
Save