Browse Source

Util: Fix Parser Json::Value construction

master
Riyyi 2 years ago
parent
commit
e5978310bf
  1. 2
      src/util/json/array.h
  2. 36
      src/util/json/parser.cpp

2
src/util/json/array.h

@ -29,7 +29,7 @@ public:
{ {
} }
void emplace(Value value) void emplace_back(Value value)
{ {
m_values.emplace_back(std::move(value)); m_values.emplace_back(std::move(value));
} }

36
src/util/json/parser.cpp

@ -114,7 +114,7 @@ Value Parser::getArray()
size_t index = m_index; size_t index = m_index;
m_index++; m_index++;
Array array; Value array;
Token token; Token token;
for (;;) { for (;;) {
@ -124,30 +124,30 @@ Value Parser::getArray()
case Token::Type::Literal: case Token::Type::Literal:
printf("Adding literal to array.. v:{%s}, t:{%d}\n", token.symbol.c_str(), (int)token.type); printf("Adding literal to array.. v:{%s}, t:{%d}\n", token.symbol.c_str(), (int)token.type);
if (token.symbol == "null") { if (token.symbol == "null") {
array.emplace(Value {}); array.emplace_back(nullptr);
} }
else if (token.symbol == "true") { else if (token.symbol == "true") {
array.emplace(Value { true }); array.emplace_back(true);
} }
else if (token.symbol == "false") { else if (token.symbol == "false") {
array.emplace(Value { false }); array.emplace_back(false);
} }
break; break;
case Token::Type::Number: case Token::Type::Number:
printf("Adding number to array.. v:{%s}, t:{%d} -> %f\n", token.symbol.c_str(), (int)token.type, std::stod(token.symbol)); printf("Adding number to array.. v:{%s}, t:{%d} -> %f\n", token.symbol.c_str(), (int)token.type, std::stod(token.symbol));
array.emplace(Value { std::stod(token.symbol) }); array.emplace_back(std::stod(token.symbol));
break; break;
case Token::Type::String: case Token::Type::String:
printf("Adding string to array.. v:{%s}, t:{%d}\n", token.symbol.c_str(), (int)token.type); printf("Adding string to array.. v:{%s}, t:{%d}\n", token.symbol.c_str(), (int)token.type);
array.emplace(Value { token.symbol }); array.emplace_back(token.symbol);
break; break;
case Token::Type::BracketOpen: case Token::Type::BracketOpen:
m_index--; m_index--;
array.emplace(Value { getArray() }); array.emplace_back(getArray());
break; break;
case Token::Type::BraceOpen: case Token::Type::BraceOpen:
m_index--; m_index--;
array.emplace(Value { getObject() }); array.emplace_back(getObject());
break; break;
default: default:
// Error! // Error!
@ -170,7 +170,7 @@ Value Parser::getArray()
} }
} }
return Value { array }; return array;
} }
Value Parser::getObject() Value Parser::getObject()
@ -178,7 +178,7 @@ Value Parser::getObject()
size_t index = m_index; size_t index = m_index;
m_index++; m_index++;
Object object; Value object;
Token token; Token token;
std::string key; std::string key;
@ -227,30 +227,30 @@ Value Parser::getObject()
case Token::Type::Literal: case Token::Type::Literal:
printf("Adding literal to object.. k:{%s}, v:{%s}, t:{%d}\n", key.c_str(), token.symbol.c_str(), (int)token.type); printf("Adding literal to object.. k:{%s}, v:{%s}, t:{%d}\n", key.c_str(), token.symbol.c_str(), (int)token.type);
if (token.symbol == "null") { if (token.symbol == "null") {
object.emplace(key, Value {}); object[key] = nullptr;
} }
else if (token.symbol == "true") { else if (token.symbol == "true") {
object.emplace(key, Value { true }); object[key] = true;
} }
else if (token.symbol == "false") { else if (token.symbol == "false") {
object.emplace(key, Value { false }); object[key] = false;
} }
break; break;
case Token::Type::Number: case Token::Type::Number:
printf("Adding number to object.. k:{%s}, v:{%s}, t:{%d} -> %f\n", key.c_str(), token.symbol.c_str(), (int)token.type, std::stod(token.symbol)); printf("Adding number to object.. k:{%s}, v:{%s}, t:{%d} -> %f\n", key.c_str(), token.symbol.c_str(), (int)token.type, std::stod(token.symbol));
object.emplace(key, Value { std::stod(token.symbol) }); object[key] = std::stod(token.symbol);
break; break;
case Token::Type::String: case Token::Type::String:
printf("Adding string to object.. k:{%s}, v:{%s}, t:{%d}\n", key.c_str(), token.symbol.c_str(), (int)token.type); printf("Adding string to object.. k:{%s}, v:{%s}, t:{%d}\n", key.c_str(), token.symbol.c_str(), (int)token.type);
object.emplace(key, Value { token.symbol }); object[key] = token.symbol;
break; break;
case Token::Type::BracketOpen: case Token::Type::BracketOpen:
m_index--; m_index--;
object.emplace(key, Value { getArray() }); object[key] = getArray();
break; break;
case Token::Type::BraceOpen: case Token::Type::BraceOpen:
m_index--; m_index--;
object.emplace(key, Value { getObject() }); object[key] = getObject();
break; break;
default: default:
// Error! // Error!
@ -273,7 +273,7 @@ Value Parser::getObject()
} }
} }
return Value { object }; return object;
} }
} // namespace Json } // namespace Json

Loading…
Cancel
Save