From e22bc7eff234734c47944bc8032be39981388933 Mon Sep 17 00:00:00 2001 From: Simon Walton Date: Fri, 4 May 2018 14:10:22 -0700 Subject: [PATCH] Add FBXNode to JSON stream class --- libraries/fbx/src/FBXToJSON.cpp | 82 +++++++++++++++++++++++++++++++++ libraries/fbx/src/FBXToJSON.h | 30 ++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 libraries/fbx/src/FBXToJSON.cpp create mode 100644 libraries/fbx/src/FBXToJSON.h diff --git a/libraries/fbx/src/FBXToJSON.cpp b/libraries/fbx/src/FBXToJSON.cpp new file mode 100644 index 0000000000..1620fcca46 --- /dev/null +++ b/libraries/fbx/src/FBXToJSON.cpp @@ -0,0 +1,82 @@ +// +// FBXToJSON.cpp +// libraries/fbx/src +// +// Created by Simon Walton on 5/4/2013. +// Copyright 2018 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "FBXToJSON.h" +#include "FBX.h" + +template +inline FBXToJSON& FBXToJSON::operator<<(QVector& arrayProp) { + *this << "["; + for (auto& prop : arrayProp) { + *(std::ostringstream*)this << prop << ", "; + } + *this << "] "; + if (arrayProp.size() > 4) { + *this << "# " << arrayProp.size() << " items"; + } + *this << '\n'; + + return *this; +} + +FBXToJSON& FBXToJSON::operator<<(const FBXNode& fbxNode) { + using std::string; + + string nodeName(fbxNode.name); + if (nodeName.empty()) nodeName = "nodename"; + + *this << string(_indentLevel * 4, ' ') << '"' << nodeName << "\": {\n"; + ++_indentLevel; + int p = 0; + for (auto& prop : fbxNode.properties) { + *this << string(_indentLevel * 4, ' ') << "\"p" << p++ << "\": "; + switch (prop.userType()) { + case QMetaType::Short: + case QMetaType::Bool: + case QMetaType::Int: + case QMetaType::LongLong: + case QMetaType::Double: + case QMetaType::Float: + *this << prop.toString().toStdString(); + break; + + case QMetaType::QString: + case QMetaType::QByteArray: + *this << '"' << prop.toString().toStdString() << '"'; + break; + + default: + if (prop.canConvert>()) { + *this << prop.value>(); + } else if (prop.canConvert>()) { + *this << prop.value>(); + } else if (prop.canConvert>()) { + *this << prop.value>(); + } else if (prop.canConvert>()) { + *this << prop.value>(); + } else if (prop.canConvert>()) { + *this << prop.value>(); + } else { + *this << ""; + } + break; + } + *this << ",\n"; + } + + for (auto child : fbxNode.children) { + *this << child; + } + + *this << string(_indentLevel * 4, ' ') << "},\n"; + --_indentLevel; + return *this; +} diff --git a/libraries/fbx/src/FBXToJSON.h b/libraries/fbx/src/FBXToJSON.h new file mode 100644 index 0000000000..0961144e9f --- /dev/null +++ b/libraries/fbx/src/FBXToJSON.h @@ -0,0 +1,30 @@ +// +// FBXToJSON.h +// libraries/fbx/src +// +// Created by Simon Walton on 5/4/2013. +// Copyright 2018 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_FBXToJSON_h +#define hifi_FBXToJSON_h + +#include + +// Forward declarations. +class FBXNode; +template class QVector; + +class FBXToJSON : public std::ostringstream { +public: + FBXToJSON& operator<<(const FBXNode& fbxNode); + +private: + template FBXToJSON& operator<<(QVector& arrayProp); + int _indentLevel { 0 }; +}; + +#endif // hifi_FBXToJSON_h