1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00
This commit is contained in:
Ivan Savenko
2024-02-13 22:19:24 +02:00
parent 18bbccd167
commit 29860848a5
14 changed files with 80 additions and 81 deletions

View File

@@ -11,16 +11,17 @@
#include "StdInc.h"
#include "JsonParser.h"
#include "../ScopeGuard.h"
#include "../TextOperations.h"
#include "JsonFormatException.h"
VCMI_LIB_NAMESPACE_BEGIN
JsonParser::JsonParser(const char * inputString, size_t stringSize, const JsonParsingSettings & settings)
: input(inputString, stringSize)
, settings(settings)
, currentDepth(0)
JsonParser::JsonParser(const std::byte * inputString, size_t stringSize, const JsonParsingSettings & settings)
: settings(settings)
, input(reinterpret_cast<const char *>(inputString), stringSize)
, lineCount(1)
, currentDepth(0)
, lineStart(0)
, pos(0)
{
@@ -258,7 +259,6 @@ bool JsonParser::extractLiteral(std::string & literal)
pos++;
}
pos += literal.size();
return true;
}
@@ -309,10 +309,14 @@ bool JsonParser::extractStruct(JsonNode & node)
node.setType(JsonNode::JsonType::DATA_STRUCT);
if(currentDepth > settings.maxDepth)
error("Macimum allowed depth of json structure has been reached", true);
error("Maximum allowed depth of json structure has been reached", true);
currentDepth++;
pos++;
currentDepth++;
auto guard = vstd::makeScopeGuard([this]()
{
currentDepth--;
});
if(!extractWhitespace())
return false;
@@ -393,6 +397,11 @@ bool JsonParser::extractArray(JsonNode & node)
error("Macimum allowed depth of json structure has been reached", true);
currentDepth++;
auto guard = vstd::makeScopeGuard([this]()
{
currentDepth--;
});
pos++;
node.setType(JsonNode::JsonType::DATA_VECTOR);
@@ -441,11 +450,9 @@ bool JsonParser::extractElement(JsonNode & node, char terminator)
if(input[pos] == terminator)
{
if(comma)
{
if(settings.mode < JsonParsingSettings::JsonFormatMode::JSON5)
error("Extra comma found!", true);
}
if(comma && settings.mode < JsonParsingSettings::JsonFormatMode::JSON5)
error("Extra comma found!", true);
return true;
}
@@ -500,11 +507,8 @@ bool JsonParser::extractFloat(JsonNode & node)
pos++;
double fractMult = 0.1;
if(settings.mode < JsonParsingSettings::JsonFormatMode::JSON5)
{
if(input[pos] < '0' || input[pos] > '9')
return error("Decimal part expected!");
}
if(settings.mode < JsonParsingSettings::JsonFormatMode::JSON5 && (input[pos] < '0' || input[pos] > '9'))
return error("Decimal part expected!");
while(input[pos] >= '0' && input[pos] <= '9')
{