i doing some stuff with rapidjson and i want to add values to an array i just created like that
#include <iostream>
#include "rapidjson/document.h"
using namespace std ;
int main() {
char json[1024];
rapidjson::Document document ;
document.Parse<0>(json);
if (!document.IsObject()) {
document.SetObject();
}
assert(document.IsObject());
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
// adding member (int)
document.AddMember("mohammed",25,allocator);
assert(document.HasMember("mohammed"));
cout << document["mohammed"].GetInt() << endl ;
// adding member (array)
rapidjson::Value array(rapidjson::kArrayType);
array.PushBack(5,allocator);
array.PushBack(6,allocator);
cout << array[0u].GetInt() << endl ;
cout << array[1].GetInt() << endl ;
document.AddMember("array",array,allocator);
assert(document.HasMember("array"));
assert(document["array"].IsArray());
// here the following line give me an error
array.PushBack(7,allocator);
}
the error is
json: rapidjson/document.h:397: rapidjson::GenericValue<Encoding, Allocator>& rapidjson::GenericValue<Encoding, Allocator>::PushBack(rapidjson::GenericValue<Encoding, Allocator>&, Allocator&) [with Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>]: Assertion `IsArray()' failed.
Aborted (core dumped)
what is the problem can someone explain? what is happening i am kinda new to this,thank you.
When array.PushBack(...)
is being executed, array
has already been moved into document
, and becomes a null value type (array.IsNull() == true
). So you cannot PushBack
to a null value.
document["array"].PushBack(7,allocator)
will work.