Commit adbafa53 authored by aligungr's avatar aligungr

CLI improvements

parent e0f1de6a
...@@ -349,7 +349,15 @@ void Json::put(std::string key, Json value) ...@@ -349,7 +349,15 @@ void Json::put(std::string key, Json value)
if (m_type != Type::OBJECT) if (m_type != Type::OBJECT)
return; return;
// Replace if the key is already present
for (auto &item : m_children)
{
if (item.first == key)
{
item.second = std::move(value);
return;
}
}
m_children.emplace_back(std::move(key), std::move(value)); m_children.emplace_back(std::move(key), std::move(value));
} }
......
...@@ -34,9 +34,11 @@ class Json ...@@ -34,9 +34,11 @@ class Json
std::string m_strVal{}; std::string m_strVal{};
int64_t m_intVal{}; int64_t m_intVal{};
// Holding children via vector instead of map/unordered_map (because insertion order is needed to be preserved). // - Holding children via vector instead of map/unordered_map (because insertion order is needed to be preserved).
// Therefore remove operation will be O(n), but performance is not a concern for JSONs. (and add operation is also // Therefore add/remove operation is O(n), but performance is not a concern for JSONs. (Add operation is also
// O(n) if we compare to check the key is already present) // O(n) since we compare to check the key is already present)
// - NOTE: We're using Json type itself here which is incomplete herein. But C++17 allows std::vector, std::list,
// and std::forward_list to have incomplete types. For older versions we would need pointer etc.
std::vector<std::pair<std::string, Json>> m_children{}; std::vector<std::pair<std::string, Json>> m_children{};
private: private:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment