From b41bba72c988a7cd9f5deb4d1c678f59bdac2205 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Wed, 29 Jun 2022 15:32:25 +0200 Subject: [PATCH] Util: Rename Object key -> name --- src/util/json/object.h | 18 +++++++++--------- src/util/json/parser.cpp | 32 +++++++++++++++++--------------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/util/json/object.h b/src/util/json/object.h index dc4b769..b02e84d 100644 --- a/src/util/json/object.h +++ b/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& members() const { return m_members; } diff --git a/src/util/json/parser.cpp b/src/util/json/parser.cpp index c0aec1b..1df85ae 100644 --- a/src/util/json/parser.cpp +++ b/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 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 + "'");