-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup and add proper validation system.
- Loading branch information
1 parent
85818ff
commit 9214dc0
Showing
6 changed files
with
163 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#ifndef QJSONUTIL_H | ||
#define QJSONUTIL_H | ||
|
||
#include <QJsonValue> | ||
#include <QJsonObject> | ||
#include <functional> | ||
|
||
|
||
enum class VALIDATE_ERROR { | ||
VALIDATE_NO_CHILD, | ||
VALIDATE_WRONG_TYPE | ||
}; | ||
|
||
template<typename T> | ||
inline bool qJsonValidate(const QJsonObject& val, const QString& child, const std::function<void(VALIDATE_ERROR, const QString&)>& errFunc) | ||
{ | ||
throw "err"; | ||
return true; | ||
} | ||
|
||
template<> | ||
inline bool qJsonValidate<int>(const QJsonObject& val, const QString& child, const std::function<void(VALIDATE_ERROR, const QString&)>& errFunc) | ||
{ | ||
if(!val.contains(child)){ | ||
if(errFunc) | ||
errFunc(VALIDATE_ERROR::VALIDATE_NO_CHILD, child); | ||
return false; | ||
} | ||
QJsonValue containedValue = val.value(child); | ||
if(!containedValue.isDouble()){ | ||
if(errFunc) | ||
errFunc(VALIDATE_ERROR::VALIDATE_WRONG_TYPE, child); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
template<> | ||
inline bool qJsonValidate<QJsonObject>(const QJsonObject& val, const QString& child, const std::function<void(VALIDATE_ERROR, const QString&)>& errFunc) | ||
{ | ||
if(!val.contains(child)){ | ||
if(errFunc) | ||
errFunc(VALIDATE_ERROR::VALIDATE_NO_CHILD, child); | ||
return false; | ||
} | ||
QJsonValue containedValue = val.value(child); | ||
if(!containedValue.isObject()){ | ||
if(errFunc) | ||
errFunc(VALIDATE_ERROR::VALIDATE_WRONG_TYPE, child); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
template<> | ||
inline bool qJsonValidate<QString>(const QJsonObject& val, const QString& child, const std::function<void(VALIDATE_ERROR, const QString&)>& errFunc) | ||
{ | ||
if(!val.contains(child)){ | ||
if(errFunc) | ||
errFunc(VALIDATE_ERROR::VALIDATE_NO_CHILD, child); | ||
return false; | ||
} | ||
QJsonValue containedValue = val.value(child); | ||
if(!containedValue.isString()){ | ||
if(errFunc) | ||
errFunc(VALIDATE_ERROR::VALIDATE_WRONG_TYPE, child); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
|
||
#endif // QJSONUTIL_H |