Skip to content

Commit

Permalink
feat: use JSON type: -1 as placeholder.
Browse files Browse the repository at this point in the history
  • Loading branch information
Barenboim committed Dec 22, 2023
1 parent 447edf7 commit be618e7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
33 changes: 16 additions & 17 deletions Json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ json_value_t* json_value_copy(const json_value_t* val)

// ------------------------ Constructor -------------------------
Json::Json()
: node_(json_value_create(JSON_VALUE_NULL)), parent_(nullptr),
: node_(json_value_create(-1)), parent_(nullptr),
allocated_(true)
{
}
Expand Down Expand Up @@ -231,7 +231,7 @@ std::string Json::dump(int spaces) const

Json Json::operator[](const char* key)
{
if (is_null() && is_root())
if (is_placeholder() && is_root())
{
// todo : need is_root here?
to_object();
Expand Down Expand Up @@ -369,7 +369,7 @@ bool Json::can_obj_push_back()
{
return true;
}
if (is_root() && is_null())
if (is_root() && is_placeholder())
{
to_object();
}
Expand Down Expand Up @@ -552,15 +552,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 All @@ -585,7 +588,7 @@ void Json::normal_push_back(const std::string& key, const Json& val)

bool Json::can_arr_push_back()
{
if (is_root() && is_null())
if (is_root() && is_placeholder())
{
to_array();
}
Expand Down Expand Up @@ -746,16 +749,12 @@ bool Json::empty() const
{
switch (type())
{
case -1:
case JSON_VALUE_NULL:
{
// null values are empty
return true;
}
case JSON_VALUE_ARRAY:
case JSON_VALUE_OBJECT:
{
return size() == 0;
}
default:
// all other types are nonempty
return false;
Expand Down Expand Up @@ -786,7 +785,7 @@ void Json::clear()

bool Json::to_object()
{
if (!allocated_ || !is_null())
if (!allocated_ || !is_placeholder())
{
// watcher and non-null type can't change type
return false;
Expand All @@ -799,7 +798,7 @@ bool Json::to_object()

bool Json::to_array()
{
if (!allocated_ || !is_null())
if (!allocated_ || !is_placeholder())
{
// watcher and non-null type can't change type
return false;
Expand Down
12 changes: 6 additions & 6 deletions Json.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ class Json
return type() == JSON_VALUE_STRING;
}

// can convert to any type, just like a placeholder
bool is_placeholder() const
{
return json_value_type(node_) == -1;
}

bool is_valid() const
{
return node_ != nullptr;
Expand Down Expand Up @@ -684,12 +690,6 @@ class Json

bool can_arr_push_back();

// can convert to any type, just like a placeholder
bool is_placeholder() const
{
return is_null() && parent_ != nullptr;
}

void destroy_node(json_value_t** node)
{
if (allocated_)
Expand Down
4 changes: 2 additions & 2 deletions test/json_obj_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ TEST(ObjTest, empty_obj)
TEST(ObjTest, one_level)
{
Json data;
EXPECT_TRUE(data.is_null());
EXPECT_TRUE(data.is_placeholder());
Json data_tmp = data["test"];
EXPECT_TRUE(data_tmp.is_null());
EXPECT_TRUE(data_tmp.is_placeholder());
data["test"] = 1.0;
EXPECT_EQ(data.type(), JSON_VALUE_OBJECT);
EXPECT_TRUE(data.is_object());
Expand Down

0 comments on commit be618e7

Please sign in to comment.