[ https://issues.apache.org/jira/browse/MINIFICPP-114?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16256245#comment-16256245 ]
ASF GitHub Bot commented on MINIFICPP-114: ------------------------------------------ Github user calebj commented on a diff in the pull request: https://github.com/apache/nifi-minifi-cpp/pull/191#discussion_r151580153 --- 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(); + + rapidjson::Value opReqStrVal; + std::string operation_request_str = getOperation(payload); + opReqStrVal.SetString(operation_request_str.c_str(), operation_request_str.length(), alloc); + json_payload.AddMember("operation", opReqStrVal, alloc); + + std::string operationid = payload.getIdentifier(); + if (operationid.length() > 0) { + rapidjson::Value operationIdVal = getStringValue(operationid, alloc); + json_payload.AddMember("operationid", operationIdVal, alloc); + } + + mergePayloadContent(json_payload, payload, alloc); + + for (const auto &nested_payload : payload.getNestedPayloads()) { + rapidjson::Value np_key = getStringValue(nested_payload.getLabel(), alloc); + rapidjson::Value np_value = serializeJsonPayload(nested_payload, alloc); + json_payload.AddMember(np_key, np_value, alloc); + } + + rapidjson::StringBuffer buffer; + rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer); --- End diff -- It used to use the formatted "pretty" writer before the change; and yes, it does. Some of the tests might have to change to work with unformatted JSON. > Consolidate JSON API use to RapidJSON > ------------------------------------- > > Key: MINIFICPP-114 > URL: https://issues.apache.org/jira/browse/MINIFICPP-114 > Project: NiFi MiNiFi C++ > Issue Type: Improvement > Reporter: John Reynolds > Assignee: John Reynolds > > MiNiFi was updated with new dependencies to jsoncpp: > SiteToSiteProvenanceReportingTask > Performance observations indicate significant improvements with RapidJSON vs. > jsoncpp. Convert all use of jsoncpp to RapidJSON where applicable. -- This message was sent by Atlassian JIRA (v6.4.14#64029)