Skip to content

Commit

Permalink
Merge pull request #354 from holmes1412/master
Browse files Browse the repository at this point in the history
json: update Json in tools
  • Loading branch information
Barenboim authored Dec 25, 2023
2 parents a53af1e + 571a872 commit eeb280c
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 21 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ rules_proto_toolchains()

git_repository(
name = "workflow",
commit = "273134f0ffadbb40a59ce9155a1fde299cd9d68b",
commit = "ba98ed0945f8888f73350ddfe5d9681306103702",
remote = "https://github.com/sogou/workflow.git")

new_git_repository(
Expand Down
81 changes: 64 additions & 17 deletions tools/templates/config/Json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,22 @@ Json::Json(std::nullptr_t null)
{
}

Json::Json(double val)
: node_(json_value_create(JSON_VALUE_NUMBER, val)), parent_(nullptr),
allocated_(true)
{
}

Json::Json(int val)
: node_(json_value_create(JSON_VALUE_NUMBER, static_cast<double>(val))),
Json::Json(bool val)
: node_(val ? json_value_create(JSON_VALUE_TRUE)
: json_value_create(JSON_VALUE_FALSE)),
parent_(nullptr), allocated_(true)
{
}

Json::Json(bool val)
: node_(val ? json_value_create(JSON_VALUE_TRUE)
: json_value_create(JSON_VALUE_FALSE)),
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());
}
}

// for parse
Expand Down Expand Up @@ -378,6 +377,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 +393,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 +413,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 +428,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 +449,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 +576,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 +626,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 +642,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 +669,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 +684,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
30 changes: 28 additions & 2 deletions tools/templates/config/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 @@ -709,9 +729,15 @@ class Json
Json(const std::string& str);
Json(const char* str);
Json(std::nullptr_t null);
Json(double val);
Json(int val);
template <typename T, typename std::enable_if<detail::is_number<T>::value,
bool>::type = true>
Json(T val)
: node_(json_value_create(JSON_VALUE_NUMBER, static_cast<double>(val))),
parent_(nullptr), allocated_(true)
{
}
Json(bool val);
Json(const std::vector<std::string>& val);

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

0 comments on commit eeb280c

Please sign in to comment.