|
| 1 | +// |
| 2 | +// Copyright 2024 Autodesk |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +#ifndef MAYAUSD_UTILS_JSON_DICT_H |
| 18 | +#define MAYAUSD_UTILS_JSON_DICT_H |
| 19 | + |
| 20 | +#include <mayaUsd/base/api.h> |
| 21 | + |
| 22 | +#include <pxr/base/js/json.h> |
| 23 | +#include <pxr/base/vt/dictionary.h> |
| 24 | +#include <pxr/base/vt/value.h> |
| 25 | + |
| 26 | +#include <string> |
| 27 | +#include <vector> |
| 28 | + |
| 29 | +namespace MAYAUSD_NS_DEF { |
| 30 | + |
| 31 | +//////////////////////////////////////////////////////////////////////////// |
| 32 | +// |
| 33 | +// Convert a VtDictionary to JsValue. |
| 34 | +// Modeled after PXR_NS::JsValueTypeConverter |
| 35 | + |
| 36 | +class VtDictionaryToJsValueConverter |
| 37 | +{ |
| 38 | +public: |
| 39 | + static PXR_NS::JsObject convertToDictionary(const PXR_NS::VtDictionary& dict) |
| 40 | + { |
| 41 | + PXR_NS::JsObject object; |
| 42 | + |
| 43 | + for (const auto& keyAndValue : dict) { |
| 44 | + PXR_NS::JsValue sub; |
| 45 | + if (canConvertToValue(keyAndValue.second, sub)) |
| 46 | + object[keyAndValue.first] = sub; |
| 47 | + } |
| 48 | + |
| 49 | + return object; |
| 50 | + } |
| 51 | + |
| 52 | + static bool canConvertToDictionary(const PXR_NS::VtValue& value, PXR_NS::JsValue& into) |
| 53 | + { |
| 54 | + if (!value.IsHolding<PXR_NS::VtDictionary>()) |
| 55 | + return false; |
| 56 | + |
| 57 | + into = convertToDictionary(value.UncheckedGet<PXR_NS::VtDictionary>()); |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + static PXR_NS::JsArray convertToArrayOfValues(const std::vector<PXR_NS::VtValue>& vec) |
| 62 | + { |
| 63 | + PXR_NS::JsArray array; |
| 64 | + |
| 65 | + for (const PXR_NS::VtValue& element : vec) { |
| 66 | + PXR_NS::JsValue sub; |
| 67 | + if (canConvertToValue(element, sub)) |
| 68 | + array.push_back(sub); |
| 69 | + } |
| 70 | + |
| 71 | + return array; |
| 72 | + } |
| 73 | + |
| 74 | + static bool canConvertToArrayOfValues(const PXR_NS::VtValue& value, PXR_NS::JsValue& into) |
| 75 | + { |
| 76 | + if (!value.IsHolding<std::vector<PXR_NS::VtValue>>()) |
| 77 | + return false; |
| 78 | + |
| 79 | + into = convertToArrayOfValues(value.UncheckedGet<std::vector<PXR_NS::VtValue>>()); |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + template <typename T, typename TARGET = T> |
| 84 | + static PXR_NS::JsArray convertToArrayOf(const std::vector<T>& vec) |
| 85 | + { |
| 86 | + PXR_NS::JsArray array; |
| 87 | + |
| 88 | + for (auto&& element : vec) |
| 89 | + array.push_back(PXR_NS::JsValue(TARGET(element))); |
| 90 | + |
| 91 | + return array; |
| 92 | + } |
| 93 | + |
| 94 | + template <typename T, typename TARGET = T> |
| 95 | + static bool canConvertToArrayOf(const PXR_NS::VtValue& value, PXR_NS::JsValue& into) |
| 96 | + { |
| 97 | + if (!value.IsHolding<std::vector<T>>()) |
| 98 | + return false; |
| 99 | + |
| 100 | + into = convertToArrayOf<T, TARGET>(value.UncheckedGet<std::vector<T>>()); |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + static bool canConvertToArray(const PXR_NS::VtValue& value, PXR_NS::JsValue& into) |
| 105 | + { |
| 106 | + // Note: we don't support array of boolean becasue they are rare and in |
| 107 | + // C++, vector<bool> is specialized and cause problems when iterating |
| 108 | + // because the MacOS compiler complain about lost optimization and |
| 109 | + // cause the compilation to fail to to warning-as-error compilation |
| 110 | + // flag. |
| 111 | + // if (canConvertToArrayOf<bool>(value, into)) |
| 112 | + // return true; |
| 113 | + if (canConvertToArrayOf<int>(value, into)) |
| 114 | + return true; |
| 115 | + if (canConvertToArrayOf<unsigned int, int>(value, into)) |
| 116 | + return true; |
| 117 | + if (canConvertToArrayOf<short, int>(value, into)) |
| 118 | + return true; |
| 119 | + if (canConvertToArrayOf<unsigned short, int>(value, into)) |
| 120 | + return true; |
| 121 | + if (canConvertToArrayOf<long, int64_t>(value, into)) |
| 122 | + return true; |
| 123 | + if (canConvertToArrayOf<unsigned long, int64_t>(value, into)) |
| 124 | + return true; |
| 125 | + if (canConvertToArrayOf<int8_t, int>(value, into)) |
| 126 | + return true; |
| 127 | + if (canConvertToArrayOf<uint8_t, int>(value, into)) |
| 128 | + return true; |
| 129 | + if (canConvertToArrayOf<int16_t, int>(value, into)) |
| 130 | + return true; |
| 131 | + if (canConvertToArrayOf<uint16_t, int>(value, into)) |
| 132 | + return true; |
| 133 | + if (canConvertToArrayOf<int32_t, int>(value, into)) |
| 134 | + return true; |
| 135 | + if (canConvertToArrayOf<uint32_t, int>(value, into)) |
| 136 | + return true; |
| 137 | + if (canConvertToArrayOf<int64_t>(value, into)) |
| 138 | + return true; |
| 139 | + if (canConvertToArrayOf<uint64_t, int64_t>(value, into)) |
| 140 | + return true; |
| 141 | + if (canConvertToArrayOf<float>(value, into)) |
| 142 | + return true; |
| 143 | + if (canConvertToArrayOf<double>(value, into)) |
| 144 | + return true; |
| 145 | + if (canConvertToArrayOf<std::string>(value, into)) |
| 146 | + return true; |
| 147 | + if (canConvertToArrayOfValues(value, into)) |
| 148 | + return true; |
| 149 | + |
| 150 | + // TODO: we don't support arrays or arrays or arrays of dictionaries. |
| 151 | + // We never need that for our purpose. |
| 152 | + |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + template <typename T, typename TARGET = T> |
| 157 | + static bool canConvertTo(const PXR_NS::VtValue& value, PXR_NS::JsValue& into) |
| 158 | + { |
| 159 | + if (!value.IsHolding<T>()) |
| 160 | + return false; |
| 161 | + into = PXR_NS::JsValue(TARGET(value.UncheckedGet<T>())); |
| 162 | + return true; |
| 163 | + } |
| 164 | + |
| 165 | + static bool canConvertToValue(const PXR_NS::VtValue& value, PXR_NS::JsValue& into) |
| 166 | + { |
| 167 | + if (canConvertTo<bool>(value, into)) |
| 168 | + return true; |
| 169 | + if (canConvertTo<int>(value, into)) |
| 170 | + return true; |
| 171 | + if (canConvertTo<unsigned int, int>(value, into)) |
| 172 | + return true; |
| 173 | + if (canConvertTo<short, int>(value, into)) |
| 174 | + return true; |
| 175 | + if (canConvertTo<unsigned short, int>(value, into)) |
| 176 | + return true; |
| 177 | + if (canConvertTo<long, int64_t>(value, into)) |
| 178 | + return true; |
| 179 | + if (canConvertTo<unsigned long, int64_t>(value, into)) |
| 180 | + return true; |
| 181 | + if (canConvertTo<int8_t, int>(value, into)) |
| 182 | + return true; |
| 183 | + if (canConvertTo<uint8_t, int>(value, into)) |
| 184 | + return true; |
| 185 | + if (canConvertTo<int16_t, int>(value, into)) |
| 186 | + return true; |
| 187 | + if (canConvertTo<uint16_t, int>(value, into)) |
| 188 | + return true; |
| 189 | + if (canConvertTo<int32_t, int>(value, into)) |
| 190 | + return true; |
| 191 | + if (canConvertTo<uint32_t, int>(value, into)) |
| 192 | + return true; |
| 193 | + if (canConvertTo<int64_t>(value, into)) |
| 194 | + return true; |
| 195 | + if (canConvertTo<uint64_t, int64_t>(value, into)) |
| 196 | + return true; |
| 197 | + if (canConvertTo<float, double>(value, into)) |
| 198 | + return true; |
| 199 | + if (canConvertTo<double>(value, into)) |
| 200 | + return true; |
| 201 | + if (canConvertTo<std::string>(value, into)) |
| 202 | + return true; |
| 203 | + |
| 204 | + if (canConvertToDictionary(value, into)) |
| 205 | + return true; |
| 206 | + |
| 207 | + if (canConvertToArray(value, into)) |
| 208 | + return true; |
| 209 | + |
| 210 | + return true; |
| 211 | + } |
| 212 | +}; |
| 213 | + |
| 214 | +} // namespace MAYAUSD_NS_DEF |
| 215 | + |
| 216 | +#endif |
0 commit comments