adamdebreceni commented on a change in pull request #976:
URL: https://github.com/apache/nifi-minifi-cpp/pull/976#discussion_r559361918



##########
File path: extensions/windows-event-log/wel/JSONUtils.cpp
##########
@@ -0,0 +1,166 @@
+/**
+ * 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 "JSONUtils.h"
+
+#include <vector>
+#include <string>
+#include <functional>
+
+#include <pugixml.hpp>
+
+#include "rapidjson/document.h"
+#include "rapidjson/writer.h"
+#include "rapidjson/stringbuffer.h"
+#include "rapidjson/prettywriter.h"
+
+#include "gsl/gsl-lite.hpp";
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace wel {
+
+static rapidjson::Value xmlElementToJSON(const pugi::xml_node& node, 
rapidjson::Document& doc) {
+  gsl_Expects(node.type() == pugi::xml_node_type::node_element);
+  rapidjson::Value object(rapidjson::kObjectType);
+  object.AddMember("name", rapidjson::StringRef(node.name()), 
doc.GetAllocator());
+  auto& attributes = object.AddMember("attributes", rapidjson::kObjectType, 
doc.GetAllocator())["attributes"];
+  for (const auto& attr : node.attributes()) {
+    attributes.AddMember(rapidjson::StringRef(attr.name()), 
rapidjson::StringRef(attr.value()), doc.GetAllocator());
+  }
+  auto& children = object.AddMember("children", rapidjson::kArrayType, 
doc.GetAllocator())["children"];
+  for (const auto& child : node.children()) {
+    if (child.type() == pugi::xml_node_type::node_element) {
+      children.PushBack(xmlElementToJSON(child, doc), doc.GetAllocator());
+    }
+  }
+  object.AddMember("text", rapidjson::StringRef(node.text().get()), 
doc.GetAllocator());
+  return object;
+}
+
+static rapidjson::Value xmlDocumentToJSON(const pugi::xml_node& node, 
rapidjson::Document& doc) {
+  gsl_Expects(node.type() == pugi::xml_node_type::node_document);
+  rapidjson::Value children(rapidjson::kArrayType);
+  for (const auto& child : node.children()) {
+    if (child.type() == pugi::xml_node_type::node_element) {
+      children.PushBack(xmlElementToJSON(child, doc), doc.GetAllocator());
+    }
+  }
+  return children;
+}
+
+rapidjson::Document toRawJSON(const pugi::xml_node& root) {
+  rapidjson::Document doc;
+  if (root.type() == pugi::xml_node_type::node_document) {
+    static_cast<rapidjson::Value&>(doc) = xmlDocumentToJSON(root, doc);
+  }
+  return doc;
+}
+
+static rapidjson::Document toJSONImpl(const pugi::xml_node& root, bool 
flatten) {
+  rapidjson::Document doc{rapidjson::kObjectType};
+
+  auto event_xml = root.child("Event");
+
+  {
+    auto system_xml = event_xml.child("System");
+    auto& system = flatten ? doc : doc.AddMember("System", 
rapidjson::kObjectType, doc.GetAllocator())["System"];
+
+    {
+      auto provider_xml = system_xml.child("Provider");
+      auto& provider = flatten ? doc : system.AddMember("Provider", 
rapidjson::kObjectType, doc.GetAllocator())["Provider"];
+      provider.AddMember("Name", 
rapidjson::StringRef(provider_xml.attribute("Name").value()), 
doc.GetAllocator());
+      provider.AddMember("Guid", 
rapidjson::StringRef(provider_xml.attribute("Guid").value()), 
doc.GetAllocator());
+    }
+
+    system.AddMember("EventID", 
rapidjson::StringRef(system_xml.child("EventID").text().get()), 
doc.GetAllocator());
+    system.AddMember("Version", 
rapidjson::StringRef(system_xml.child("Version").text().get()), 
doc.GetAllocator());
+    system.AddMember("Level", 
rapidjson::StringRef(system_xml.child("Level").text().get()), 
doc.GetAllocator());
+    system.AddMember("Task", 
rapidjson::StringRef(system_xml.child("Task").text().get()), 
doc.GetAllocator());
+    system.AddMember("Opcode", 
rapidjson::StringRef(system_xml.child("Opcode").text().get()), 
doc.GetAllocator());
+    system.AddMember("Keywords", 
rapidjson::StringRef(system_xml.child("Keywords").text().get()), 
doc.GetAllocator());
+
+    {
+      auto timeCreated_xml = system_xml.child("TimeCreated");
+      auto& timeCreated = flatten ? doc : system.AddMember("TimeCreated", 
rapidjson::kObjectType, doc.GetAllocator())["TimeCreated"];
+      timeCreated.AddMember("SystemTime", 
rapidjson::StringRef(timeCreated_xml.attribute("SystemTime").value()), 
doc.GetAllocator());
+    }
+
+    system.AddMember("EventRecordID", 
rapidjson::StringRef(system_xml.child("EventRecordID").text().get()), 
doc.GetAllocator());
+
+    {
+      auto correlation_xml = system_xml.child("Correlation");

Review comment:
       in pugixml both xml_node and xml_attribute are implicitly nullable, and 
calling the various methods on the null versions produces sensible "null" 
output, e.g. `xml_node().text()` is the empty string




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to