Skip to content

Commit

Permalink
[json] Port json parser from swssconfig to swss-common so that other
Browse files Browse the repository at this point in the history
component can take advantage of this tool
  • Loading branch information
Stephen Sun authored and stephenxs committed Aug 14, 2020
1 parent e95f0e7 commit 2a5e8b9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
62 changes: 62 additions & 0 deletions common/json.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <stdlib.h>
#include <sstream>
#include <fstream>
#include <iostream>
#include <limits>

#include "common/json.h"
Expand Down Expand Up @@ -36,4 +39,63 @@ void JSon::readJson(const string &jsonstr, vector<FieldValueTuple> &fv)
}
}

bool JSon::loadJsonFromFile(ifstream &fs, vector<KeyOpFieldsValuesTuple> &db_items)
{
nlohmann::json json_array;
fs >> json_array;

if (!json_array.is_array())
{
SWSS_LOG_ERROR("Root element must be an array.");
return false;
}

for (size_t i = 0; i < json_array.size(); i++)
{
auto &arr_item = json_array[i];

if (arr_item.is_object())
{
if (el_count != arr_item.size())
{
SWSS_LOG_ERROR("Chlid elements must have both key and op entry. %s",
arr_item.dump().c_str());
return false;
}

db_items.push_back(KeyOpFieldsValuesTuple());
auto &cur_db_item = db_items[db_items.size() - 1];

for (nlohmann::json::iterator child_it = arr_item.begin(); child_it != arr_item.end(); child_it++) {
auto cur_obj_key = child_it.key();
auto &cur_obj = child_it.value();

if (cur_obj.is_object()) {
kfvKey(cur_db_item) = cur_obj_key;
for (nlohmann::json::iterator cur_obj_it = cur_obj.begin(); cur_obj_it != cur_obj.end(); cur_obj_it++)
{
string field_str = cur_obj_it.key();
string value_str;
if ((*cur_obj_it).is_number())
value_str = to_string((*cur_obj_it).get<int>());
else if ((*cur_obj_it).is_string())
value_str = (*cur_obj_it).get<string>();
kfvFieldsValues(cur_db_item).push_back(FieldValueTuple(field_str, value_str));
}
}
else
{
kfvOp(cur_db_item) = cur_obj.get<string>();
}
}
}
else
{
SWSS_LOG_ERROR("Child elements must be objects. element:%s", arr_item.dump().c_str());
return false;
}
}
return true;
}

}
8 changes: 6 additions & 2 deletions common/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __JSON__

#include <string>
#include <fstream>
#include <vector>

#include "table.h"
Expand All @@ -10,9 +11,12 @@ namespace swss {

class JSon
{
private:
static const int el_count = 2;
public:
static std::string buildJson(const std::vector<FieldValueTuple> &fv);
static void readJson(const std::string &json, std::vector<FieldValueTuple> &fv);
static std::string buildJson(const std::vector<FieldValueTuple> &fv);
static void readJson(const std::string &json, std::vector<FieldValueTuple> &fv);
static bool loadJsonFromFile(std::ifstream &fs, std::vector<KeyOpFieldsValuesTuple> &db_items);
};

}
Expand Down

0 comments on commit 2a5e8b9

Please sign in to comment.