adamdebreceni commented on code in PR #1391:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1391#discussion_r1003006036


##########
libminifi/include/core/json/JsonNode.h:
##########
@@ -0,0 +1,248 @@
+/**
+ *
+ * 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <utility>
+#include <memory>
+
+#include "core/flow/Node.h"
+#include "rapidjson/document.h"
+#include "utils/gsl.h"
+#include "utils/ValueCaster.h"
+
+namespace org::apache::nifi::minifi::core {
+
+class JsonNode : public flow::Node::NodeImpl {
+ public:
+  explicit JsonNode(const rapidjson::Value* node): node_(node) {}
+
+  explicit operator bool() const override {
+    return node_ != nullptr;
+  }
+  bool isSequence() const override {
+    return node_ ? node_->IsArray() : false;
+  }
+  bool isMap() const override {
+    return node_ ? node_->IsObject() : false;
+  }
+  bool isNull() const override {
+    return node_ ? node_->IsNull() : false;
+  }
+
+  nonstd::expected<std::string, std::exception_ptr> getString() const override 
{
+    try {
+      if (!node_) {
+        throw std::runtime_error("Cannot get string of invalid json value");
+      }
+      if (!node_->IsString()) {
+        throw std::runtime_error("Cannot get string of non-string json value");
+      }
+      return std::string{node_->GetString(), node_->GetStringLength()};
+    } catch (...) {
+      return nonstd::make_unexpected(std::current_exception());
+    }
+  }
+
+  nonstd::expected<int, std::exception_ptr> getInt() const override {
+    return getNumber<int>("int");
+  }
+  nonstd::expected<unsigned int, std::exception_ptr> getUInt() const override {
+    return getNumber<unsigned int>("unsigned int");
+  }
+  nonstd::expected<int64_t, std::exception_ptr> getInt64() const override {
+    return getNumber<int64_t>("int64_t");
+  }
+  nonstd::expected<uint64_t, std::exception_ptr> getUInt64() const override {
+    return getNumber<uint64_t>("uint64_t");
+  }
+
+  nonstd::expected<bool, std::exception_ptr> getBool() const override {
+    try {
+      if (!node_) {
+        throw std::runtime_error("Cannot get bool of invalid json value");
+      }
+      if (!node_->IsBool()) {
+        throw std::runtime_error("Cannot get bool of non-bool json value");
+      }
+      return node_->GetBool();
+    } catch (...) {
+      return nonstd::make_unexpected(std::current_exception());
+    }
+  }
+
+  std::string getDebugString() const override {
+    if (!node_) return "<invalid>";
+    if (node_->IsObject()) return "<Map>";
+    if (node_->IsArray()) return "<Array>";
+    if (node_->IsNull()) return "null";
+    if (node_->IsInt()) return std::to_string(node_->GetInt());
+    if (node_->IsUint()) return std::to_string(node_->GetUint());
+    if (node_->IsInt64()) return std::to_string(node_->GetInt64());
+    if (node_->IsUint64()) return std::to_string(node_->GetUint64());
+    if (node_->IsTrue()) return "true";
+    if (node_->IsFalse()) return "false";
+    if (node_->IsDouble()) return std::to_string(node_->GetDouble());
+    if (node_->IsString()) return '"' + std::string(node_->GetString(), 
node_->GetStringLength()) + '"';
+    return "<unknown>";
+  }
+
+  size_t size() const override {
+    if (!node_) {
+      throw std::runtime_error("Cannot get size of invalid json value");
+    }
+    if (!node_->IsArray()) {
+      throw std::runtime_error("Cannot get size of non-array json value");
+    }
+    return node_->Size();
+  }
+  flow::Node::Iterator begin() const override;
+  flow::Node::Iterator end() const override;
+
+  flow::Node operator[](std::string_view key) const override {
+    if (!node_) {
+      throw std::runtime_error("Cannot get member of invalid json value");
+    }
+    if (!node_->IsObject()) {
+      return flow::Node{std::make_shared<JsonNode>(nullptr)};

Review Comment:
   the current parsing logic assumes the yaml behavior, I don't think it is 
useful and IMO we should throw as early as possible, but I didn't want to make 
larger changes to the logic, implementing a "stricter" behavior for json while 
maintaining yaml backwards compatibility should come in part2



-- 
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: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to