Browse Source

Util: Rename Object key -> name

master
Riyyi 3 years ago
parent
commit
b41bba72c9
  1. 18
      src/util/json/object.h
  2. 32
      src/util/json/parser.cpp

18
src/util/json/object.h

@ -25,22 +25,22 @@ public:
{ {
} }
void emplace(const std::string& key, Value value) void emplace(const std::string& name, Value value)
{ {
m_members.emplace(key, std::move(value)); m_members.emplace(name, std::move(value));
} }
Value& at(const std::string& key) Value& at(const std::string& name)
{ {
if (m_members.find(key) == m_members.end()) { if (m_members.find(name) == m_members.end()) {
emplace(key, {}); emplace(name, {});
} }
return m_members.at(key); return m_members.at(name);
} }
Value& operator[](const std::string& key) { return at(key); } Value& operator[](const std::string& name) { return at(name); }
const Value& at(const std::string& key) const { return m_members.at(key); } const Value& at(const std::string& name) const { return m_members.at(name); }
const Value& operator[](const std::string& key) const { return m_members.at(key); } const Value& operator[](const std::string& name) const { return m_members.at(name); }
size_t size() const { return m_members.size(); } size_t size() const { return m_members.size(); }
const std::map<std::string, Value>& members() const { return m_members; } const std::map<std::string, Value>& members() const { return m_members; }

32
src/util/json/parser.cpp

@ -307,7 +307,7 @@ Value Parser::getObject()
Value object = Value::Type::Object; Value object = Value::Type::Object;
Token token; Token token;
std::string key; std::string name;
std::map<std::string, uint8_t> unique; std::map<std::string, uint8_t> unique;
for (;;) { for (;;) {
token = consume(); token = consume();
@ -319,20 +319,20 @@ Value Parser::getObject()
} }
break; break;
} }
// Find string key // Find string name
if (token.type != Token::Type::String) { if (token.type != Token::Type::String) {
reportError(token, "expecting string, or '}' not '" + token.symbol + "'"); reportError(token, "expecting string, or '}' not '" + token.symbol + "'");
break; break;
} }
// Check if key exists in hashmap // Check if name exists in hashmap
key = token.symbol; name = token.symbol;
if (unique.find(key) != unique.end()) { if (unique.find(name) != unique.end()) {
reportError(token, "duplicate key '" + token.symbol + "', names should be unique"); reportError(token, "duplicate name '" + token.symbol + "', names should be unique");
break; break;
} }
// Add key to hashmap // Add name to hashmap
unique.insert({ key, 0 }); unique.insert({ name, 0 });
// Find : // Find :
token = consume(); token = consume();
@ -344,26 +344,28 @@ Value Parser::getObject()
// Add member (name:value pair) to object // Add member (name:value pair) to object
token = consume(); token = consume();
if (token.type == Token::Type::Literal) { if (token.type == 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", name.c_str(), token.symbol.c_str(), (int)token.type);
m_index--; m_index--;
object[key] = getLiteral(); object[name] = getLiteral();
} }
else if (token.type == Token::Type::Number) { else if (token.type == Token::Type::Number) {
printf("Adding number to object.. k:{%s}, v:{%s}, t:{%d} -> %f\n", name.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", name.c_str(), token.symbol.c_str(), (int)token.type, std::stod(token.symbol));
m_index--; m_index--;
object[key] = getNumber(); object[name] = getNumber();
} }
else if (token.type == Token::Type::String) { else if (token.type == 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); #ifdef JSON_DEBUG
object[key] = token.symbol; printf("Adding string to object.. k:{%s}, v:{%s}, t:{%d}\n", name.c_str(), token.symbol.c_str(), (int)token.type);
#endif
object[name] = token.symbol;
} }
else if (token.type == Token::Type::BracketOpen) { else if (token.type == Token::Type::BracketOpen) {
m_index--; m_index--;
object[key] = getArray(); object[name] = getArray();
} }
else if (token.type == Token::Type::BraceOpen) { else if (token.type == Token::Type::BraceOpen) {
m_index--; m_index--;
object[key] = getObject(); object[name] = getObject();
} }
else { else {
reportError(token, "expecting value, not '" + token.symbol + "'"); reportError(token, "expecting value, not '" + token.symbol + "'");

Loading…
Cancel
Save