static Generic fromGenericAttribute(const GenericAttribute * attr) { { auto boolAttr = dynamic_cast<const BoolAttribute *>(attr); if(boolAttr != nullptr) { return Generic(boolAttr->get()); } } { auto numberAttr = dynamic_cast<const _NumberAttribute<float> *>(attr); if(numberAttr != nullptr) { return Generic(numberAttr->get()); } } { auto stringAttr = dynamic_cast<const _StringAttribute<std::string> *>(attr); if(stringAttr != nullptr) { return Generic(stringAttr->get()); } } { auto listAttr = dynamic_cast<const GenericAttributeList *>(attr); if(listAttr != nullptr) { GenericArray genericArray; genericArray.reserve(listAttr->size()); for(const auto & element : *listAttr) { genericArray.emplace_back(fromGenericAttribute(element.get())); } return Generic(std::move(genericArray)); } } { auto mapAttr = dynamic_cast<const GenericAttributeMap *>(attr); if(mapAttr != nullptr) { GenericMap genericMap; genericMap.reserve(mapAttr->size()); for(const auto & element : *mapAttr) { genericMap.emplace(element.first, fromGenericAttribute(element.second.get())); } return Generic(std::move(genericMap)); } } return Generic(); }
void GenericConversionTest::testArraySerialization() { GenericArray array; array.emplace_back(true); array.emplace_back(std::string("[1, 2, \"xyz\"]")); array.emplace_back(12345.6f); array.emplace_back(false); GenericArray innerArray; innerArray.emplace_back(std::string("one")); innerArray.emplace_back(std::string("two")); innerArray.emplace_back(std::string("three")); array.emplace_back(innerArray); const Util::Generic genericArray(array); std::stringstream tempStream; Util::GenericConversion::toJSON(genericArray, tempStream); const std::string serialization = tempStream.str(); const Util::Generic importedGenericArray = Util::GenericConversion::fromJSON(tempStream); CPPUNIT_ASSERT(checkGenericsEqual(genericArray, importedGenericArray)); }
static bool checkGenericArraysEqual(const GenericArray & expected, const GenericArray & actual) { CPPUNIT_ASSERT_EQUAL(expected.size(), actual.size()); return std::equal(expected.cbegin(), expected.cend(), actual.cbegin(), &checkGenericsEqual); }