3 #include <SimoxUtility/json/eigen_conversion.h>
16 jsonStack.push({
"", nlohmann::json()});
22 nlohmann::json& obj = jsonStack.top().second;
23 if (obj.front().is_object())
64 jsonStack.push({key,
nullptr});
69 jsonStack.push({key, nlohmann::json(nlohmann::json::value_t::object)});
75 jsonStack.push({key, nlohmann::json(nlohmann::json::value_t::object)});
83 auto obj = jsonStack.top();
85 insertIntoJSON(obj.first, obj.second);
127 std::string key =
"";
147 jsonStack.push({key, nlohmann::json(nlohmann::json::value_t::array)});
151 jsonStack.push({key,
nullptr});
158 auto list = jsonStack.top();
160 insertIntoJSON(
list.first,
list.second);
172 const std::string key = nd.getPath().getLastElement();
175 if (nd.getType() ==
"float")
177 Eigen::Map<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic>> m(
178 reinterpret_cast<float*
>(nd.getData()), t.getRows(), t.getCols());
181 else if (nd.getType() ==
"double")
183 Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>> m(
184 reinterpret_cast<double*
>(nd.getData()), t.getRows(), t.getCols());
189 obj = handleGenericNDArray(nd);
192 insertIntoJSON(key, obj);
196 std::string key = elementType->getPath().getLastElement();
197 insertNULLIntoJSON(key);
211 const std::string key = elementData->getPath().getLastElement();
213 insertIntoJSON(key, handleGenericNDArray(nd));
217 std::string key = elementType->getPath().getLastElement();
218 insertNULLIntoJSON(key);
232 const std::string key = elementData->getPath().getLastElement();
236 insertIntoJSON(key, obj);
238 else if (elementType)
240 std::string key = elementType->getPath().getLastElement();
241 insertNULLIntoJSON(key);
256 const std::string key = elementData->getPath().getLastElement();
258 insertIntoJSON(key, handleGenericNDArray(nd));
260 else if (elementType)
262 std::string key = elementType->getPath().getLastElement();
263 insertNULLIntoJSON(key);
277 const std::string key = elementData->getPath().getLastElement();
279 insertIntoJSON(key, handleGenericNDArray(nd));
281 else if (elementType)
283 std::string key = elementType->getPath().getLastElement();
284 insertNULLIntoJSON(key);
296 this->
visitInt(elementData, elementType);
304 const std::string key = elementData->getPath().getLastElement();
306 insertIntoJSON(key, i);
308 else if (elementType)
310 std::string key = elementType->getPath().getLastElement();
311 insertNULLIntoJSON(key);
325 const std::string key = elementData->getPath().getLastElement();
327 insertIntoJSON(key, l);
329 else if (elementType)
331 std::string key = elementType->getPath().getLastElement();
332 insertNULLIntoJSON(key);
346 const std::string key = elementData->getPath().getLastElement();
348 insertIntoJSON(key, f);
350 else if (elementType)
352 std::string key = elementType->getPath().getLastElement();
353 insertNULLIntoJSON(key);
367 const std::string key = elementData->getPath().getLastElement();
369 insertIntoJSON(key, d);
371 else if (elementType)
373 std::string key = elementType->getPath().getLastElement();
374 insertNULLIntoJSON(key);
388 const std::string key = elementData->getPath().getLastElement();
390 insertIntoJSON(key, b);
392 else if (elementType)
394 std::string key = elementType->getPath().getLastElement();
395 insertNULLIntoJSON(key);
409 const std::string key = elementData->getPath().getLastElement();
411 insertIntoJSON(key,
s);
413 else if (elementType)
415 std::string key = elementType->getPath().getLastElement();
416 insertNULLIntoJSON(key);
438 template <
typename ElementType>
440 TreeTypedJSONConverter::insertIntoJSON(
const std::string& key,
const ElementType&
data)
442 if (jsonStack.top().second.is_object())
444 jsonStack.top().second[key] = nlohmann::json(
data);
448 jsonStack.top().second.emplace_back(
data);
453 TreeTypedJSONConverter::insertNULLIntoJSON(
const std::string& key)
455 if (jsonStack.top().second.is_object())
457 jsonStack.top().second[key] =
nullptr;
461 jsonStack.top().second.emplace_back(
nullptr);
466 TreeTypedJSONConverter::handleGenericNDArray(
const aron::data::NDArray& nd)
468 nlohmann::json ndobj;
469 std::vector<int> shape = nd.getShape();
470 ndobj[
"dimensions"] = shape;
471 ndobj[
"type"] = nd.getType();
476 : std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<>());
477 std::vector<unsigned char> d = std::vector<unsigned char>(elements);
478 memcpy(d.data(), nd.getData(), elements);