2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2011-07-21 21:29:22 +03:00
|
|
|
#include "JsonNode.h"
|
|
|
|
|
2012-07-19 21:52:44 +03:00
|
|
|
#include "HeroBonus.h"
|
2012-07-16 19:18:02 +03:00
|
|
|
|
2011-07-22 19:22:22 +03:00
|
|
|
const JsonNode JsonNode::nullNode;
|
|
|
|
|
2011-07-21 21:29:22 +03:00
|
|
|
JsonNode::JsonNode(JsonType Type):
|
|
|
|
type(DATA_NULL)
|
|
|
|
{
|
|
|
|
setType(Type);
|
|
|
|
}
|
|
|
|
|
2011-09-23 18:58:18 +03:00
|
|
|
JsonNode::JsonNode(const char *data, size_t datasize):
|
2011-07-21 21:29:22 +03:00
|
|
|
type(DATA_NULL)
|
|
|
|
{
|
2011-09-23 18:58:18 +03:00
|
|
|
JsonParser parser(data, datasize, *this);
|
2011-10-08 19:58:25 +03:00
|
|
|
JsonValidator validator(*this);
|
2011-07-21 21:29:22 +03:00
|
|
|
}
|
|
|
|
|
2011-09-23 18:58:18 +03:00
|
|
|
JsonNode::JsonNode(std::string filename):
|
2011-08-20 14:27:09 +03:00
|
|
|
type(DATA_NULL)
|
2011-08-20 04:04:59 +03:00
|
|
|
{
|
2011-09-23 18:58:18 +03:00
|
|
|
FILE * file = fopen(filename.c_str(), "rb");
|
2011-11-25 22:41:23 +03:00
|
|
|
if (!file)
|
2012-06-16 13:41:14 +03:00
|
|
|
{
|
|
|
|
tlog1 << "Failed to open file " << filename << "\n";
|
|
|
|
perror("Last system error was ");
|
2011-11-25 22:41:23 +03:00
|
|
|
return;
|
2012-06-16 13:41:14 +03:00
|
|
|
}
|
2011-11-25 22:41:23 +03:00
|
|
|
|
2011-09-23 18:58:18 +03:00
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
size_t datasize = ftell(file);
|
|
|
|
fseek(file, 0, SEEK_SET);
|
2011-10-08 19:58:25 +03:00
|
|
|
|
2011-09-23 18:58:18 +03:00
|
|
|
char *input = new char[datasize];
|
2011-10-08 19:58:25 +03:00
|
|
|
datasize = fread((void*)input, 1, datasize, file);
|
|
|
|
fclose(file);
|
|
|
|
|
2011-09-23 18:58:18 +03:00
|
|
|
JsonParser parser(input, datasize, *this);
|
2011-10-08 19:58:25 +03:00
|
|
|
JsonValidator validator(*this);
|
2011-09-23 18:58:18 +03:00
|
|
|
delete [] input;
|
2011-08-20 04:04:59 +03:00
|
|
|
}
|
|
|
|
|
2011-07-21 21:29:22 +03:00
|
|
|
JsonNode::JsonNode(const JsonNode ©):
|
|
|
|
type(DATA_NULL)
|
|
|
|
{
|
2011-10-08 19:58:25 +03:00
|
|
|
setType(copy.getType());
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
break; case DATA_NULL:
|
|
|
|
break; case DATA_BOOL: Bool() = copy.Bool();
|
|
|
|
break; case DATA_FLOAT: Float() = copy.Float();
|
|
|
|
break; case DATA_STRING: String() = copy.String();
|
|
|
|
break; case DATA_VECTOR: Vector() = copy.Vector();
|
|
|
|
break; case DATA_STRUCT: Struct() = copy.Struct();
|
|
|
|
}
|
2011-07-21 21:29:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode::~JsonNode()
|
|
|
|
{
|
|
|
|
setType(DATA_NULL);
|
|
|
|
}
|
|
|
|
|
2011-10-08 19:58:25 +03:00
|
|
|
void JsonNode::swap(JsonNode &b)
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
2011-10-08 19:58:25 +03:00
|
|
|
using std::swap;
|
|
|
|
swap(data, b.data);
|
|
|
|
swap(type, b.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode & JsonNode::operator =(JsonNode node)
|
|
|
|
{
|
|
|
|
swap(node);
|
2011-07-21 21:29:22 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
bool JsonNode::operator == (const JsonNode &other) const
|
|
|
|
{
|
|
|
|
if (getType() == other.getType())
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
break; case DATA_NULL: return true;
|
|
|
|
break; case DATA_BOOL: return Bool() == other.Bool();
|
|
|
|
break; case DATA_FLOAT: return Float() == other.Float();
|
|
|
|
break; case DATA_STRING: return String() == other.String();
|
|
|
|
break; case DATA_VECTOR: return Vector() == other.Vector();
|
|
|
|
break; case DATA_STRUCT: return Struct() == other.Struct();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonNode::operator != (const JsonNode &other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonNode::minimize(const JsonNode& schema)
|
|
|
|
{
|
|
|
|
JsonValidator validator(*this, schema, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonNode::validate(const JsonNode& schema)
|
|
|
|
{
|
|
|
|
JsonValidator validator(*this, schema, false);
|
|
|
|
}
|
|
|
|
|
2011-07-21 21:29:22 +03:00
|
|
|
JsonNode::JsonType JsonNode::getType() const
|
|
|
|
{
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonNode::setType(JsonType Type)
|
|
|
|
{
|
|
|
|
if (type == Type)
|
|
|
|
return;
|
|
|
|
|
2011-09-23 18:58:18 +03:00
|
|
|
//Reset node to NULL
|
2011-07-21 21:29:22 +03:00
|
|
|
if (Type != DATA_NULL)
|
|
|
|
setType(DATA_NULL);
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
break; case DATA_STRING: delete data.String;
|
2011-09-23 18:58:18 +03:00
|
|
|
break; case DATA_VECTOR: delete data.Vector;
|
2011-07-21 21:29:22 +03:00
|
|
|
break; case DATA_STRUCT: delete data.Struct;
|
|
|
|
break; default:
|
|
|
|
break;
|
|
|
|
}
|
2011-09-23 18:58:18 +03:00
|
|
|
//Set new node type
|
2011-07-21 21:29:22 +03:00
|
|
|
type = Type;
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
break; case DATA_NULL:
|
|
|
|
break; case DATA_BOOL: data.Bool = false;
|
|
|
|
break; case DATA_FLOAT: data.Float = 0;
|
|
|
|
break; case DATA_STRING: data.String = new std::string;
|
|
|
|
break; case DATA_VECTOR: data.Vector = new JsonVector;
|
|
|
|
break; case DATA_STRUCT: data.Struct = new JsonMap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-22 19:22:22 +03:00
|
|
|
bool JsonNode::isNull() const
|
|
|
|
{
|
|
|
|
return type == DATA_NULL;
|
|
|
|
}
|
|
|
|
|
2011-07-21 21:29:22 +03:00
|
|
|
bool & JsonNode::Bool()
|
|
|
|
{
|
|
|
|
setType(DATA_BOOL);
|
|
|
|
return data.Bool;
|
|
|
|
}
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
double & JsonNode::Float()
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
|
|
|
setType(DATA_FLOAT);
|
|
|
|
return data.Float;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string & JsonNode::String()
|
|
|
|
{
|
|
|
|
setType(DATA_STRING);
|
|
|
|
return *data.String;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonVector & JsonNode::Vector()
|
|
|
|
{
|
|
|
|
setType(DATA_VECTOR);
|
|
|
|
return *data.Vector;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonMap & JsonNode::Struct()
|
|
|
|
{
|
|
|
|
setType(DATA_STRUCT);
|
|
|
|
return *data.Struct;
|
|
|
|
}
|
|
|
|
|
2011-09-23 18:58:18 +03:00
|
|
|
const bool boolDefault = false;
|
2011-08-30 09:19:07 +03:00
|
|
|
const bool & JsonNode::Bool() const
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
2011-09-23 18:58:18 +03:00
|
|
|
if (type == DATA_NULL)
|
|
|
|
return boolDefault;
|
2011-07-21 21:29:22 +03:00
|
|
|
assert(type == DATA_BOOL);
|
|
|
|
return data.Bool;
|
|
|
|
}
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
const double floatDefault = 0;
|
|
|
|
const double & JsonNode::Float() const
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
2011-09-23 18:58:18 +03:00
|
|
|
if (type == DATA_NULL)
|
|
|
|
return floatDefault;
|
2011-07-21 21:29:22 +03:00
|
|
|
assert(type == DATA_FLOAT);
|
|
|
|
return data.Float;
|
|
|
|
}
|
|
|
|
|
2011-09-23 18:58:18 +03:00
|
|
|
const std::string stringDefault = std::string();
|
2011-08-30 09:19:07 +03:00
|
|
|
const std::string & JsonNode::String() const
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
2011-09-23 18:58:18 +03:00
|
|
|
if (type == DATA_NULL)
|
|
|
|
return stringDefault;
|
2011-07-21 21:29:22 +03:00
|
|
|
assert(type == DATA_STRING);
|
|
|
|
return *data.String;
|
|
|
|
}
|
|
|
|
|
2011-09-23 18:58:18 +03:00
|
|
|
const JsonVector vectorDefault = JsonVector();
|
2011-08-30 09:19:07 +03:00
|
|
|
const JsonVector & JsonNode::Vector() const
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
2011-09-23 18:58:18 +03:00
|
|
|
if (type == DATA_NULL)
|
|
|
|
return vectorDefault;
|
2011-07-21 21:29:22 +03:00
|
|
|
assert(type == DATA_VECTOR);
|
|
|
|
return *data.Vector;
|
|
|
|
}
|
|
|
|
|
2011-09-23 18:58:18 +03:00
|
|
|
const JsonMap mapDefault = JsonMap();
|
2011-08-30 09:19:07 +03:00
|
|
|
const JsonMap & JsonNode::Struct() const
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
2011-09-23 18:58:18 +03:00
|
|
|
if (type == DATA_NULL)
|
|
|
|
return mapDefault;
|
2011-07-21 21:29:22 +03:00
|
|
|
assert(type == DATA_STRUCT);
|
|
|
|
return *data.Struct;
|
|
|
|
}
|
|
|
|
|
2011-07-22 19:22:22 +03:00
|
|
|
JsonNode & JsonNode::operator[](std::string child)
|
|
|
|
{
|
|
|
|
return Struct()[child];
|
|
|
|
}
|
|
|
|
|
|
|
|
const JsonNode & JsonNode::operator[](std::string child) const
|
|
|
|
{
|
|
|
|
JsonMap::const_iterator it = Struct().find(child);
|
|
|
|
if (it != Struct().end())
|
|
|
|
return it->second;
|
|
|
|
return nullNode;
|
|
|
|
}
|
|
|
|
|
2011-07-21 21:29:22 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-10-08 19:58:25 +03:00
|
|
|
template<typename Iterator>
|
|
|
|
void JsonWriter::writeContainer(Iterator begin, Iterator end)
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
|
|
|
if (begin == end)
|
|
|
|
return;
|
2012-07-19 21:52:44 +03:00
|
|
|
|
2011-10-08 19:58:25 +03:00
|
|
|
prefix += '\t';
|
|
|
|
end--;
|
|
|
|
while (begin != end)
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
2011-10-08 19:58:25 +03:00
|
|
|
writeEntry(begin++);
|
2011-07-21 21:29:22 +03:00
|
|
|
out<<",\n";
|
|
|
|
}
|
2012-07-19 21:52:44 +03:00
|
|
|
|
2011-10-08 19:58:25 +03:00
|
|
|
writeEntry(begin);
|
2011-07-21 21:29:22 +03:00
|
|
|
out<<"\n";
|
2011-10-08 19:58:25 +03:00
|
|
|
prefix.resize(prefix.size()-1);
|
2011-07-21 21:29:22 +03:00
|
|
|
}
|
|
|
|
|
2011-10-08 19:58:25 +03:00
|
|
|
void JsonWriter::writeEntry(JsonMap::const_iterator entry)
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
|
|
|
out << prefix;
|
2011-10-08 19:58:25 +03:00
|
|
|
writeString(entry->first);
|
|
|
|
out << " : ";
|
|
|
|
writeNode(entry->second);
|
2011-07-21 21:29:22 +03:00
|
|
|
}
|
|
|
|
|
2011-10-08 19:58:25 +03:00
|
|
|
void JsonWriter::writeEntry(JsonVector::const_iterator entry)
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
2011-10-08 19:58:25 +03:00
|
|
|
out << prefix;
|
|
|
|
writeNode(*entry);
|
2011-07-21 21:29:22 +03:00
|
|
|
}
|
|
|
|
|
2011-10-08 19:58:25 +03:00
|
|
|
void JsonWriter::writeString(const std::string &string)
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
2011-10-08 19:58:25 +03:00
|
|
|
static const std::string escaped = "\"\\/\b\f\n\r\t";
|
|
|
|
|
|
|
|
out <<'\"';
|
|
|
|
size_t pos=0, start=0;
|
|
|
|
for (; pos<string.size(); pos++)
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
2011-10-08 19:58:25 +03:00
|
|
|
size_t escapedChar = escaped.find(string[pos]);
|
2012-07-19 21:52:44 +03:00
|
|
|
|
2011-10-08 19:58:25 +03:00
|
|
|
if (escapedChar != std::string::npos)
|
|
|
|
{
|
|
|
|
out.write(string.data()+start, pos - start);
|
|
|
|
out << '\\' << escaped[escapedChar];
|
|
|
|
start = pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out.write(string.data()+start, pos - start);
|
|
|
|
out <<'\"';
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonWriter::writeNode(const JsonNode &node)
|
|
|
|
{
|
|
|
|
switch(node.getType())
|
|
|
|
{
|
|
|
|
break; case JsonNode::DATA_NULL:
|
2011-07-21 21:29:22 +03:00
|
|
|
out << "null";
|
|
|
|
|
2011-10-08 19:58:25 +03:00
|
|
|
break; case JsonNode::DATA_BOOL:
|
|
|
|
if (node.Bool())
|
2011-07-21 21:29:22 +03:00
|
|
|
out << "true";
|
|
|
|
else
|
|
|
|
out << "false";
|
|
|
|
|
2011-10-08 19:58:25 +03:00
|
|
|
break; case JsonNode::DATA_FLOAT:
|
|
|
|
out << node.Float();
|
2011-07-21 21:29:22 +03:00
|
|
|
|
2011-10-08 19:58:25 +03:00
|
|
|
break; case JsonNode::DATA_STRING:
|
|
|
|
writeString(node.String());
|
2011-07-21 21:29:22 +03:00
|
|
|
|
2011-10-08 19:58:25 +03:00
|
|
|
break; case JsonNode::DATA_VECTOR:
|
2011-07-21 21:29:22 +03:00
|
|
|
out << "[" << "\n";
|
2011-10-08 19:58:25 +03:00
|
|
|
writeContainer(node.Vector().begin(), node.Vector().end());
|
2011-07-21 21:29:22 +03:00
|
|
|
out << prefix << "]";
|
|
|
|
|
2011-10-08 19:58:25 +03:00
|
|
|
break; case JsonNode::DATA_STRUCT:
|
2011-07-21 21:29:22 +03:00
|
|
|
out << "{" << "\n";
|
2011-10-08 19:58:25 +03:00
|
|
|
writeContainer(node.Struct().begin(), node.Struct().end());
|
2011-07-21 21:29:22 +03:00
|
|
|
out << prefix << "}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-08 19:58:25 +03:00
|
|
|
JsonWriter::JsonWriter(std::ostream &output, const JsonNode &node):
|
|
|
|
out(output)
|
|
|
|
{
|
|
|
|
writeNode(node);
|
|
|
|
}
|
|
|
|
|
2011-07-21 21:29:22 +03:00
|
|
|
std::ostream & operator<<(std::ostream &out, const JsonNode &node)
|
|
|
|
{
|
2011-10-08 19:58:25 +03:00
|
|
|
JsonWriter(out, node);
|
2011-07-21 21:29:22 +03:00
|
|
|
return out << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-09-23 18:58:18 +03:00
|
|
|
JsonParser::JsonParser(const char * inputString, size_t stringSize, JsonNode &root):
|
|
|
|
input(inputString, stringSize),
|
2011-07-21 21:29:22 +03:00
|
|
|
lineCount(1),
|
|
|
|
lineStart(0),
|
|
|
|
pos(0)
|
|
|
|
{
|
|
|
|
extractValue(root);
|
2011-08-20 14:27:09 +03:00
|
|
|
extractWhitespace(false);
|
2011-07-21 21:29:22 +03:00
|
|
|
|
|
|
|
//Warn if there are any non-whitespace symbols left
|
2011-08-20 14:27:09 +03:00
|
|
|
if (pos < input.size())
|
2011-07-21 21:29:22 +03:00
|
|
|
error("Not all file was parsed!", true);
|
|
|
|
|
2011-08-20 14:27:09 +03:00
|
|
|
//TODO: better way to show errors (like printing file name as well)
|
2011-11-01 15:58:01 +03:00
|
|
|
tlog3<<errors;
|
2011-07-21 21:29:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonParser::extractSeparator()
|
|
|
|
{
|
|
|
|
if (!extractWhitespace())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ( input[pos] !=':')
|
|
|
|
return error("Separator expected");
|
|
|
|
|
|
|
|
pos++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonParser::extractValue(JsonNode &node)
|
|
|
|
{
|
|
|
|
if (!extractWhitespace())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
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);
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
if (input[pos] >= '0' && input[pos] <= '9')
|
|
|
|
return extractFloat(node);
|
|
|
|
return error("Value expected!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-20 14:27:09 +03:00
|
|
|
bool JsonParser::extractWhitespace(bool verbose)
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
while (pos < input.size() && (ui8)input[pos] <= ' ')
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
|
|
|
if (input[pos] == '\n')
|
|
|
|
{
|
|
|
|
lineCount++;
|
|
|
|
lineStart = pos+1;
|
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
if (pos >= input.size() || input[pos] != '/')
|
|
|
|
break;
|
|
|
|
|
|
|
|
pos++;
|
|
|
|
if (pos == input.size())
|
|
|
|
break;
|
|
|
|
if (input[pos] == '/')
|
|
|
|
pos++;
|
|
|
|
else
|
2011-10-08 19:58:25 +03:00
|
|
|
error("Comments must consist from two slashes!", true);
|
2011-07-21 21:29:22 +03:00
|
|
|
|
2011-09-23 18:58:18 +03:00
|
|
|
while (pos < input.size() && input[pos] != '\n')
|
|
|
|
pos++;
|
2011-07-21 21:29:22 +03:00
|
|
|
}
|
|
|
|
|
2011-08-20 14:27:09 +03:00
|
|
|
if (pos >= input.size() && verbose)
|
2011-07-21 21:29:22 +03:00
|
|
|
return error("Unexpected end of file!");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonParser::extractEscaping(std::string &str)
|
|
|
|
{
|
|
|
|
switch(input[pos++])
|
|
|
|
{
|
|
|
|
break; case '\"': str += '\"';
|
|
|
|
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';
|
2011-10-08 19:58:25 +03:00
|
|
|
break; default: return error("Unknown escape sequence!", true);
|
2011-07-21 21:29:22 +03:00
|
|
|
};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonParser::extractString(std::string &str)
|
|
|
|
{
|
|
|
|
if (input[pos] != '\"')
|
|
|
|
return error("String expected!");
|
|
|
|
pos++;
|
|
|
|
|
|
|
|
size_t first = pos;
|
|
|
|
|
|
|
|
while (pos != input.size())
|
|
|
|
{
|
|
|
|
if (input[pos] == '\"') // Correct end of string
|
|
|
|
{
|
2011-09-23 18:58:18 +03:00
|
|
|
str.append( &input[first], pos-first);
|
2011-07-21 21:29:22 +03:00
|
|
|
pos++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (input[pos] == '\\') // Escaping
|
|
|
|
{
|
2011-09-23 18:58:18 +03:00
|
|
|
str.append( &input[first], pos-first);
|
2011-07-21 21:29:22 +03:00
|
|
|
first = pos++;
|
|
|
|
if (pos == input.size())
|
|
|
|
break;
|
|
|
|
extractEscaping(str);
|
|
|
|
}
|
|
|
|
if (input[pos] == '\n') // end-of-line
|
|
|
|
{
|
2011-09-23 18:58:18 +03:00
|
|
|
str.append( &input[first], pos-first);
|
2011-07-21 21:29:22 +03:00
|
|
|
return error("Closing quote not found!", true);
|
|
|
|
}
|
|
|
|
if (input[pos] < ' ') // control character
|
|
|
|
{
|
2011-09-23 18:58:18 +03:00
|
|
|
str.append( &input[first], pos-first);
|
2011-07-21 21:29:22 +03:00
|
|
|
first = pos+1;
|
|
|
|
error("Illegal character in the string!", true);
|
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
return error("Unterminated string!");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonParser::extractString(JsonNode &node)
|
|
|
|
{
|
|
|
|
std::string str;
|
|
|
|
if (!extractString(str))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
node.setType(JsonNode::DATA_STRING);
|
|
|
|
node.String() = str;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonParser::extractLiteral(const std::string &literal)
|
|
|
|
{
|
2011-09-23 18:58:18 +03:00
|
|
|
if (literal.compare(0, literal.size(), &input[pos], literal.size()) != 0)
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
2011-09-23 18:58:18 +03:00
|
|
|
while (pos < input.size() && ((input[pos]>'a' && input[pos]<'z')
|
|
|
|
|| (input[pos]>'A' && input[pos]<'Z')))
|
|
|
|
pos++;
|
2011-07-21 21:29:22 +03:00
|
|
|
return error("Unknown literal found", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
pos += literal.size();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonParser::extractNull(JsonNode &node)
|
|
|
|
{
|
|
|
|
if (!extractLiteral("null"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
node.setType(JsonNode::DATA_NULL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonParser::extractTrue(JsonNode &node)
|
|
|
|
{
|
|
|
|
if (!extractLiteral("true"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
node.Bool() = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonParser::extractFalse(JsonNode &node)
|
|
|
|
{
|
|
|
|
if (!extractLiteral("false"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
node.Bool() = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonParser::extractStruct(JsonNode &node)
|
|
|
|
{
|
|
|
|
node.setType(JsonNode::DATA_STRUCT);
|
|
|
|
pos++;
|
|
|
|
|
|
|
|
if (!extractWhitespace())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
//Empty struct found
|
|
|
|
if (input[pos] == '}')
|
|
|
|
{
|
|
|
|
pos++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (!extractWhitespace())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::string key;
|
|
|
|
if (!extractString(key))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (node.Struct().find(key) != node.Struct().end())
|
|
|
|
error("Dublicated element encountered!", true);
|
|
|
|
|
|
|
|
if (!extractSeparator())
|
|
|
|
return false;
|
|
|
|
|
2011-10-08 19:58:25 +03:00
|
|
|
if (!extractElement(node.Struct()[key], '}'))
|
2011-07-21 21:29:22 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (input[pos] == '}')
|
|
|
|
{
|
|
|
|
pos++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonParser::extractArray(JsonNode &node)
|
|
|
|
{
|
|
|
|
pos++;
|
|
|
|
node.setType(JsonNode::DATA_VECTOR);
|
|
|
|
|
|
|
|
if (!extractWhitespace())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
//Empty array found
|
|
|
|
if (input[pos] == ']')
|
|
|
|
{
|
|
|
|
pos++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2011-10-08 19:58:25 +03:00
|
|
|
//NOTE: currently 50% of time is this vector resizing.
|
|
|
|
//May be useful to use list during parsing and then swap() all items to vector
|
2011-07-21 21:29:22 +03:00
|
|
|
node.Vector().resize(node.Vector().size()+1);
|
|
|
|
|
2011-10-08 19:58:25 +03:00
|
|
|
if (!extractElement(node.Vector().back(), ']'))
|
2011-07-21 21:29:22 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (input[pos] == ']')
|
|
|
|
{
|
|
|
|
pos++;
|
|
|
|
return true;
|
|
|
|
}
|
2011-10-08 19:58:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonParser::extractElement(JsonNode &node, char terminator)
|
|
|
|
{
|
|
|
|
if (!extractValue(node))
|
|
|
|
return false;
|
2011-07-21 21:29:22 +03:00
|
|
|
|
2011-10-08 19:58:25 +03:00
|
|
|
if (!extractWhitespace())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool comma = (input[pos] == ',');
|
|
|
|
if (comma )
|
|
|
|
{
|
|
|
|
pos++;
|
|
|
|
if (!extractWhitespace())
|
|
|
|
return false;
|
2011-07-21 21:29:22 +03:00
|
|
|
}
|
2011-10-08 19:58:25 +03:00
|
|
|
|
|
|
|
if (input[pos] == terminator)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!comma)
|
|
|
|
error("Comma expected!", true);
|
|
|
|
|
|
|
|
return true;
|
2011-07-21 21:29:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonParser::extractFloat(JsonNode &node)
|
|
|
|
{
|
|
|
|
assert(input[pos] == '-' || (input[pos] >= '0' && input[pos] <= '9'));
|
|
|
|
bool negative=false;
|
2011-12-14 00:23:17 +03:00
|
|
|
double result=0;
|
2011-07-21 21:29:22 +03:00
|
|
|
|
|
|
|
if (input[pos] == '-')
|
|
|
|
{
|
|
|
|
pos++;
|
|
|
|
negative = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input[pos] < '0' || input[pos] > '9')
|
|
|
|
return error("Number expected!");
|
2011-10-08 19:58:25 +03:00
|
|
|
|
2011-07-21 21:29:22 +03:00
|
|
|
//Extract integer part
|
|
|
|
while (input[pos] >= '0' && input[pos] <= '9')
|
|
|
|
{
|
|
|
|
result = result*10+(input[pos]-'0');
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input[pos] == '.')
|
|
|
|
{
|
|
|
|
//extract fractional part
|
|
|
|
pos++;
|
2011-12-14 00:23:17 +03:00
|
|
|
double fractMult = 0.1;
|
2011-07-21 21:29:22 +03:00
|
|
|
if (input[pos] < '0' || input[pos] > '9')
|
|
|
|
return error("Decimal part expected!");
|
|
|
|
|
|
|
|
while (input[pos] >= '0' && input[pos] <= '9')
|
|
|
|
{
|
|
|
|
result = result + fractMult*(input[pos]-'0');
|
|
|
|
fractMult /= 10;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//TODO: exponential part
|
|
|
|
if (negative)
|
|
|
|
result = -result;
|
|
|
|
|
|
|
|
node.setType(JsonNode::DATA_FLOAT);
|
|
|
|
node.Float() = result;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
errors += stream.str();
|
|
|
|
|
|
|
|
return warning;
|
|
|
|
}
|
2011-10-08 19:58:25 +03:00
|
|
|
|
|
|
|
static const std::map<std::string, JsonNode::JsonType> stringToType =
|
|
|
|
boost::assign::map_list_of
|
|
|
|
("null", JsonNode::DATA_NULL) ("bool", JsonNode::DATA_BOOL)
|
|
|
|
("number", JsonNode::DATA_FLOAT) ("string", JsonNode::DATA_STRING)
|
|
|
|
("array", JsonNode::DATA_VECTOR) ("object", JsonNode::DATA_STRUCT);
|
|
|
|
|
|
|
|
//Check current schema entry for validness and converts "type" string to JsonType
|
|
|
|
bool JsonValidator::validateSchema(JsonNode::JsonType &type, const JsonNode &schema)
|
|
|
|
{
|
|
|
|
if (schema.isNull())
|
|
|
|
return addMessage("Missing schema for current entry!");
|
|
|
|
|
|
|
|
const JsonNode &nodeType = schema["type"];
|
|
|
|
if (nodeType.isNull())
|
|
|
|
return addMessage("Entry type is not defined in schema!");
|
|
|
|
|
|
|
|
if (nodeType.getType() != JsonNode::DATA_STRING)
|
|
|
|
return addMessage("Entry type must be string!");
|
|
|
|
|
|
|
|
std::map<std::string, JsonNode::JsonType>::const_iterator iter = stringToType.find(nodeType.String());
|
|
|
|
|
|
|
|
if (iter == stringToType.end())
|
|
|
|
return addMessage("Unknown entry type found!");
|
|
|
|
|
|
|
|
type = iter->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Replaces node with default value if needed and calls type-specific validators
|
|
|
|
bool JsonValidator::validateType(JsonNode &node, const JsonNode &schema, JsonNode::JsonType type)
|
|
|
|
{
|
|
|
|
if (node.isNull())
|
|
|
|
{
|
|
|
|
const JsonNode & defaultValue = schema["default"];
|
|
|
|
if (defaultValue.isNull())
|
|
|
|
return addMessage("Null entry without default entry!");
|
|
|
|
else
|
|
|
|
node = defaultValue;
|
|
|
|
}
|
2012-01-12 18:23:00 +03:00
|
|
|
if (minimize && node == schema["default"])
|
|
|
|
{
|
|
|
|
node.setType(JsonNode::DATA_NULL);
|
|
|
|
return false;
|
|
|
|
}
|
2011-10-08 19:58:25 +03:00
|
|
|
|
|
|
|
if (type != node.getType())
|
|
|
|
{
|
|
|
|
node.setType(JsonNode::DATA_NULL);
|
|
|
|
return addMessage("Type mismatch!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == JsonNode::DATA_VECTOR)
|
|
|
|
return validateItems(node, schema["items"]);
|
|
|
|
|
|
|
|
if (type == JsonNode::DATA_STRUCT)
|
|
|
|
return validateProperties(node, schema["properties"]);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Basic checks common for any nodes
|
|
|
|
bool JsonValidator::validateNode(JsonNode &node, const JsonNode &schema, const std::string &name)
|
|
|
|
{
|
|
|
|
currentPath.push_back(name);
|
|
|
|
|
|
|
|
JsonNode::JsonType type = JsonNode::DATA_NULL;
|
2012-01-12 18:23:00 +03:00
|
|
|
if (!validateSchema(type, schema)
|
|
|
|
|| !validateType(node, schema, type))
|
2011-10-08 19:58:25 +03:00
|
|
|
{
|
2012-01-12 18:23:00 +03:00
|
|
|
node.setType(JsonNode::DATA_NULL);
|
2011-10-08 19:58:25 +03:00
|
|
|
currentPath.pop_back();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
currentPath.pop_back();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Checks "items" entry from schema (type-specific check for Vector)
|
|
|
|
bool JsonValidator::validateItems(JsonNode &node, const JsonNode &schema)
|
|
|
|
{
|
|
|
|
JsonNode::JsonType type = JsonNode::DATA_NULL;
|
|
|
|
if (!validateSchema(type, schema))
|
|
|
|
return false;
|
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
bool result = true;
|
2011-10-08 19:58:25 +03:00
|
|
|
BOOST_FOREACH(JsonNode &entry, node.Vector())
|
|
|
|
{
|
|
|
|
if (!validateType(entry, schema, type))
|
2012-01-12 18:23:00 +03:00
|
|
|
{
|
|
|
|
result = false;
|
|
|
|
entry.setType(JsonNode::DATA_NULL);
|
|
|
|
}
|
2011-10-08 19:58:25 +03:00
|
|
|
}
|
2012-01-12 18:23:00 +03:00
|
|
|
return result;
|
2011-10-08 19:58:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//Checks "propertries" entry from schema (type-specific check for Struct)
|
|
|
|
//Function is similar to merging of two sorted lists - check every entry that present in one of the input nodes
|
|
|
|
bool JsonValidator::validateProperties(JsonNode &node, const JsonNode &schema)
|
|
|
|
{
|
|
|
|
if (schema.isNull())
|
|
|
|
return addMessage("Properties entry is missing for struct in schema");
|
|
|
|
|
|
|
|
JsonMap::iterator nodeIter = node.Struct().begin();
|
|
|
|
JsonMap::const_iterator schemaIter = schema.Struct().begin();
|
|
|
|
|
|
|
|
while (nodeIter != node.Struct().end() && schemaIter != schema.Struct().end())
|
|
|
|
{
|
2012-01-12 18:23:00 +03:00
|
|
|
if (nodeIter->first < schemaIter->first) //No schema for entry
|
|
|
|
{
|
|
|
|
validateNode(nodeIter->second, JsonNode::nullNode, nodeIter->first);
|
2011-10-08 19:58:25 +03:00
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
JsonMap::iterator toRemove = nodeIter++;
|
|
|
|
node.Struct().erase(toRemove);
|
|
|
|
}
|
2011-10-08 19:58:25 +03:00
|
|
|
else
|
2012-01-12 18:23:00 +03:00
|
|
|
if (schemaIter->first < nodeIter->first) //No entry
|
|
|
|
{
|
|
|
|
if (!validateNode(node[schemaIter->first], schemaIter->second, schemaIter->first))
|
|
|
|
node.Struct().erase(schemaIter->first);
|
2011-10-08 19:58:25 +03:00
|
|
|
schemaIter++;
|
2012-01-12 18:23:00 +03:00
|
|
|
}
|
|
|
|
else //both entry and schema are present
|
2011-10-08 19:58:25 +03:00
|
|
|
{
|
2012-01-12 18:23:00 +03:00
|
|
|
JsonMap::iterator current = nodeIter++;
|
|
|
|
if (!validateNode(current->second, schemaIter->second, current->first))
|
|
|
|
node.Struct().erase(current);
|
|
|
|
|
2011-10-08 19:58:25 +03:00
|
|
|
schemaIter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (nodeIter != node.Struct().end())
|
|
|
|
{
|
2012-01-12 18:23:00 +03:00
|
|
|
validateNode(nodeIter->second, JsonNode::nullNode, nodeIter->first);
|
|
|
|
JsonMap::iterator toRemove = nodeIter++;
|
|
|
|
node.Struct().erase(toRemove);
|
2011-10-08 19:58:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
while (schemaIter != schema.Struct().end())
|
|
|
|
{
|
2012-01-12 18:23:00 +03:00
|
|
|
if (!validateNode(node[schemaIter->first], schemaIter->second, schemaIter->first))
|
|
|
|
node.Struct().erase(schemaIter->first);
|
2011-10-08 19:58:25 +03:00
|
|
|
schemaIter++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonValidator::addMessage(const std::string &message)
|
|
|
|
{
|
|
|
|
std::ostringstream stream;
|
|
|
|
|
|
|
|
stream << "At ";
|
|
|
|
BOOST_FOREACH(const std::string &path, currentPath)
|
|
|
|
stream << path<<"/";
|
|
|
|
stream << "\t Error: " << message <<"\n";
|
|
|
|
errors += stream.str();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
JsonValidator::JsonValidator(JsonNode &root, bool Minimize):
|
|
|
|
minimize(Minimize)
|
2011-10-08 19:58:25 +03:00
|
|
|
{
|
2011-11-01 15:58:01 +03:00
|
|
|
JsonNode schema;
|
|
|
|
schema.swap(root["schema"]);
|
2012-01-12 18:23:00 +03:00
|
|
|
root.Struct().erase("schema");
|
2011-10-08 19:58:25 +03:00
|
|
|
|
|
|
|
if (!schema.isNull())
|
|
|
|
{
|
|
|
|
validateProperties(root, schema);
|
|
|
|
}
|
|
|
|
//This message is quite annoying now - most files do not have schemas. May be re-enabled later
|
|
|
|
//else
|
|
|
|
// addMessage("Schema not found!", true);
|
|
|
|
|
|
|
|
//TODO: better way to show errors (like printing file name as well)
|
2011-11-01 15:58:01 +03:00
|
|
|
tlog3<<errors;
|
2011-10-08 19:58:25 +03:00
|
|
|
}
|
2012-01-12 18:23:00 +03:00
|
|
|
|
|
|
|
JsonValidator::JsonValidator(JsonNode &root, const JsonNode &schema, bool Minimize):
|
|
|
|
minimize(Minimize)
|
|
|
|
{
|
|
|
|
validateProperties(root, schema);
|
|
|
|
if (schema.isNull())
|
|
|
|
addMessage("Schema not found!");
|
|
|
|
tlog3<<errors;
|
2012-07-19 21:52:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Bonus * ParseBonus (const JsonVector &ability_vec) //TODO: merge with AddAbility, create universal parser for all bonus properties
|
|
|
|
{
|
|
|
|
Bonus * b = new Bonus();
|
|
|
|
std::string type = ability_vec[0].String();
|
|
|
|
auto it = bonusNameMap.find(type);
|
|
|
|
if (it == bonusNameMap.end())
|
|
|
|
{
|
|
|
|
tlog1 << "Error: invalid ability type " << type << " in creatures.txt" << std::endl;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
b->type = it->second;
|
|
|
|
b->val = ability_vec[1].Float();
|
|
|
|
b->subtype = ability_vec[2].Float();
|
|
|
|
b->additionalInfo = ability_vec[3].Float();
|
|
|
|
b->duration = Bonus::PERMANENT;
|
|
|
|
b->turnsRemain = 0;
|
|
|
|
return b;
|
|
|
|
}
|