[Fix](json reader) fix rapidjson array->PushBack may take ownership… (#21988)

With bellow json path
`["$.data","$.data.datatimestamp"]`

After `array_obj->PushBack` the `data` field owner will be taken from array_obj, and lead to null values for json path `$.data.datatimestamp`

Rapidjson doc:
```
//! Append a GenericValue at the end of the array.
  \note The ownership of \c value will be transferred to this array on success.
 */
GenericValue& PushBack(GenericValue& value, Allocator& allocator);
```
This commit is contained in:
lihangyu
2023-07-21 17:02:01 +08:00
committed by GitHub
parent d1c5025bce
commit 40299d280d
4 changed files with 35 additions and 2 deletions

View File

@ -172,11 +172,13 @@ rapidjson::Value* JsonFunctions::get_json_array_from_parsed_json(
rapidjson::Value* root = match_value(parsed_paths, document, mem_allocator, true);
if (root == nullptr || root == document) { // not found
return nullptr;
} else if (!root->IsArray()) {
} else if (!root->IsArray() && wrap_explicitly) {
rapidjson::Value* array_obj = nullptr;
array_obj = static_cast<rapidjson::Value*>(mem_allocator.Malloc(sizeof(rapidjson::Value)));
array_obj->SetArray();
array_obj->PushBack(*root, mem_allocator);
rapidjson::Value copy;
copy.CopyFrom(*root, mem_allocator);
array_obj->PushBack(std::move(copy), mem_allocator);
// set `wrap_explicitly` to true, so that the caller knows that this Array is wrapped actively.
*wrap_explicitly = true;
return array_obj;