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