mirror of
https://github.com/vcmi/vcmi.git
synced 2025-01-02 00:10:22 +02:00
Unify formatting
This commit is contained in:
parent
2632ab04f5
commit
18bbccd167
@ -11,9 +11,9 @@
|
||||
#include "StdInc.h"
|
||||
#include "JsonNode.h"
|
||||
|
||||
#include "filesystem/Filesystem.h"
|
||||
#include "JsonParser.h"
|
||||
#include "JsonWriter.h"
|
||||
#include "filesystem/Filesystem.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
@ -56,31 +56,38 @@ static const JsonNode nullNode;
|
||||
|
||||
JsonNode::JsonNode(bool boolean)
|
||||
: data(boolean)
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
JsonNode::JsonNode(int32_t number)
|
||||
: data(static_cast<int64_t>(number))
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
JsonNode::JsonNode(uint32_t number)
|
||||
: data(static_cast<int64_t>(number))
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
JsonNode::JsonNode(int64_t number)
|
||||
: data(number)
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
JsonNode::JsonNode(double number)
|
||||
: data(number)
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
JsonNode::JsonNode(const std::string & string)
|
||||
: data(string)
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
JsonNode::JsonNode(const std::byte * data, size_t datasize)
|
||||
: JsonNode(data, datasize, JsonParsingSettings())
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
JsonNode::JsonNode(const std::byte * data, size_t datasize, const JsonParsingSettings & parserSettings)
|
||||
{
|
||||
@ -150,14 +157,16 @@ void JsonNode::setModScope(const std::string & metadata, bool recursive)
|
||||
{
|
||||
switch(getType())
|
||||
{
|
||||
break; case JsonType::DATA_VECTOR:
|
||||
break;
|
||||
case JsonType::DATA_VECTOR:
|
||||
{
|
||||
for(auto & node : Vector())
|
||||
{
|
||||
node.setModScope(metadata);
|
||||
}
|
||||
}
|
||||
break; case JsonType::DATA_STRUCT:
|
||||
break;
|
||||
case JsonType::DATA_STRUCT:
|
||||
{
|
||||
for(auto & node : Struct())
|
||||
{
|
||||
@ -190,13 +199,27 @@ void JsonNode::setType(JsonType Type)
|
||||
//Set new node type
|
||||
switch(Type)
|
||||
{
|
||||
break; case JsonType::DATA_NULL: data = JsonData();
|
||||
break; case JsonType::DATA_BOOL: data = JsonData(false);
|
||||
break; case JsonType::DATA_FLOAT: data = JsonData(static_cast<double>(0.0));
|
||||
break; case JsonType::DATA_STRING: data = JsonData(std::string());
|
||||
break; case JsonType::DATA_VECTOR: data = JsonData(JsonVector());
|
||||
break; case JsonType::DATA_STRUCT: data = JsonData(JsonMap());
|
||||
break; case JsonType::DATA_INTEGER: data = JsonData(static_cast<si64>(0));
|
||||
case JsonType::DATA_NULL:
|
||||
data = JsonData();
|
||||
break;
|
||||
case JsonType::DATA_BOOL:
|
||||
data = JsonData(false);
|
||||
break;
|
||||
case JsonType::DATA_FLOAT:
|
||||
data = JsonData(static_cast<double>(0.0));
|
||||
break;
|
||||
case JsonType::DATA_STRING:
|
||||
data = JsonData(std::string());
|
||||
break;
|
||||
case JsonType::DATA_VECTOR:
|
||||
data = JsonData(JsonVector());
|
||||
break;
|
||||
case JsonType::DATA_STRUCT:
|
||||
data = JsonData(JsonMap());
|
||||
break;
|
||||
case JsonType::DATA_INTEGER:
|
||||
data = JsonData(static_cast<si64>(0));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -58,6 +58,7 @@ private:
|
||||
std::string modScope;
|
||||
|
||||
bool overrideFlag = false;
|
||||
|
||||
public:
|
||||
JsonNode() = default;
|
||||
|
||||
@ -145,7 +146,8 @@ public:
|
||||
std::string toString() const;
|
||||
std::vector<std::byte> toBytes() const;
|
||||
|
||||
template <typename Handler> void serialize(Handler &h)
|
||||
template<typename Handler>
|
||||
void serialize(Handler & h)
|
||||
{
|
||||
h & modScope;
|
||||
|
||||
|
@ -11,18 +11,18 @@
|
||||
#include "StdInc.h"
|
||||
#include "JsonParser.h"
|
||||
|
||||
#include "JsonFormatException.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),
|
||||
lineCount(1),
|
||||
lineStart(0),
|
||||
pos(0)
|
||||
JsonParser::JsonParser(const char * inputString, size_t stringSize, const JsonParsingSettings & settings)
|
||||
: input(inputString, stringSize)
|
||||
, settings(settings)
|
||||
, currentDepth(0)
|
||||
, lineCount(1)
|
||||
, lineStart(0)
|
||||
, pos(0)
|
||||
{
|
||||
}
|
||||
|
||||
@ -79,13 +79,20 @@ bool JsonParser::extractValue(JsonNode &node)
|
||||
|
||||
switch(input[pos])
|
||||
{
|
||||
case '\"': return extractString(node);
|
||||
case 'n' : return extractNull(node);
|
||||
case 't' : return extractTrue(node);
|
||||
case 'f' : return extractFalse(node);
|
||||
case '{' : return extractStruct(node);
|
||||
case '[' : return extractArray(node);
|
||||
case '-' : return extractFloat(node);
|
||||
case '\"':
|
||||
return extractString(node);
|
||||
case 'n':
|
||||
return extractNull(node);
|
||||
case 't':
|
||||
return extractTrue(node);
|
||||
case 'f':
|
||||
return extractFalse(node);
|
||||
case '{':
|
||||
return extractStruct(node);
|
||||
case '[':
|
||||
return extractArray(node);
|
||||
case '-':
|
||||
return extractFloat(node);
|
||||
default:
|
||||
{
|
||||
if(input[pos] >= '0' && input[pos] <= '9')
|
||||
@ -142,15 +149,32 @@ bool JsonParser::extractEscaping(std::string &str)
|
||||
|
||||
switch(input[pos])
|
||||
{
|
||||
break; case '\"': str += '\"';
|
||||
break; case '\\': str += '\\';
|
||||
break; case 'b': str += '\b';
|
||||
break; case 'f': str += '\f';
|
||||
break; case 'n': str += '\n';
|
||||
break; case 'r': str += '\r';
|
||||
break; case 't': str += '\t';
|
||||
break; case '/': str += '/';
|
||||
break; default: return error("Unknown escape sequence!", true);
|
||||
case '\"':
|
||||
str += '\"';
|
||||
break;
|
||||
case '\\':
|
||||
str += '\\';
|
||||
break;
|
||||
case 'b':
|
||||
str += '\b';
|
||||
break;
|
||||
case 'f':
|
||||
str += '\f';
|
||||
break;
|
||||
case 'n':
|
||||
str += '\n';
|
||||
break;
|
||||
case 'r':
|
||||
str += '\r';
|
||||
break;
|
||||
case 't':
|
||||
str += '\t';
|
||||
break;
|
||||
case '/':
|
||||
str += '/';
|
||||
break;
|
||||
default:
|
||||
return error("Unknown escape sequence!", true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -551,8 +575,7 @@ bool JsonParser::error(const std::string &message, bool warning)
|
||||
std::ostringstream stream;
|
||||
std::string type(warning ? " warning: " : " error: ");
|
||||
|
||||
stream << "At line " << lineCount << ", position "<<pos-lineStart
|
||||
<< type << message <<"\n";
|
||||
stream << "At line " << lineCount << ", position " << pos - lineStart << type << message << "\n";
|
||||
errors += stream.str();
|
||||
|
||||
return warning;
|
||||
|
@ -9,9 +9,9 @@
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "GameCallbackHolder.h"
|
||||
#include "GameConstants.h"
|
||||
#include "ResourceSet.h"
|
||||
#include "GameCallbackHolder.h"
|
||||
|
||||
VCMI_LIB_NAMESPACE_BEGIN
|
||||
|
||||
|
@ -68,9 +68,7 @@ void JsonWriter::writeString(const std::string &string)
|
||||
for(; pos < string.size(); pos++)
|
||||
{
|
||||
//we need to check if special character was been already escaped
|
||||
if((string[pos] == '\\')
|
||||
&& (pos+1 < string.size())
|
||||
&& (std::find(escaped_code.begin(), escaped_code.end(), string[pos+1]) != escaped_code.end()) )
|
||||
if((string[pos] == '\\') && (pos + 1 < string.size()) && (std::find(escaped_code.begin(), escaped_code.end(), string[pos + 1]) != escaped_code.end()))
|
||||
{
|
||||
pos++; //write unchanged, next simbol also checked
|
||||
}
|
||||
@ -85,7 +83,6 @@ void JsonWriter::writeString(const std::string &string)
|
||||
start = pos + 1;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
out.write(string.data() + start, pos - start);
|
||||
out << '\"';
|
||||
@ -99,40 +96,48 @@ void JsonWriter::writeNode(const JsonNode &node)
|
||||
|
||||
switch(node.getType())
|
||||
{
|
||||
break; case JsonNode::JsonType::DATA_NULL:
|
||||
case JsonNode::JsonType::DATA_NULL:
|
||||
out << "null";
|
||||
break;
|
||||
|
||||
break; case JsonNode::JsonType::DATA_BOOL:
|
||||
case JsonNode::JsonType::DATA_BOOL:
|
||||
if(node.Bool())
|
||||
out << "true";
|
||||
else
|
||||
out << "false";
|
||||
break;
|
||||
|
||||
break; case JsonNode::JsonType::DATA_FLOAT:
|
||||
case JsonNode::JsonType::DATA_FLOAT:
|
||||
out << node.Float();
|
||||
break;
|
||||
|
||||
break; case JsonNode::JsonType::DATA_STRING:
|
||||
case JsonNode::JsonType::DATA_STRING:
|
||||
writeString(node.String());
|
||||
break;
|
||||
|
||||
break; case JsonNode::JsonType::DATA_VECTOR:
|
||||
case JsonNode::JsonType::DATA_VECTOR:
|
||||
out << "[" << (compactMode ? " " : "\n");
|
||||
writeContainer(node.Vector().begin(), node.Vector().end());
|
||||
out << (compactMode ? " " : prefix) << "]";
|
||||
break;
|
||||
|
||||
break; case JsonNode::JsonType::DATA_STRUCT:
|
||||
case JsonNode::JsonType::DATA_STRUCT:
|
||||
out << "{" << (compactMode ? " " : "\n");
|
||||
writeContainer(node.Struct().begin(), node.Struct().end());
|
||||
out << (compactMode ? " " : prefix) << "}";
|
||||
break;
|
||||
|
||||
break; case JsonNode::JsonType::DATA_INTEGER:
|
||||
case JsonNode::JsonType::DATA_INTEGER:
|
||||
out << node.Integer();
|
||||
break;
|
||||
}
|
||||
|
||||
compactMode = originalMode;
|
||||
}
|
||||
|
||||
JsonWriter::JsonWriter(std::ostream & output, bool compact)
|
||||
: out(output), compact(compact)
|
||||
: out(output)
|
||||
, compact(compact)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,7 @@ class JsonWriter
|
||||
bool compact;
|
||||
//tracks whether we are currently using single-line format
|
||||
bool compactMode = false;
|
||||
|
||||
public:
|
||||
template<typename Iterator>
|
||||
void writeContainer(Iterator begin, Iterator end);
|
||||
|
Loading…
Reference in New Issue
Block a user