I have a json file, which contains two object. The first object is an array of objects. Each of these objects has an element "key" and empty array. I need to fill the array with 4 numbers and I need to save back the json. I am checking the tutorial, but probably I am missing something. May somebody help me please? Here is my code, that doesn't work:
void BimObjectsToProjection::modifyViewBoxForProjection(std::string projectionName, long long minX, long long minY, long long Xlen, long long Ylen)
{
Value& projections = md_FilesJsonDocument["ProjectionImages"];
for (Value::ValueIterator projectionsIterator = projections.Begin(); projectionsIterator != projections.End(); ++projectionsIterator)
{
rapidjson::Value& projectionJson = *projectionsIterator;
string name = projectionJson["Name"].GetString();
if (projectionName == name)
{
Document::AllocatorType& allocator = md_FilesJsonDocument.GetAllocator();
rapidjson::Value& viewBox = (*projectionsIterator)["BB"];
viewBox.PushBack((int)minX, allocator);
viewBox.PushBack((int)minY, allocator);
viewBox.PushBack((int)Xlen, allocator);
viewBox.PushBack((int)Ylen, allocator);
rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
md_FilesJsonDocument.Accept(writer);
break;
}
}
}
The json looks like this:
{
"ProjectionImages": [
{
"Id": "33c75d31-7ccd-4daf-814a-56250cdee42f",
"Name": "Projection_Image_Architectural_First Floor_Zone 1.png",
"Discipline": "Architectural",
"LevelName": "First Floor",
"BB": [],
"SheetFileName": null,
"ProjectionLineFileId": "36bb6683-c6d3-43c2-bbdc-aedf3203ea86",
"ProjectionLineFileName": "Projection_Image_Architectural_First Floor_Zone 1.pdf",
"ZoneName": "Zone 1"
},
...
Even if I try to add new array member, this code doesn't work. Is it because of my writing approch?
void BimObjectsToProjection::modifyViewBoxForProjection(std::string projectionName, long long minX, long long minY, long long Xlen, long long Ylen)
{
Value& projections = md_FilesJsonDocument["ProjectionImages"];
for (Value::ValueIterator projectionsIterator = projections.Begin(); projectionsIterator != projections.End(); ++projectionsIterator)
{
rapidjson::Value& projectionJson = *projectionsIterator;
string name = projectionJson["Name"].GetString();
if (projectionName == name)
{
Document::AllocatorType& allocator = md_FilesJsonDocument.GetAllocator();
Value a(kArrayType);
a.PushBack((int)minX, allocator);
a.PushBack((int)minY, allocator);
a.PushBack((int)Xlen, allocator);
a.PushBack((int)Ylen, allocator);
(*projectionsIterator).AddMember("AA", a, allocator);
rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
md_FilesJsonDocument.Accept(writer);
break;
}
}
}
This part of the code doesn't do anything:
rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
md_FilesJsonDocument.Accept(writer);
strbuf
contains the json string but it is discarded. I would move this into a separate function and print the conents with std::cout << strbuf;
.
To write directly to a file:
std::ofstream ofs("out.json", std::ios::out);
if (ofs.is_open()) {
rapidjson::OStreamWrapper osw(ofs);
rapidjson::Writer<rapidjson::OStreamWrapper> writer(osw);
md_FilesJsonDocument.Accept(writer);
}