Commit e0f1de6a authored by aligungr's avatar aligungr

CLI improvements

parent d9ed8194
......@@ -37,7 +37,7 @@ Json ToJson(const GnbConfig &v)
Json ToJson(const NgapAmfContext &v)
{
return Json::Obj({
{"ctx-id", v.ctxId},
{"id", v.ctxId},
{"name", v.amfName},
{"address", v.address + ":" + std::to_string(v.port)},
{"state", ToJson(v.state).str()},
......
......@@ -304,9 +304,8 @@ Json Json::Arr(std::initializer_list<Json> &&elements)
{
Json json{};
json.m_type = Type::ARRAY;
int index = 0;
for (auto &item : elements)
json.m_children[std::to_string(index++)] = item;
json.push(item);
return json;
}
......@@ -314,9 +313,8 @@ Json Json::Arr(const std::vector<Json> &elements)
{
Json json{};
json.m_type = Type::ARRAY;
int index = 0;
for (auto &item : elements)
json.m_children[std::to_string(index++)] = item;
json.push(item);
return json;
}
......@@ -324,9 +322,8 @@ Json Json::Arr(std::vector<Json> &&elements)
{
Json json{};
json.m_type = Type::ARRAY;
int index = 0;
for (auto &item : elements)
json.m_children[std::to_string(index++)] = std::move(item);
json.push(std::move(item));
return json;
}
......@@ -335,7 +332,7 @@ Json Json::Obj(std::initializer_list<std::pair<std::string, Json>> &&elements)
Json json{};
json.m_type = Type::OBJECT;
for (auto &item : elements)
json.m_children[item.first] = item.second;
json.put(item.first, item.second);
return json;
}
......@@ -343,14 +340,18 @@ void Json::push(Json element)
{
if (m_type != Type::ARRAY)
return;
m_children[std::to_string(m_children.size())] = std::move(element);
m_children.emplace_back(std::to_string(m_children.size()), std::move(element));
}
void Json::put(std::string key, Json value)
{
if (m_type != Type::OBJECT)
return;
m_children[std::move(key)] = std::move(value);
m_children.emplace_back(std::move(key), std::move(value));
}
Json ToJson(nullptr_t)
......
......@@ -34,12 +34,14 @@ class Json
std::string m_strVal{};
int64_t m_intVal{};
// TODO: Switch to some ordered map (preserving insertion order)
std::map<std::string, Json> m_children{};
// 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
// O(n) if we compare to check the key is already present)
std::vector<std::pair<std::string, Json>> m_children{};
private:
typedef decltype(m_children.begin()) iterator;
typedef decltype(const_cast<const std::map<std::string, Json> &>(m_children).begin()) const_iterator;
typedef decltype(const_cast<const std::vector<std::pair<std::string, Json>> &>(m_children).begin()) const_iterator;
public:
Json();
......
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