mirror of
				https://github.com/vcmi/vcmi.git
				synced 2025-10-31 00:07:39 +02:00 
			
		
		
		
	Merge pull request #4263 from Alexander-Wilms/json-error
JsonParser::parse(): Print file name if there were errors while parsing
This commit is contained in:
		| @@ -43,7 +43,7 @@ void GlobalLobbyClient::onPacketReceived(const std::shared_ptr<INetworkConnectio | ||||
| { | ||||
| 	boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex); | ||||
|  | ||||
| 	JsonNode json(message.data(), message.size()); | ||||
| 	JsonNode json(message.data(), message.size(), "<lobby network packet>"); | ||||
|  | ||||
| 	if(json["type"].String() == "accountCreated") | ||||
| 		return receiveAccountCreated(json); | ||||
|   | ||||
| @@ -191,7 +191,7 @@ void CAnimation::init() | ||||
| 		std::unique_ptr<ui8[]> textData(new ui8[stream->getSize()]); | ||||
| 		stream->read(textData.get(), stream->getSize()); | ||||
|  | ||||
| 		const JsonNode config(reinterpret_cast<const std::byte*>(textData.get()), stream->getSize()); | ||||
| 		const JsonNode config(reinterpret_cast<const std::byte*>(textData.get()), stream->getSize(), jsonResource.getName()); | ||||
|  | ||||
| 		initFromJson(config); | ||||
| 	} | ||||
|   | ||||
| @@ -89,7 +89,7 @@ QVariant JsonFromFile(QString filename) | ||||
| 	} | ||||
|  | ||||
| 	const auto data = file.readAll(); | ||||
| 	JsonNode node(reinterpret_cast<const std::byte*>(data.data()), data.size()); | ||||
| 	JsonNode node(reinterpret_cast<const std::byte*>(data.data()), data.size(), filename.toStdString()); | ||||
| 	return toVariant(node); | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -67,7 +67,7 @@ UpdateDialog::UpdateDialog(bool calledManually, QWidget *parent): | ||||
| 		} | ||||
| 		 | ||||
| 		auto byteArray = response->readAll(); | ||||
| 		JsonNode node(reinterpret_cast<const std::byte*>(byteArray.constData()), byteArray.size()); | ||||
| 		JsonNode node(reinterpret_cast<const std::byte*>(byteArray.constData()), byteArray.size(), "<network packet from server at updateConfigUrl>"); | ||||
| 		loadFromJson(node); | ||||
| 	}); | ||||
| } | ||||
|   | ||||
| @@ -46,7 +46,7 @@ void CampaignHandler::readCampaign(Campaign * ret, const std::vector<ui8> & inpu | ||||
| 	} | ||||
| 	else // text format (json) | ||||
| 	{ | ||||
| 		JsonNode jsonCampaign(reinterpret_cast<const std::byte*>(input.data()), input.size()); | ||||
| 		JsonNode jsonCampaign(reinterpret_cast<const std::byte*>(input.data()), input.size(), filename); | ||||
| 		readHeaderFromJson(*ret, jsonCampaign, filename, modName, encoding); | ||||
|  | ||||
| 		for(auto & scenario : jsonCampaign["scenarios"].Vector()) | ||||
|   | ||||
| @@ -117,7 +117,7 @@ void CFilesystemGenerator::loadJsonMap(const std::string &mountPoint, const Json | ||||
| 	if (filename) | ||||
| 	{ | ||||
| 		auto configData = CResourceHandler::get("initial")->load(JsonPath::builtin(URI))->readAll(); | ||||
| 		const JsonNode configInitial(reinterpret_cast<std::byte *>(configData.first.get()), configData.second); | ||||
| 		const JsonNode configInitial(reinterpret_cast<std::byte *>(configData.first.get()), configData.second, URI); | ||||
| 		filesystem->addLoader(new CMappedFileLoader(mountPoint, configInitial), false); | ||||
| 	} | ||||
| } | ||||
| @@ -212,7 +212,7 @@ void CResourceHandler::load(const std::string &fsConfigURI, bool extractArchives | ||||
| { | ||||
| 	auto fsConfigData = get("initial")->load(JsonPath::builtin(fsConfigURI))->readAll(); | ||||
|  | ||||
| 	const JsonNode fsConfig(reinterpret_cast<std::byte *>(fsConfigData.first.get()), fsConfigData.second); | ||||
| 	const JsonNode fsConfig(reinterpret_cast<std::byte *>(fsConfigData.first.get()), fsConfigData.second, fsConfigURI); | ||||
|  | ||||
| 	addFilesystem("data", ModScope::scopeBuiltin(), createFileSystem("", fsConfig["filesystem"], extractArchives)); | ||||
| } | ||||
|   | ||||
| @@ -86,15 +86,15 @@ JsonNode::JsonNode(const std::string & string) | ||||
| { | ||||
| } | ||||
|  | ||||
| JsonNode::JsonNode(const std::byte * data, size_t datasize) | ||||
| 	: JsonNode(data, datasize, JsonParsingSettings()) | ||||
| JsonNode::JsonNode(const std::byte * data, size_t datasize, const std::string & fileName) | ||||
| 	: JsonNode(data, datasize, JsonParsingSettings(), fileName) | ||||
| { | ||||
| } | ||||
|  | ||||
| JsonNode::JsonNode(const std::byte * data, size_t datasize, const JsonParsingSettings & parserSettings) | ||||
| JsonNode::JsonNode(const std::byte * data, size_t datasize, const JsonParsingSettings & parserSettings, const std::string & fileName) | ||||
| { | ||||
| 	JsonParser parser(data, datasize, parserSettings); | ||||
| 	*this = parser.parse("<unknown>"); | ||||
| 	*this = parser.parse(fileName); | ||||
| } | ||||
|  | ||||
| JsonNode::JsonNode(const JsonPath & fileURI) | ||||
|   | ||||
| @@ -72,8 +72,8 @@ public: | ||||
| 	explicit JsonNode(const std::string & string); | ||||
|  | ||||
| 	/// Create tree from Json-formatted input | ||||
| 	explicit JsonNode(const std::byte * data, size_t datasize); | ||||
| 	explicit JsonNode(const std::byte * data, size_t datasize, const JsonParsingSettings & parserSettings); | ||||
| 	explicit JsonNode(const std::byte * data, size_t datasize, const std::string & fileName); | ||||
| 	explicit JsonNode(const std::byte * data, size_t datasize, const JsonParsingSettings & parserSettings, const std::string & fileName); | ||||
|  | ||||
| 	/// Create tree from JSON file | ||||
| 	explicit JsonNode(const JsonPath & fileURI); | ||||
|   | ||||
| @@ -55,7 +55,7 @@ JsonNode JsonParser::parse(const std::string & fileName) | ||||
|  | ||||
| 	if(!errors.empty()) | ||||
| 	{ | ||||
| 		logMod->warn("File %s is not a valid JSON file!", fileName); | ||||
| 		logMod->warn("%s is not valid JSON!", fileName); | ||||
| 		logMod->warn(errors); | ||||
| 	} | ||||
| 	return root; | ||||
|   | ||||
| @@ -269,7 +269,7 @@ JsonNode JsonUtils::assembleFromFiles(const std::string & filename) | ||||
| 	for(auto & loader : CResourceHandler::get()->getResourcesWithName(resID)) | ||||
| 	{ | ||||
| 		auto textData = loader->load(resID)->readAll(); | ||||
| 		JsonNode section(reinterpret_cast<std::byte *>(textData.first.get()), textData.second); | ||||
| 		JsonNode section(reinterpret_cast<std::byte *>(textData.first.get()), textData.second, resID.getName()); | ||||
| 		merge(result, section); | ||||
| 	} | ||||
| 	return result; | ||||
|   | ||||
| @@ -813,7 +813,7 @@ JsonNode CMapLoaderJson::getFromArchive(const std::string & archiveFilename) | ||||
|  | ||||
| 	auto data = loader.load(resource)->readAll(); | ||||
|  | ||||
| 	JsonNode res(reinterpret_cast<const std::byte*>(data.first.get()), data.second); | ||||
| 	JsonNode res(reinterpret_cast<const std::byte*>(data.first.get()), data.second, archiveFilename); | ||||
|  | ||||
| 	return res; | ||||
| } | ||||
|   | ||||
| @@ -213,7 +213,7 @@ static JsonNode loadLobbyGameRoomToJson(const LobbyGameRoom & gameRoom) | ||||
| 	jsonEntry["playerLimit"].Integer() = gameRoom.playerLimit; | ||||
| 	jsonEntry["ageSeconds"].Integer() = gameRoom.age.count(); | ||||
| 	if (!gameRoom.modsJson.empty()) // not present in match history | ||||
| 		jsonEntry["mods"] = JsonNode(reinterpret_cast<const std::byte *>(gameRoom.modsJson.data()), gameRoom.modsJson.size()); | ||||
| 		jsonEntry["mods"] = JsonNode(reinterpret_cast<const std::byte *>(gameRoom.modsJson.data()), gameRoom.modsJson.size(), "<lobby "+gameRoom.roomID+">"); | ||||
|  | ||||
| 	for(const auto & account : gameRoom.participants) | ||||
| 		jsonEntry["participants"].Vector().push_back(loadLobbyAccountToJson(account)); | ||||
| @@ -348,7 +348,7 @@ JsonNode LobbyServer::parseAndValidateMessage(const std::vector<std::byte> & mes | ||||
| 	JsonNode json; | ||||
| 	try | ||||
| 	{ | ||||
| 		JsonNode jsonTemp(message.data(), message.size()); | ||||
| 		JsonNode jsonTemp(message.data(), message.size(), "<lobby message>"); | ||||
| 		json = std::move(jsonTemp); | ||||
| 	} | ||||
| 	catch (const JsonFormatException & e) | ||||
|   | ||||
| @@ -599,7 +599,7 @@ void Animation::init() | ||||
| 		std::unique_ptr<ui8[]> textData(new ui8[stream->getSize()]); | ||||
| 		stream->read(textData.get(), stream->getSize()); | ||||
|  | ||||
| 		const JsonNode config(reinterpret_cast<const std::byte*>(textData.get()), stream->getSize()); | ||||
| 		const JsonNode config(reinterpret_cast<const std::byte*>(textData.get()), stream->getSize(), resID.getName()); | ||||
|  | ||||
| 		initFromJson(config); | ||||
| 	} | ||||
|   | ||||
| @@ -97,7 +97,7 @@ QVariant JsonFromFile(QString filename) | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		JsonNode node(reinterpret_cast<const std::byte*>(data.data()), data.size()); | ||||
| 		JsonNode node(reinterpret_cast<const std::byte*>(data.data()), data.size(), filename.toStdString()); | ||||
| 		return toVariant(node); | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -67,7 +67,7 @@ void GlobalLobbyProcessor::onPacketReceived(const std::shared_ptr<INetworkConnec | ||||
| { | ||||
| 	if (connection == controlConnection) | ||||
| 	{ | ||||
| 		JsonNode json(message.data(), message.size()); | ||||
| 		JsonNode json(message.data(), message.size(), "<lobby network packet>"); | ||||
|  | ||||
| 		if(json["type"].String() == "operationFailed") | ||||
| 			return receiveOperationFailed(json); | ||||
|   | ||||
| @@ -95,7 +95,7 @@ static JsonNode getFromArchive(CZipLoader & archive, const std::string & archive | ||||
|  | ||||
| 	auto data = archive.load(resource)->readAll(); | ||||
|  | ||||
| 	JsonNode res(reinterpret_cast<const std::byte *>(data.first.get()), data.second); | ||||
| 	JsonNode res(reinterpret_cast<const std::byte *>(data.first.get()), data.second, resource.getName()); | ||||
|  | ||||
| 	return res; | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user