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()) {
emplace(key, {});
if (m_members.find(name) == m_members.end()) {
emplace(name, {});
}
return m_members.at(key);
return m_members.at(name);
}
Value& operator[](const std::string& key) { return at(key); }
const Value& at(const std::string& key) const { return m_members.at(key); }
const Value& operator[](const std::string& key) const { return m_members.at(key); }
Value& operator[](const std::string& name) { return at(name); }
const Value& at(const std::string& name) const { return m_members.at(name); }
const Value& operator[](const std::string& name) const { return m_members.at(name); }
size_t size() const { return m_members.size(); }
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;
Token token;
std::string key;
std::string name;
std::map<std::string, uint8_t> unique;
for (;;) {
token = consume();
@ -319,20 +319,20 @@ Value Parser::getObject()
}
break;
}
// Find string key
// Find string name
if (token.type != Token::Type::String) {
reportError(token, "expecting string, or '}' not '" + token.symbol + "'");
break;
}
// Check if key exists in hashmap
key = token.symbol;
if (unique.find(key) != unique.end()) {
reportError(token, "duplicate key '" + token.symbol + "', names should be unique");
// Check if name exists in hashmap
name = token.symbol;
if (unique.find(name) != unique.end()) {
reportError(token, "duplicate name '" + token.symbol + "', names should be unique");
break;
}
// Add key to hashmap
unique.insert({ key, 0 });
// Add name to hashmap
unique.insert({ name, 0 });
// Find :
token = consume();
@ -344,26 +344,28 @@ Value Parser::getObject()
// Add member (name:value pair) to object
token = consume();
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--;
object[key] = getLiteral();
object[name] = getLiteral();
}
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));
m_index--;
object[key] = getNumber();
object[name] = getNumber();
}
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);
object[key] = token.symbol;
#ifdef JSON_DEBUG
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) {
m_index--;
object[key] = getArray();
object[name] = getArray();
}
else if (token.type == Token::Type::BraceOpen) {
m_index--;
object[key] = getObject();
object[name] = getObject();
}
else {
reportError(token, "expecting value, not '" + token.symbol + "'");

Loading…
Cancel
Save