Github user calebj commented on a diff in the pull request: https://github.com/apache/nifi-minifi-cpp/pull/191#discussion_r151580773 --- Diff: extensions/http-curl/protocols/RESTProtocol.cpp --- @@ -81,48 +93,117 @@ const C2Payload RESTProtocol::parseJsonResponse(const C2Payload &payload, const return std::move(C2Payload(payload.getOperation(), state::UpdateState::READ_ERROR, true)); } -Json::Value RESTProtocol::serializeJsonPayload(Json::Value &json_root, const C2Payload &payload) { - // get the name from the content - Json::Value json_payload; - std::map<std::string, std::vector<Json::Value>> children; - for (const auto &nested_payload : payload.getNestedPayloads()) { - Json::Value child_payload = serializeJsonPayload(json_payload, nested_payload); - children[nested_payload.getLabel()].push_back(child_payload); - } - for (auto child_vector : children) { - if (child_vector.second.size() > 1) { - Json::Value children_json(Json::arrayValue); - for (auto child : child_vector.second) { - json_payload[child_vector.first] = child; - } - } else { - if (child_vector.second.size() == 1) { - if (child_vector.second.at(0).isMember(child_vector.first)) { - json_payload[child_vector.first] = child_vector.second.at(0)[child_vector.first]; - } else { - json_payload[child_vector.first] = child_vector.second.at(0); - } - } - } - } +void setJsonStr(const std::string& key, const std::string& value, rapidjson::Value& parent, rapidjson::Document::AllocatorType& alloc) { // NOLINT + rapidjson::Value keyVal; + rapidjson::Value valueVal; + const char* c_key = key.c_str(); + const char* c_val = value.c_str(); + keyVal.SetString(c_key, key.length()), alloc; + valueVal.SetString(c_val, value.length(), alloc); + + parent.AddMember(keyVal, valueVal, alloc); +} + +rapidjson::Value getStringValue(const std::string& value, rapidjson::Document::AllocatorType& alloc) { // NOLINT + rapidjson::Value Val; + Val.SetString(value.c_str(), value.length(), alloc); + return Val; +} + +void RESTProtocol::mergePayloadContent(rapidjson::Value &target, const C2Payload &payload, rapidjson::Document::AllocatorType &alloc) { const std::vector<C2ContentResponse> &content = payload.getContent(); + for (const auto &payload_content : content) { - Json::Value payload_content_values; + rapidjson::Value payload_content_values(rapidjson::kObjectType); bool use_sub_option = true; + if (payload_content.op == payload.getOperation()) { for (auto content : payload_content.operation_arguments) { if (payload_content.operation_arguments.size() == 1 && payload_content.name == content.first) { - json_payload[payload_content.name] = content.second; + setJsonStr(payload_content.name, content.second, target, alloc); use_sub_option = false; } else { - payload_content_values[content.first] = content.second; + setJsonStr(content.first, content.second, payload_content_values, alloc); } } } - if (use_sub_option) - json_payload[payload_content.name] = payload_content_values; + if (use_sub_option) { + rapidjson::Value sub_key = getStringValue(payload_content.name, alloc); + target.AddMember(sub_key, payload_content_values, alloc); + } + } +} + +std::string RESTProtocol::serializeJsonRootPayload(const C2Payload& payload) { + rapidjson::Document json_payload(rapidjson::kObjectType); + rapidjson::Document::AllocatorType &alloc = json_payload.GetAllocator(); --- End diff -- I believe it's supposed to be per-document.
---