Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vztpv authored Apr 23, 2024
1 parent 1ccedf8 commit 786cf2a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
28 changes: 23 additions & 5 deletions claujson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,19 @@ namespace claujson {
return false;
}

bool Structured::change_key(uint64_t idx, const Value& new_key) {
if (this->is_object() && new_key.is_str()) {
if (idx == npos) {
return false;
}

get_key_list(idx) = new_key.clone();

return true;
}
return false;
}

Value& Structured::get_value() {
return data_null;
}
Expand Down Expand Up @@ -4444,6 +4457,11 @@ namespace claujson {

temp_parent[i] = pos[i]->get_parent();

// chk....-2024.04.21 with json explorer?
if (pos[i] == j.as_structured_ptr()) {
temp_parent[i] = nullptr;
}

if (i == 0) {
thr_result[0] = pool->enqueue(save_, std::ref(stream[0]), std::cref(j), temp_parent[0], pretty, (false));
}
Expand Down Expand Up @@ -6188,7 +6206,7 @@ namespace claujson {
}

if (i < sz_x) {
for (uint64_t _i = sz_x; _i > i; --i) {
for (uint64_t _i = sz_x; _i > i; --_i) {
Object* obj = new (std::nothrow) Object();

if (obj == nullptr) {
Expand Down Expand Up @@ -6318,7 +6336,7 @@ namespace claujson {
return _diff(x, y, "");
}

Value patch(const Value& x, const Value& diff) {
Value& patch(Value& x, const Value& diff) {
Value unvalid_data(nullptr, false);

const Structured* j_diff = diff.as_structured_ptr();
Expand All @@ -6334,7 +6352,7 @@ namespace claujson {
static const Value _last_key_str = Value("last_key"sv);
static const Value _last_idx_str = Value("last_idx"sv);

Value result = x.clone();
Value& result = x;

uint64_t sz_diff = j_diff->get_data_size();

Expand Down Expand Up @@ -6389,7 +6407,7 @@ namespace claujson {

uint64_t last_idx = obj->get_value_list(last_idx_idx).uint_val();

delete parent->get_value_list(last_idx).as_structured_ptr(); // chk!
claujson::clean(parent->get_value_list(last_idx));
parent->erase(last_idx);
}
else {
Expand All @@ -6401,7 +6419,7 @@ namespace claujson {

const Value& last_key = obj->get_value_list(last_key_idx);
uint64_t _idx = parent->find(last_key);
delete parent->get_value_list(_idx).as_structured_ptr();
claujson::clean(parent->get_value_list(_idx));
parent->erase(_idx);
}
}
Expand Down
4 changes: 2 additions & 2 deletions claujson.h
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,7 @@ namespace claujson {

public:
bool change_key(const Value& key, const Value& new_key);
bool change_key(uint64_t idx, const Value& new_key);

virtual Value& get_value();

Expand Down Expand Up @@ -1153,8 +1154,7 @@ namespace claujson {
[[nodiscard]]
Value diff(const Value& x, const Value& y);

[[nodiscard]]
Value patch(const Value& x, const Value& diff);
Value& patch(Value& x, const Value& diff);


void init(int thr_num); // call first, before use claujson..
Expand Down
6 changes: 4 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ void json_pointer_test() {
claujson::Value diff = claujson::diff(x, y);
std::cout << diff << "\n";

claujson::Value result = claujson::patch(x, diff);
claujson::Value result = x.clone();

claujson::patch(result, diff);

std::cout << result << "\n";

Expand Down Expand Up @@ -392,7 +394,7 @@ int main(int argc, char* argv[])
}

// not-thread-safe..
auto x = claujson::parse(argv[1], j, thr_num); // argv[1], j, 64 ??
auto x = claujson::parse(argv[1], j, 0); // argv[1], j, 64 ??

if (!x.first) {
std::cout << "fail\n";
Expand Down

0 comments on commit 786cf2a

Please sign in to comment.