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 5414405
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
12 changes: 6 additions & 6 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 @@ -585,7 +585,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 @@ -786,7 +786,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 +799,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
2 changes: 1 addition & 1 deletion Json.h
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ class Json
// can convert to any type, just like a placeholder
bool is_placeholder() const
{
return is_null() && parent_ != nullptr;
return json_value_type(node_) == -1;
}

void destroy_node(json_value_t** node)
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 5414405

Please sign in to comment.