/*========================================================================= Program: Visualization Toolkit Module: OMFHelpers.cxx Language: C++ Copyright (c) 1993-2002 Ken Martin, Will Schroeder, Bill Lorensen All rights reserved. See Copyright.txt or http://www.kitware.com/Copyright.htm for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ #include "OMFHelpers.h" #include "vtkLogger.h" #include "vtk_jsoncpp.h" namespace omf { namespace helper { //------------------------------------------------------------------------------ void PrintMemberNames(const Json::Value& root) { vtkLogStartScope(INFO, "print-member-names"); if (!root.isObject()) { vtkLogEndScope("print-member-names"); return; } auto members = root.getMemberNames(); for (const auto& member : members) { vtkLog(INFO, << member); } vtkLogEndScope("print-member-names"); } //------------------------------------------------------------------------------ bool GetPointFromJSON(const Json::Value& pointJSON, double point[3]) { if (pointJSON.isNull() || !pointJSON.isArray()) { return false; } for (Json::Value::ArrayIndex i = 0; i < pointJSON.size(); ++i) { point[i] = pointJSON[i].asDouble(); } return true; } //------------------------------------------------------------------------------ bool GetIntValue(const Json::Value& root, int& value) { if (root.empty() || !root.isInt()) { return false; } value = root.asInt(); return true; } //------------------------------------------------------------------------------ bool GetUIntValue(const Json::Value& root, unsigned int& value) { if (root.empty() || !root.isUInt()) { return false; } value = root.asUInt(); return true; } //------------------------------------------------------------------------------ bool GetDoubleValue(const Json::Value& root, double& value) { if (root.empty() || !root.isDouble()) { return false; } value = root.asDouble(); return true; } //------------------------------------------------------------------------------ bool GetStringValue(const Json::Value& root, std::string& value) { if (root.empty() || !root.isString()) { return false; } value = root.asString(); return true; } //------------------------------------------------------------------------------ bool GetBoolValue(const Json::Value& root, bool& value) { if (root.empty() || !root.isBool()) { return false; } value = root.asBool(); return true; } //------------------------------------------------------------------------------ bool GetIntArray(const Json::Value& root, std::vector& value) { if (root.empty() || !root.isArray()) { return false; } value.reserve(root.size()); for (const auto& intValue : root) { if (intValue.empty() && !intValue.isInt()) { value.clear(); return false; } value.push_back(intValue.asInt()); } if (value.empty()) { value.clear(); return false; } return true; } //------------------------------------------------------------------------------ bool GetUIntArray(const Json::Value& root, std::vector& value) { if (root.empty() || !root.isArray()) { return false; } value.reserve(root.size()); for (const auto& uIntValue : root) { if (uIntValue.empty() && !uIntValue.isUInt()) { value.clear(); return false; } value.push_back(uIntValue.asUInt()); } if (value.empty()) { value.clear(); return false; } return true; } //------------------------------------------------------------------------------ bool GetFloatArray(const Json::Value& root, std::vector& value) { if (root.empty() || !root.isArray()) { return false; } value.reserve(root.size()); for (const auto& floatValue : root) { if (floatValue.empty() && !floatValue.isDouble()) { value.clear(); return false; } value.push_back(floatValue.asDouble()); } if (value.empty()) { value.clear(); return false; } return true; } //------------------------------------------------------------------------------ bool GetDoubleArray(const Json::Value& root, std::vector& value) { if (root.empty() || !root.isArray()) { return false; } value.reserve(root.size()); for (const auto& doubleValue : root) { if (doubleValue.empty() && !doubleValue.isDouble()) { value.clear(); return false; } value.push_back(doubleValue.asDouble()); } if (value.empty()) { value.clear(); return false; } return true; } } // end namespace helper } // end namespace omf