fgerlits commented on code in PR #1980: URL: https://github.com/apache/nifi-minifi-cpp/pull/1980#discussion_r2274015576
########## extensions/standard-processors/processors/SplitJson.cpp: ########## @@ -0,0 +1,133 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "SplitJson.h" + +#include <unordered_map> + +#include "core/ProcessSession.h" +#include "core/ProcessContext.h" +#include "core/Resource.h" +#include "utils/ProcessorConfigUtils.h" +#include "utils/Id.h" + +#include "jsoncons_ext/jsonpath/jsonpath.hpp" + +namespace org::apache::nifi::minifi::processors { + +void SplitJson::initialize() { + setSupportedProperties(Properties); + setSupportedRelationships(Relationships); +} + +void SplitJson::onSchedule(core::ProcessContext& context, core::ProcessSessionFactory&) { + json_path_expression_ = utils::parseProperty(context, SplitJson::JsonPathExpression); + null_value_representation_ = utils::parseEnumProperty<split_json::NullValueRepresentationOption>(context, SplitJson::NullValueRepresentation); +} + +std::optional<jsoncons::json> SplitJson::queryArrayUsingJsonPath(core::ProcessSession& session, const std::shared_ptr<core::FlowFile>& flow_file) const { + const auto flow_file_read_result = session.readBuffer(flow_file); + const auto json_string = std::string(reinterpret_cast<const char*>(flow_file_read_result.buffer.data()), flow_file_read_result.buffer.size()); Review Comment: this could use the convenience function ```suggestion const auto json_string = to_string(flow_file_read_result); ``` (it's in `core/ProcessSession.h`, so already included) ########## extensions/standard-processors/processors/SplitJson.cpp: ########## @@ -0,0 +1,133 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "SplitJson.h" + +#include <unordered_map> + +#include "core/ProcessSession.h" +#include "core/ProcessContext.h" +#include "core/Resource.h" +#include "utils/ProcessorConfigUtils.h" +#include "utils/Id.h" + +#include "jsoncons_ext/jsonpath/jsonpath.hpp" + +namespace org::apache::nifi::minifi::processors { + +void SplitJson::initialize() { + setSupportedProperties(Properties); + setSupportedRelationships(Relationships); +} + +void SplitJson::onSchedule(core::ProcessContext& context, core::ProcessSessionFactory&) { + json_path_expression_ = utils::parseProperty(context, SplitJson::JsonPathExpression); + null_value_representation_ = utils::parseEnumProperty<split_json::NullValueRepresentationOption>(context, SplitJson::NullValueRepresentation); +} + +std::optional<jsoncons::json> SplitJson::queryArrayUsingJsonPath(core::ProcessSession& session, const std::shared_ptr<core::FlowFile>& flow_file) const { + const auto flow_file_read_result = session.readBuffer(flow_file); + const auto json_string = std::string(reinterpret_cast<const char*>(flow_file_read_result.buffer.data()), flow_file_read_result.buffer.size()); + if (json_string.empty()) { + logger_->log_error("FlowFile content is empty, transferring to Failure relationship"); + return std::nullopt; + } + + jsoncons::json json_object; + try { + json_object = jsoncons::json::parse(json_string); + } catch (const jsoncons::json_exception& e) { + logger_->log_error("FlowFile content is not a valid JSON document, transferring to Failure relationship: {}", e.what()); + return std::nullopt; + } + + jsoncons::json query_result; + try { + query_result = jsoncons::jsonpath::json_query(json_object, json_path_expression_); + } catch (const jsoncons::jsonpath::jsonpath_error& e) { + logger_->log_error("Invalid JSON path expression '{}' set in the 'JsonPath Expression' property: {}", json_path_expression_, e.what()); + return std::nullopt; + } + + return query_result; +} + +std::string SplitJson::jsonValueToString(const jsoncons::json& json_value) const { + if (json_value.is_null()) { + return null_value_representation_ == split_json::NullValueRepresentationOption::EmptyString ? "" : "null"; + } + + if (json_value.is_string()) { + return json_value.as<std::string>(); + } + + return json_value.to_string(); +} + +void SplitJson::onTrigger(core::ProcessContext&, core::ProcessSession& session) { + auto flow_file = session.get(); + if (!flow_file) { + return; + } Review Comment: we usually add a `yield` in these cases -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
