Skip to content

Commit

Permalink
Merge pull request #63 from Barenboim/main
Browse files Browse the repository at this point in the history
  • Loading branch information
chanchann authored Dec 24, 2023
2 parents 41c4a3c + 0c1a6e7 commit 5d72793
Show file tree
Hide file tree
Showing 4 changed files with 371 additions and 274 deletions.
71 changes: 65 additions & 6 deletions Json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ Json::Json(const std::string& str, bool parse_flag) : parent_(nullptr)
allocated_ = node_ == nullptr ? false : true;
}

Json::Json(const std::vector<std::string>& val)
: node_(json_value_create(JSON_VALUE_ARRAY)),
parent_(nullptr), allocated_(true)
{
json_array_t* arr = json_value_array(node_);
for (const auto& str : val)
{
json_array_append(arr, JSON_VALUE_STRING, str.c_str());
}
}

Json::~Json()
{
destroy_node(&node_);
Expand Down Expand Up @@ -378,6 +389,11 @@ bool Json::can_obj_push_back()

void Json::push_back(const std::string& key, bool val)
{
if (is_placeholder())
{
*this = Json::Object{{key, val}};
return;
}
if (!can_obj_push_back())
{
return;
Expand All @@ -389,6 +405,11 @@ void Json::push_back(const std::string& key, bool val)

void Json::push_back(const std::string& key, std::nullptr_t val)
{
if (is_placeholder())
{
*this = Json::Object{{key, val}};
return;
}
if (!can_obj_push_back())
{
return;
Expand All @@ -404,6 +425,11 @@ void Json::push_back(const std::string& key, const std::string& val)

void Json::push_back(const std::string& key, const char* val)
{
if (is_placeholder())
{
*this = Json::Object{{key, val}};
return;
}
if (!can_obj_push_back())
{
return;
Expand All @@ -414,6 +440,11 @@ void Json::push_back(const std::string& key, const char* val)

void Json::push_back(const std::string& key, const std::vector<std::string>& val)
{
if (is_placeholder())
{
*this = Json::Object{{key, val}};
return;
}
if (!can_obj_push_back())
{
return;
Expand All @@ -430,6 +461,11 @@ void Json::push_back(const std::string& key, const std::vector<std::string>& val

void Json::push_back(const std::string& key, const Json& val)
{
if (is_placeholder())
{
*this = Json::Object{{key, val}};
return;
}
if (!can_obj_push_back())
{
return;
Expand Down Expand Up @@ -552,15 +588,18 @@ void Json::normal_push_back(const std::string& key,
{
json_object_t* obj = json_value_object(parent_);
const json_value_t* find = json_object_find(key.c_str(), obj);
const json_value_t* v;
if (find == nullptr)
{
json_object_append(obj, key.c_str(), JSON_VALUE_NULL);
return;
v = json_object_append(obj, key.c_str(), JSON_VALUE_ARRAY);
}
else
{
v = json_object_insert_before(find, obj, key.c_str(),
JSON_VALUE_ARRAY);
json_value_t* remove_val = json_object_remove(find, obj);
json_value_destroy(remove_val);
}
const json_value_t *v = json_object_insert_before(find, obj, key.c_str(),
JSON_VALUE_ARRAY);
json_value_t* remove_val = json_object_remove(find, obj);
json_value_destroy(remove_val);
json_array_t* arr = json_value_array(v);
for (const auto& str : val)
json_array_append(arr, JSON_VALUE_STRING, str.c_str());
Expand Down Expand Up @@ -599,6 +638,11 @@ Json Json::copy() const

void Json::push_back(bool val)
{
if (is_placeholder())
{
*this = Json::Array{{val}};
return;
}
if (!can_arr_push_back())
{
return;
Expand All @@ -610,6 +654,11 @@ void Json::push_back(bool val)

void Json::push_back(std::nullptr_t val)
{
if (is_placeholder())
{
*this = Json::Array{{val}};
return;
}
if (!can_arr_push_back())
{
return;
Expand All @@ -632,6 +681,11 @@ void Json::push_back(const std::vector<std::string>& val)

void Json::push_back(const char* val)
{
if (is_placeholder())
{
*this = Json::Array{{val}};
return;
}
if (!can_arr_push_back())
{
return;
Expand All @@ -642,6 +696,11 @@ void Json::push_back(const char* val)

void Json::push_back(const Json& val)
{
if (is_placeholder())
{
*this = Json::Array{{val}};
return;
}
if (!can_arr_push_back())
{
return;
Expand Down
21 changes: 21 additions & 0 deletions Json.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ class Json
bool>::type = true>
void push_back(const std::string& key, const T& val)
{
if (is_placeholder())
{
*this = Json::Object{{key, val}};
return;
}
if (!can_obj_push_back())
{
return;
Expand All @@ -219,6 +224,11 @@ class Json
bool>::type = true>
void push_back(const std::string& key, const T& val)
{
if (is_placeholder())
{
*this = Json::Object{{key, val}};
return;
}
if (!can_obj_push_back())
{
return;
Expand Down Expand Up @@ -345,6 +355,11 @@ class Json
bool>::type = true>
void push_back(T val)
{
if (is_placeholder())
{
*this = Json::Array{{val}};
return;
}
if (!can_arr_push_back())
{
return;
Expand All @@ -359,6 +374,11 @@ class Json
bool>::type = true>
void push_back(const T& val)
{
if (is_placeholder())
{
*this = Json::Array{{val}};
return;
}
if (!can_arr_push_back())
{
return;
Expand Down Expand Up @@ -712,6 +732,7 @@ class Json
Json(double val);
Json(int val);
Json(bool val);
Json(const std::vector<std::string>& val);

// For parse
Json(const std::string& str, bool parse_flag);
Expand Down
Loading

0 comments on commit 5d72793

Please sign in to comment.