JsonUtils.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //project include
  2. #include <utils/JsonUtils.h>
  3. // util includes
  4. #include <utils/jsonschema/QJsonSchemaChecker.h>
  5. //qt includes
  6. #include <QRegularExpression>
  7. #include <QJsonObject>
  8. #include <QJsonParseError>
  9. namespace JsonUtils {
  10. bool readFile(const QString& path, QJsonObject& obj, Logger* log, bool ignError)
  11. {
  12. QString data;
  13. if(!FileUtils::readFile(path, data, log, ignError))
  14. return false;
  15. if(!parse(path, data, obj, log))
  16. return false;
  17. return true;
  18. }
  19. bool readSchema(const QString& path, QJsonObject& obj, Logger* log)
  20. {
  21. QJsonObject schema;
  22. if(!readFile(path, schema, log))
  23. return false;
  24. if(!resolveRefs(schema, obj, log))
  25. return false;
  26. return true;
  27. }
  28. bool parse(const QString& path, const QString& data, QJsonObject& obj, Logger* log)
  29. {
  30. QJsonDocument doc;
  31. if(!parse(path, data, doc, log))
  32. return false;
  33. obj = doc.object();
  34. return true;
  35. }
  36. bool parse(const QString& path, const QString& data, QJsonArray& arr, Logger* log)
  37. {
  38. QJsonDocument doc;
  39. if(!parse(path, data, doc, log))
  40. return false;
  41. arr = doc.array();
  42. return true;
  43. }
  44. bool parse(const QString& path, const QString& data, QJsonDocument& doc, Logger* log)
  45. {
  46. //remove Comments in data
  47. QString cleanData = data;
  48. QJsonParseError error;
  49. doc = QJsonDocument::fromJson(cleanData.toUtf8(), &error);
  50. if (error.error != QJsonParseError::NoError)
  51. {
  52. // report to the user the failure and their locations in the document.
  53. int errorLine(0), errorColumn(0);
  54. for( int i=0, count=qMin( error.offset,cleanData.size()); i<count; ++i )
  55. {
  56. ++errorColumn;
  57. if(data.at(i) == '\n' )
  58. {
  59. errorColumn = 0;
  60. ++errorLine;
  61. }
  62. }
  63. Error(log, "Failed to parse json data from %s: Error: %s at Line: %i, Column: %i, Data: '%s'", QSTRING_CSTR(path), QSTRING_CSTR(error.errorString()), errorLine, errorColumn, QSTRING_CSTR(data));
  64. return false;
  65. }
  66. return true;
  67. }
  68. bool validate(const QString& file, const QJsonObject& json, const QString& schemaPath, Logger* log)
  69. {
  70. // get the schema data
  71. QJsonObject schema;
  72. if(!readFile(schemaPath, schema, log))
  73. return false;
  74. if(!validate(file, json, schema, log))
  75. return false;
  76. return true;
  77. }
  78. bool validate(const QString& file, const QJsonObject& json, const QJsonObject& schema, Logger* log)
  79. {
  80. QJsonSchemaChecker schemaChecker;
  81. schemaChecker.setSchema(schema);
  82. if (!schemaChecker.validate(json).first)
  83. {
  84. const QStringList & errors = schemaChecker.getMessages();
  85. for (auto & error : errors)
  86. {
  87. Error(log, "While validating schema against json data of '%s':%s", QSTRING_CSTR(file), QSTRING_CSTR(error));
  88. }
  89. return false;
  90. }
  91. return true;
  92. }
  93. bool write(const QString& filename, const QJsonObject& json, Logger* log)
  94. {
  95. QJsonDocument doc;
  96. doc.setObject(json);
  97. QByteArray data = doc.toJson(QJsonDocument::Indented);
  98. if(!FileUtils::writeFile(filename, data, log))
  99. return false;
  100. return true;
  101. }
  102. bool resolveRefs(const QJsonObject& schema, QJsonObject& obj, Logger* log)
  103. {
  104. for (QJsonObject::const_iterator i = schema.begin(); i != schema.end(); ++i)
  105. {
  106. QString attribute = i.key();
  107. const QJsonValue & attributeValue = *i;
  108. if (attribute == "$ref" && attributeValue.isString())
  109. {
  110. if(!readSchema(":/" + attributeValue.toString(), obj, log))
  111. {
  112. Error(log,"Error while getting schema ref: %s",QSTRING_CSTR(QString(":/" + attributeValue.toString())));
  113. return false;
  114. }
  115. }
  116. else if (attributeValue.isObject())
  117. obj.insert(attribute, resolveRefs(attributeValue.toObject(), obj, log));
  118. else
  119. {
  120. obj.insert(attribute, attributeValue);
  121. }
  122. }
  123. return true;
  124. }
  125. };