JSON.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "JSON.h"
  2. #include "WallpaperEngine/Data/Parsers/UserSettingParser.h"
  3. #include <cctype>
  4. using namespace WallpaperEngine::Data::JSON;
  5. using namespace WallpaperEngine::Data::Model;
  6. using namespace WallpaperEngine::Data::Parsers;
  7. UserSettingUniquePtr JsonExtensions::user (const std::string& key, const Properties& properties) const {
  8. const auto value = this->require (key, "User setting without default value must be present");
  9. return UserSettingParser::parse (value, properties);
  10. }
  11. UserSettingUniquePtr JsonExtensions::color (const std::string& key, const Properties& properties) const {
  12. const auto value = this->require (key, "User setting without default value must be present");
  13. return UserSettingParser::parse (value, properties, true);
  14. }
  15. JSON JsonExtensions::parseAsset (const std::string& content) {
  16. std::string cleaned;
  17. cleaned.reserve (content.size ());
  18. const auto length = content.size ();
  19. size_t i = 0;
  20. while (i < length) {
  21. const char c = content[i];
  22. if (c == '"') {
  23. const auto start = i++;
  24. while (i < length && content[i] != '"') {
  25. i += content[i] == '\\' ? 2 : 1;
  26. }
  27. i = std::min (i + 1, length);
  28. cleaned.append (content, start, i - start);
  29. continue;
  30. }
  31. if (c == '/' && i + 1 < length && (content[i + 1] == '/' || content[i + 1] == '*')) {
  32. const auto start = i;
  33. const bool block = content[i + 1] == '*';
  34. i += 2;
  35. if (block) {
  36. const auto end = content.find ("*/", i);
  37. i = end == std::string::npos ? length : end + 2;
  38. } else {
  39. while (i < length && content[i] != '\n') {
  40. i++;
  41. }
  42. }
  43. cleaned.append (content, start, i - start);
  44. continue;
  45. }
  46. if (c == ',') {
  47. auto next = i + 1;
  48. while (next < length && std::isspace (static_cast<unsigned char> (content[next]))) {
  49. next++;
  50. }
  51. if (next < length && (content[next] == ']' || content[next] == '}')) {
  52. i++;
  53. continue;
  54. }
  55. }
  56. cleaned.push_back (c);
  57. i++;
  58. }
  59. return JSON::parse (cleaned, nullptr, true, true);
  60. }