Ejemplo n.º 1
0
void GenericConversionTest::testMapSerialization() {
	GenericMap map;
	map.emplace(Util::StringIdentifier("firstBool"), Util::Generic(true));
	map.emplace(Util::StringIdentifier("secondBool"), Util::Generic(false));
	map.emplace(Util::StringIdentifier("first string"), Util::Generic(std::string("[1, 2, \"xyz\"]")));
	map.emplace(Util::StringIdentifier("second string"), Util::Generic(std::string("hello")));
	map.emplace(Util::StringIdentifier("first number"), Util::Generic(12345.6f));
	map.emplace(Util::StringIdentifier("second number"), Util::Generic(-4321.1f));

	GenericMap innerMap;
	innerMap.emplace(Util::StringIdentifier("a"), Util::Generic(std::string("one")));
	innerMap.emplace(Util::StringIdentifier("b"), Util::Generic(std::string("two")));
	innerMap.emplace(Util::StringIdentifier("c"), Util::Generic(std::string("three")));
	map.emplace(Util::StringIdentifier("innerMap"), Util::Generic(innerMap));

	const Util::Generic genericMap(map);

	std::stringstream tempStream;
	Util::GenericConversion::toJSON(genericMap, tempStream);
	const std::string serialization = tempStream.str();

	const Util::Generic importedGenericArray = Util::GenericConversion::fromJSON(tempStream);
	CPPUNIT_ASSERT(checkGenericsEqual(genericMap, importedGenericArray));
}
Ejemplo n.º 2
0
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();
}