#include "JsonParsing.hpp" #include namespace lottie { thread_local int isExceptionExpectedLevel = 0; LottieParsingException::Guard::Guard() { assert(isExceptionExpectedLevel >= 0); isExceptionExpectedLevel++; } LottieParsingException::Guard::~Guard() { assert(isExceptionExpectedLevel - 1 >= 0); isExceptionExpectedLevel--; } LottieParsingException::LottieParsingException() { if (isExceptionExpectedLevel == 0) { assert(true); } } const char* LottieParsingException::what() const throw() { return "Lottie parsing exception"; } lottiejson11::Json getAny(lottiejson11::Json::object const &object, std::string const &key) noexcept(false) { auto value = object.find(key); if (value == object.end()) { throw LottieParsingException(); } return value->second; } std::optional getOptionalAny(lottiejson11::Json::object const &object, std::string const &key) noexcept(false) { auto value = object.find(key); if (value == object.end()) { return std::nullopt; } return value->second; } lottiejson11::Json::object getObject(lottiejson11::Json::object const &object, std::string const &key) noexcept(false) { auto value = object.find(key); if (value == object.end()) { throw LottieParsingException(); } if (!value->second.is_object()) { throw LottieParsingException(); } return value->second.object_items(); } std::optional getOptionalObject(lottiejson11::Json::object const &object, std::string const &key) noexcept(false) { auto value = object.find(key); if (value == object.end()) { return std::nullopt; } if (!value->second.is_object()) { throw LottieParsingException(); } return value->second.object_items(); } std::vector getObjectArray(lottiejson11::Json::object const &object, std::string const &key) noexcept(false) { auto value = object.find(key); if (value == object.end()) { throw LottieParsingException(); } if (!value->second.is_array()) { throw LottieParsingException(); } std::vector result; for (const auto &item : value->second.array_items()) { if (!item.is_object()) { throw LottieParsingException(); } result.push_back(item.object_items()); } return result; } std::optional> getOptionalObjectArray(lottiejson11::Json::object const &object, std::string const &key) noexcept(false) { auto value = object.find(key); if (value == object.end()) { return std::nullopt; } if (!value->second.is_array()) { throw LottieParsingException(); } std::vector result; for (const auto &item : value->second.array_items()) { if (!item.is_object()) { throw LottieParsingException(); } result.push_back(item.object_items()); } return result; } std::vector getAnyArray(lottiejson11::Json::object const &object, std::string const &key) noexcept(false) { auto value = object.find(key); if (value == object.end()) { throw LottieParsingException(); } if (!value->second.is_array()) { throw LottieParsingException(); } return value->second.array_items(); } std::optional> getOptionalAnyArray(lottiejson11::Json::object const &object, std::string const &key) noexcept(false) { auto value = object.find(key); if (value == object.end()) { throw std::nullopt; } if (!value->second.is_array()) { throw LottieParsingException(); } return value->second.array_items(); } std::string getString(lottiejson11::Json::object const &object, std::string const &key) noexcept(false) { auto value = object.find(key); if (value == object.end()) { throw LottieParsingException(); } if (!value->second.is_string()) { throw LottieParsingException(); } return value->second.string_value(); } std::optional getOptionalString(lottiejson11::Json::object const &object, std::string const &key) noexcept(false) { auto value = object.find(key); if (value == object.end()) { return std::nullopt; } if (!value->second.is_string()) { throw LottieParsingException(); } return value->second.string_value(); } int32_t getInt(lottiejson11::Json::object const &object, std::string const &key) noexcept(false) { auto value = object.find(key); if (value == object.end()) { throw LottieParsingException(); } if (!value->second.is_number()) { throw LottieParsingException(); } return value->second.int_value(); } std::optional getOptionalInt(lottiejson11::Json::object const &object, std::string const &key) noexcept(false) { auto value = object.find(key); if (value == object.end()) { return std::nullopt; } if (!value->second.is_number()) { throw LottieParsingException(); } return value->second.int_value(); } double getDouble(lottiejson11::Json::object const &object, std::string const &key) noexcept(false) { auto value = object.find(key); if (value == object.end()) { throw LottieParsingException(); } if (!value->second.is_number()) { throw LottieParsingException(); } return value->second.number_value(); } std::optional getOptionalDouble(lottiejson11::Json::object const &object, std::string const &key) noexcept(false) { auto value = object.find(key); if (value == object.end()) { return std::nullopt; } if (!value->second.is_number()) { throw LottieParsingException(); } return value->second.number_value(); } bool getBool(lottiejson11::Json::object const &object, std::string const &key) noexcept(false) { auto value = object.find(key); if (value == object.end()) { throw LottieParsingException(); } if (!value->second.is_bool()) { throw LottieParsingException(); } return value->second.bool_value(); } std::optional getOptionalBool(lottiejson11::Json::object const &object, std::string const &key) noexcept(false) { auto value = object.find(key); if (value == object.end()) { return std::nullopt; } if (!value->second.is_bool()) { throw LottieParsingException(); } return value->second.bool_value(); } }