Skip to content

Commit

Permalink
Fixed issue #263
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Aug 12, 2020
1 parent 5d8a71f commit 0dd2ff0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
31 changes: 31 additions & 0 deletions include/jsoncons/basic_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5582,6 +5582,37 @@ class basic_json
o = visitor.get_result();
return is;
}

friend basic_json deep_copy(const basic_json& other)
{
switch (other.storage())
{
case storage_kind::array_value:
{
basic_json j(json_array_arg, other.tag(), other.get_allocator());

for (const auto& item : other.array_range())
{
j.push_back(deep_copy(item));
}
return j;
}
case storage_kind::object_value:
{
basic_json j(json_object_arg, other.tag(), other.get_allocator());

for (const auto& item : other.object_range())
{
j.try_emplace(item.key(), deep_copy(item.value()));
}
return j;
}
case storage_kind::json_const_pointer:
return deep_copy(*(other.cast<json_const_pointer_storage>().value()));
default:
return other;
}
}
};

template <class Json>
Expand Down
27 changes: 27 additions & 0 deletions tests/src/json_const_pointer_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,32 @@ TEST_CASE("json_const_pointer identifier tests")
target = j3;
CHECK(target == expected);
}

SECTION("test2")
{
json expected = json::parse("[1,2,4,5,6,8]");
json target;
{
json j1(json_array_arg);
json j2(json_array_arg);
json j3(json_array_arg);
const json v1(json_const_pointer_arg, &source.at("reservations"));
flatten(v1, "instances", j1);

const json v2(json_const_pointer_arg, &j1);
flatten(v2, "foo", j2);

const json v3(json_const_pointer_arg, &j2);
flatten(v3, "bar", j3);

target = deep_copy(j3);
}
CHECK(target == expected);
CHECK(target.storage() == storage_kind::array_value);
for (const auto& item : target.array_range())
{
CHECK(item.storage() == storage_kind::uint64_value);
}
}
}

0 comments on commit 0dd2ff0

Please sign in to comment.