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 =
"";
133 else if (elementType)
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);
169 auto t = *aron::type::Matrix::DynamicCastAndCheck(elementType);
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());
179 Eigen::to_json(
obj, m);
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());
185 Eigen::to_json(
obj, m);
189 obj = handleGenericNDArray(nd);
192 insertIntoJSON(key,
obj);
194 else if (elementType)
196 std::string key = elementType->getPath().getLastElement();
197 insertNULLIntoJSON(key);
211 const std::string key = elementData->getPath().getLastElement();
213 insertIntoJSON(key, handleGenericNDArray(nd));
215 else if (elementType)
217 std::string key = elementType->getPath().getLastElement();
218 insertNULLIntoJSON(key);
232 const std::string key = elementData->getPath().getLastElement();
237 insertIntoJSON(key,
obj);
239 else if (elementType)
241 std::string key = elementType->getPath().getLastElement();
242 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);
void visitBool(DataInput &elementData, TypeInput &elementType) override
void visitNDArray(DataInput &elementData, TypeInput &elementType) override
void visitIntEnum(DataInput &elementData, TypeInput &elementType) override
void visitTupleOnEnter(DataInput &elementData, TypeInput &elementType) override
void visitPairOnExit(DataInput &elementData, TypeInput &elementType) override
void visitString(DataInput &elementData, TypeInput &elementType) override
void visitInt(DataInput &elementData, TypeInput &elementType) override
void visitDictOnExit(DataInput &elementData, TypeInput &elementType) override
void visitListOnEnter(DataInput &elementData, TypeInput &elementType) override
void visitObjectOnExit(DataInput &elementData, TypeInput &elementType) override
void visitPairOnEnter(DataInput &elementData, TypeInput &elementType) override
void visitQuaternion(DataInput &elementData, TypeInput &elementType) override
void visitImage(DataInput &elementData, TypeInput &elementType) override
void visitMatrix(DataInput &elementData, TypeInput &elementType) override
void visitTupleOnExit(DataInput &elementData, TypeInput &elementType) override
void visitDictOnEnter(DataInput &elementData, TypeInput &elementType) override
const nlohmann::json & getJSON()
void visitObjectOnEnter(DataInput &elementData, TypeInput &elementType) override
void visitListOnExit(DataInput &elementData, TypeInput &elementType) override
MapElements getObjectElements(DataInput &elementData, TypeInput &elementType) override
void visitFloat(DataInput &elementData, TypeInput &elementType) override
void visitDouble(DataInput &elementData, TypeInput &elementType) override
void visitPointCloud(DataInput &elementData, TypeInput &elementType) override
void visitLong(DataInput &elementData, TypeInput &elementType) override
std::vector< std::string > getPath() const
std::string getLastElement() const
static Eigen::Quaternion< float > ConvertToQuaternionf(const data::NDArrayPtr &)
static PointerType DynamicCastAndCheck(const VariantPtr &n)
static PointerType DynamicCast(const VariantPtr &n)
A base class for aron exceptions.
ElementTypes::ElementType ElementType
static MapElements GetObjectElementsWithNullType(DataInput &o, TypeInput &t)
typename TypedVisitorBase< const data::VariantPtr, const type::VariantPtr >::TypeInput TypeInput
typename TypedVisitorBase< const data::VariantPtr, const type::VariantPtr >::DataInput DataInput
std::map< std::string, std::pair< DataInputNonConst, TypeInputNonConst > > MapElements