|
@@ -4,6 +4,8 @@
|
|
using namespace Framework;
|
|
using namespace Framework;
|
|
using namespace JSON;
|
|
using namespace JSON;
|
|
|
|
|
|
|
|
+#pragma region JSONValue
|
|
|
|
+
|
|
JSONValue::JSONValue()
|
|
JSONValue::JSONValue()
|
|
: ReferenceCounter()
|
|
: ReferenceCounter()
|
|
{
|
|
{
|
|
@@ -59,6 +61,9 @@ JSONObject* JSONValue::asObject() const
|
|
return (JSONObject*)this;
|
|
return (JSONObject*)this;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#pragma endregion Content
|
|
|
|
+
|
|
|
|
+#pragma region JSONBool
|
|
|
|
|
|
JSONBool::JSONBool(bool b)
|
|
JSONBool::JSONBool(bool b)
|
|
: JSONValue(JSONType::BOOLEAN)
|
|
: JSONValue(JSONType::BOOLEAN)
|
|
@@ -84,6 +89,9 @@ JSONValue* JSONBool::clone() const
|
|
return new JSONBool(b);
|
|
return new JSONBool(b);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#pragma endregion Content
|
|
|
|
+
|
|
|
|
+#pragma region JSONNumber
|
|
|
|
|
|
JSONNumber::JSONNumber(double num)
|
|
JSONNumber::JSONNumber(double num)
|
|
: JSONValue(JSONType::NUMBER)
|
|
: JSONValue(JSONType::NUMBER)
|
|
@@ -106,6 +114,9 @@ JSONValue* JSONNumber::clone() const
|
|
return new JSONNumber(number);
|
|
return new JSONNumber(number);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#pragma endregion Content
|
|
|
|
+
|
|
|
|
+#pragma region JSONString
|
|
|
|
|
|
JSONString::JSONString(Text string)
|
|
JSONString::JSONString(Text string)
|
|
: JSONValue(JSONType::STRING)
|
|
: JSONValue(JSONType::STRING)
|
|
@@ -131,6 +142,9 @@ JSONValue* JSONString::clone() const
|
|
return new JSONString(string);
|
|
return new JSONString(string);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#pragma endregion Content
|
|
|
|
+
|
|
|
|
+#pragma region JSONArray
|
|
|
|
|
|
JSONArray::JSONArray()
|
|
JSONArray::JSONArray()
|
|
: JSONValue(JSONType::ARRAY)
|
|
: JSONValue(JSONType::ARRAY)
|
|
@@ -239,6 +253,9 @@ JSONValue* JSONArray::clone() const
|
|
return new JSONArray(toString());
|
|
return new JSONArray(toString());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#pragma endregion Content
|
|
|
|
+
|
|
|
|
+#pragma region JSONObject
|
|
|
|
|
|
JSONObject::JSONObject()
|
|
JSONObject::JSONObject()
|
|
: JSONValue(JSONType::OBJECT)
|
|
: JSONValue(JSONType::OBJECT)
|
|
@@ -399,6 +416,8 @@ JSONValue* JSONObject::clone() const
|
|
return new JSONObject(toString());
|
|
return new JSONObject(toString());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#pragma endregion Content
|
|
|
|
+
|
|
JSONValue* JSON::loadJSONFromFile(Text path)
|
|
JSONValue* JSON::loadJSONFromFile(Text path)
|
|
{
|
|
{
|
|
Datei d;
|
|
Datei d;
|
|
@@ -414,6 +433,8 @@ JSONValue* JSON::loadJSONFromFile(Text path)
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#pragma region Parser
|
|
|
|
+
|
|
int Parser::findObjectEndInArray(const char* str)
|
|
int Parser::findObjectEndInArray(const char* str)
|
|
{
|
|
{
|
|
return findValueEndInObject(str);
|
|
return findValueEndInObject(str);
|
|
@@ -633,9 +654,11 @@ int Parser::findValueEndInObject(const char* str)
|
|
return i;
|
|
return i;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#pragma endregion Content
|
|
|
|
|
|
using namespace Validator;
|
|
using namespace Validator;
|
|
|
|
|
|
|
|
+#pragma region JSONValidationResult
|
|
|
|
|
|
JSONValidationResult::JSONValidationResult()
|
|
JSONValidationResult::JSONValidationResult()
|
|
: ReferenceCounter()
|
|
: ReferenceCounter()
|
|
@@ -643,6 +666,7 @@ JSONValidationResult::JSONValidationResult()
|
|
|
|
|
|
JSONValidationResult::~JSONValidationResult() {}
|
|
JSONValidationResult::~JSONValidationResult() {}
|
|
|
|
|
|
|
|
+#pragma region JSONTypeMissmatch
|
|
|
|
|
|
JSONTypeMissmatch::JSONTypeMissmatch(Text path, JSONValue* foundValue, XML::Element* expected, JSONValidationResult* reason)
|
|
JSONTypeMissmatch::JSONTypeMissmatch(Text path, JSONValue* foundValue, XML::Element* expected, JSONValidationResult* reason)
|
|
: JSONValidationResult()
|
|
: JSONValidationResult()
|
|
@@ -686,6 +710,11 @@ JSONValue* JSONTypeMissmatch::getValidPart() const
|
|
Text* p = reason->getPath().getTeilText(path.getLength());
|
|
Text* p = reason->getPath().getTeilText(path.getLength());
|
|
if (foundValue->getType() == JSONType::ARRAY)
|
|
if (foundValue->getType() == JSONType::ARRAY)
|
|
{
|
|
{
|
|
|
|
+ if (!valid && (!expected->hasAttribute("removeInvalidEntries") || !expected->getAttributeValue("removeInvalidEntries").istGleich("true")))
|
|
|
|
+ {
|
|
|
|
+ p->release();
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
if (p->hatAt(0, "[") && p->hatAt(p->getLength() - 1, "]"))
|
|
if (p->hatAt(0, "[") && p->hatAt(p->getLength() - 1, "]"))
|
|
{
|
|
{
|
|
Text* it = p->getTeilText(1, p->getLength() - 1);
|
|
Text* it = p->getTeilText(1, p->getLength() - 1);
|
|
@@ -720,59 +749,43 @@ JSONValue* JSONTypeMissmatch::getValidPart() const
|
|
}
|
|
}
|
|
else if (foundValue->getType() == JSONType::OBJECT)
|
|
else if (foundValue->getType() == JSONType::OBJECT)
|
|
{
|
|
{
|
|
|
|
+ if (!valid && (!expected->hasAttribute("removeInvalidEntries") || !expected->getAttributeValue("removeInvalidEntries").istGleich("true")))
|
|
|
|
+ {
|
|
|
|
+ p->release();
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
if (p->hatAt(0, "."))
|
|
if (p->hatAt(0, "."))
|
|
{
|
|
{
|
|
Text* at = p->getTeilText(1);
|
|
Text* at = p->getTeilText(1);
|
|
Text attr = *at;
|
|
Text attr = *at;
|
|
at->release();
|
|
at->release();
|
|
- if (foundValue->asObject()->hasValue(attr))
|
|
|
|
|
|
+ JSONValue* tmp = foundValue->clone();
|
|
|
|
+ tmp->asObject()->removeValue(attr);
|
|
|
|
+ if (valid)
|
|
|
|
+ tmp->asObject()->addValue(attr, valid);
|
|
|
|
+ JSONValidationResult* res = JSONValidator(dynamic_cast<XML::Element*>(expected->getThis())).validate(tmp);
|
|
|
|
+ if (res->isValid())
|
|
{
|
|
{
|
|
- JSONValue* tmp = foundValue->clone();
|
|
|
|
- tmp->asObject()->removeValue(attr);
|
|
|
|
- if (valid)
|
|
|
|
- tmp->asObject()->addValue(attr, valid);
|
|
|
|
- JSONValidationResult* res = JSONValidator(dynamic_cast<XML::Element*>(expected->getThis())).validate(tmp);
|
|
|
|
- if (res->isValid())
|
|
|
|
- {
|
|
|
|
- res->release();
|
|
|
|
- p->release();
|
|
|
|
- return tmp;
|
|
|
|
- }
|
|
|
|
- else if (res->isDifferent(this, path) || !valid)
|
|
|
|
- {
|
|
|
|
- p->release();
|
|
|
|
- tmp->release();
|
|
|
|
- JSONValue* result = res->getValidPart();
|
|
|
|
- res->release();
|
|
|
|
- return result;
|
|
|
|
- }
|
|
|
|
|
|
+ res->release();
|
|
|
|
+ p->release();
|
|
|
|
+ return tmp;
|
|
|
|
+ }
|
|
|
|
+ else if (res->isDifferent(this, path) || !valid)
|
|
|
|
+ {
|
|
|
|
+ p->release();
|
|
tmp->release();
|
|
tmp->release();
|
|
|
|
+ JSONValue* result = res->getValidPart();
|
|
res->release();
|
|
res->release();
|
|
|
|
+ return result;
|
|
}
|
|
}
|
|
|
|
+ tmp->release();
|
|
|
|
+ res->release();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
p->release();
|
|
p->release();
|
|
if (valid)
|
|
if (valid)
|
|
valid->release();
|
|
valid->release();
|
|
}
|
|
}
|
|
- if (expected->hasAttribute("default"))
|
|
|
|
- {
|
|
|
|
- JSONValue* def = Parser::getValue(expected->getAttributeValue("default"));
|
|
|
|
- JSONValidationResult* res = JSONValidator(dynamic_cast<XML::Element*>(expected->getThis())).validate(def);
|
|
|
|
- if (res->isValid())
|
|
|
|
- {
|
|
|
|
- res->release();
|
|
|
|
- return def;
|
|
|
|
- }
|
|
|
|
- else if (res->isDifferent(this, path))
|
|
|
|
- {
|
|
|
|
- def->release();
|
|
|
|
- JSONValue* result = res->getValidPart();
|
|
|
|
- res->release();
|
|
|
|
- return result;
|
|
|
|
- }
|
|
|
|
- def->release();
|
|
|
|
- }
|
|
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -791,6 +804,9 @@ bool JSONTypeMissmatch::isDifferent(const JSONValidationResult* zResult, Text ad
|
|
return reason->isDifferent(casted->reason, path);
|
|
return reason->isDifferent(casted->reason, path);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#pragma endregion Content
|
|
|
|
+
|
|
|
|
+#pragma region JSONUnknownValue
|
|
|
|
|
|
JSONUnknownValue::JSONUnknownValue(Text path, JSONValue* foundValue)
|
|
JSONUnknownValue::JSONUnknownValue(Text path, JSONValue* foundValue)
|
|
: JSONValidationResult()
|
|
: JSONValidationResult()
|
|
@@ -813,7 +829,7 @@ void JSONUnknownValue::printInvalidInfo(int indent) const
|
|
{
|
|
{
|
|
Text ind = "";
|
|
Text ind = "";
|
|
ind.fillText(' ', indent);
|
|
ind.fillText(' ', indent);
|
|
- std::cout << ind.getText() << "Unknown Value at '" << path.getText() << "'. Value found:\n" << foundValue->toString().getText() << "\n";
|
|
|
|
|
|
+ std::cout << ind.getText() << "Unknown Value at '" << path.getText() << "'. Value found:\n" << ind.getText() << foundValue->toString().getText() << "\n";
|
|
}
|
|
}
|
|
|
|
|
|
JSONValue* JSONUnknownValue::getValidPart() const
|
|
JSONValue* JSONUnknownValue::getValidPart() const
|
|
@@ -836,6 +852,9 @@ bool JSONUnknownValue::isDifferent(const JSONValidationResult* zResult, Text add
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#pragma endregion Content
|
|
|
|
+
|
|
|
|
+#pragma region JSONMissingValue
|
|
|
|
|
|
JSONMissingValue::JSONMissingValue(Text path, XML::Element* expected)
|
|
JSONMissingValue::JSONMissingValue(Text path, XML::Element* expected)
|
|
: JSONValidationResult()
|
|
: JSONValidationResult()
|
|
@@ -858,7 +877,7 @@ void JSONMissingValue::printInvalidInfo(int indent) const
|
|
{
|
|
{
|
|
Text ind = "";
|
|
Text ind = "";
|
|
ind.fillText(' ', indent);
|
|
ind.fillText(' ', indent);
|
|
- std::cout << ind.getText() << "Missing Value at '" << path.getText() << "'. Expected type:\n" << expected->toString().getText() << "\n";
|
|
|
|
|
|
+ std::cout << ind.getText() << "Missing Value at '" << path.getText() << "'. Expected type:\n" << ind.getText() << expected->toString().getText() << "\n";
|
|
}
|
|
}
|
|
|
|
|
|
JSONValue* JSONMissingValue::getValidPart() const
|
|
JSONValue* JSONMissingValue::getValidPart() const
|
|
@@ -899,6 +918,9 @@ bool JSONMissingValue::isDifferent(const JSONValidationResult* zResult, Text add
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#pragma endregion Content
|
|
|
|
+
|
|
|
|
+#pragma region JSONMissingOneOf
|
|
|
|
|
|
JSONMissingOneOf::JSONMissingOneOf(Text path, XML::Editor expected)
|
|
JSONMissingOneOf::JSONMissingOneOf(Text path, XML::Editor expected)
|
|
: JSONValidationResult()
|
|
: JSONValidationResult()
|
|
@@ -949,6 +971,9 @@ bool JSONMissingOneOf::isDifferent(const JSONValidationResult* zResult, Text add
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#pragma endregion Content
|
|
|
|
+
|
|
|
|
+#pragma region JSONNoTypeMatching
|
|
|
|
|
|
JSONNoTypeMatching::JSONNoTypeMatching(Text path, JSONValue* foundValue, RCArray<XML::Element>& expected, RCArray<JSONValidationResult>& reasons)
|
|
JSONNoTypeMatching::JSONNoTypeMatching(Text path, JSONValue* foundValue, RCArray<XML::Element>& expected, RCArray<JSONValidationResult>& reasons)
|
|
: JSONValidationResult()
|
|
: JSONValidationResult()
|
|
@@ -1013,6 +1038,9 @@ bool JSONNoTypeMatching::isDifferent(const JSONValidationResult* zResult, Text a
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#pragma endregion Content
|
|
|
|
+
|
|
|
|
+#pragma region JSONValidValue
|
|
|
|
|
|
JSONValidValue::JSONValidValue(Text path, JSONValue* value)
|
|
JSONValidValue::JSONValidValue(Text path, JSONValue* value)
|
|
: JSONValidationResult()
|
|
: JSONValidationResult()
|
|
@@ -1054,6 +1082,11 @@ bool JSONValidValue::isDifferent(const JSONValidationResult* zResult, Text addit
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#pragma endregion Content
|
|
|
|
+
|
|
|
|
+#pragma endregion Content
|
|
|
|
+
|
|
|
|
+#pragma region JSONValidator
|
|
|
|
|
|
JSONValidator::JSONValidator(XML::Element* constraints)
|
|
JSONValidator::JSONValidator(XML::Element* constraints)
|
|
: ReferenceCounter(),
|
|
: ReferenceCounter(),
|
|
@@ -1163,15 +1196,15 @@ JSONValidationResult* JSONValidator::validate(JSONValue* zValue, XML::Element* z
|
|
{
|
|
{
|
|
return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
|
|
return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
|
|
}
|
|
}
|
|
- else if (zConstraints->hasAttribute("contains") && zValue->asString()->getString().hat(zConstraints->getAttributeValue("contains")))
|
|
|
|
|
|
+ else if (zConstraints->hasAttribute("contains") && zValue->asString()->getString().hat(zConstraints->getAttributeValue("contains").getText()))
|
|
{
|
|
{
|
|
return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
|
|
return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
|
|
}
|
|
}
|
|
- else if (zConstraints->hasAttribute("startsWith") && zValue->asString()->getString().positionVon(zConstraints->getAttributeValue("startsWith")) == 0)
|
|
|
|
|
|
+ else if (zConstraints->hasAttribute("startsWith") && zValue->asString()->getString().positionVon(zConstraints->getAttributeValue("startsWith").getText()) == 0)
|
|
{
|
|
{
|
|
return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
|
|
return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
|
|
}
|
|
}
|
|
- else if (zConstraints->hasAttribute("endsWith") && zValue->asString()->getString().positionVon(zConstraints->getAttributeValue("endsWith")) == zValue->asString()->getString().getLength() - zConstraints->getAttributeValue("endsWith").getLength())
|
|
|
|
|
|
+ else if (zConstraints->hasAttribute("endsWith") && zValue->asString()->getString().positionVon(zConstraints->getAttributeValue("endsWith").getText()) == zValue->asString()->getString().getLength() - zConstraints->getAttributeValue("endsWith").getLength())
|
|
{
|
|
{
|
|
return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
|
|
return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
|
|
}
|
|
}
|
|
@@ -1184,7 +1217,6 @@ JSONValidationResult* JSONValidator::validate(JSONValue* zValue, XML::Element* z
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- bool valid = 1;
|
|
|
|
int index = 0;
|
|
int index = 0;
|
|
for (JSON::JSONValue* value : *zValue->asArray())
|
|
for (JSON::JSONValue* value : *zValue->asArray())
|
|
{
|
|
{
|
|
@@ -1209,7 +1241,6 @@ JSONValidationResult* JSONValidator::validate(JSONValue* zValue, XML::Element* z
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- bool valid = 1;
|
|
|
|
JSON::JSONObject* obj = zValue->asObject();
|
|
JSON::JSONObject* obj = zValue->asObject();
|
|
for (auto i = obj->getFields(); i; i++)
|
|
for (auto i = obj->getFields(); i; i++)
|
|
{
|
|
{
|
|
@@ -1218,7 +1249,7 @@ JSONValidationResult* JSONValidator::validate(JSONValue* zValue, XML::Element* z
|
|
p += i.val();
|
|
p += i.val();
|
|
if (!zConstraints->selectChildsByAttribute("name", i.val()).exists())
|
|
if (!zConstraints->selectChildsByAttribute("name", i.val()).exists())
|
|
{
|
|
{
|
|
- return new JSONUnknownValue(p, zValue->asObject()->getValue(i.val()));
|
|
|
|
|
|
+ return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), new JSONUnknownValue(p, zValue->asObject()->getValue(i.val())));
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
@@ -1239,8 +1270,8 @@ JSONValidationResult* JSONValidator::validate(JSONValue* zValue, XML::Element* z
|
|
p += ".";
|
|
p += ".";
|
|
p += constraint->getAttributeValue("name");
|
|
p += constraint->getAttributeValue("name");
|
|
if (constraint->getChildCount() != 1)
|
|
if (constraint->getChildCount() != 1)
|
|
- return new JSONMissingOneOf(p, constraint->selectChildren());
|
|
|
|
- return new JSONMissingValue(path, dynamic_cast<XML::Element*>(constraint->selectChildren().begin().val()->getThis()));
|
|
|
|
|
|
+ return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), new JSONMissingOneOf(p, constraint->selectChildren()));
|
|
|
|
+ return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), new JSONMissingValue(p, dynamic_cast<XML::Element*>(constraint->selectChildren().begin().val()->getThis())));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -1342,4 +1373,6 @@ ArrayValidationBuilder<JSONValidator>* JSONValidator::buildForArray()
|
|
{
|
|
{
|
|
return new JSONValidator(e.dublicate());
|
|
return new JSONValidator(e.dublicate());
|
|
});
|
|
});
|
|
-}
|
|
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#pragma endregion Content
|