9
3

JsonParser.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "JsonParser.h"
  2. JsonParser::JsonParser(const std::string &filename) {
  3. is_loaded = false;
  4. try {
  5. boost::property_tree::read_json(filename, pt);
  6. parseTree(pt, "");
  7. is_loaded = true;
  8. } catch (const boost::property_tree::json_parser_error &e) {
  9. std::cerr << "Error reading JSON file: " << e.what() << std::endl;
  10. }
  11. }
  12. bool JsonParser::convertStringToUnsignedChar(const std::string& str, unsigned char& result) {
  13. unsigned long ul;
  14. try {
  15. ul = std::stoul(str);
  16. } catch (const std::invalid_argument&) {
  17. return false; // Not a valid number
  18. } catch (const std::out_of_range&) {
  19. return false; // Number is too large for unsigned long
  20. }
  21. if (ul > std::numeric_limits<unsigned char>::max()) {
  22. return false; // Number is too large for unsigned short
  23. }
  24. result = static_cast<unsigned char>(ul);
  25. return true;
  26. }
  27. bool JsonParser::convertStringToUnsignedShort(const std::string& str, unsigned short& result) {
  28. unsigned long ul;
  29. try {
  30. ul = std::stoul(str);
  31. } catch (const std::invalid_argument&) {
  32. return false; // Not a valid number
  33. } catch (const std::out_of_range&) {
  34. return false; // Number is too large for unsigned long
  35. }
  36. if (ul > std::numeric_limits<unsigned short>::max()) {
  37. return false; // Number is too large for unsigned short
  38. }
  39. result = static_cast<unsigned short>(ul);
  40. return true;
  41. }
  42. bool JsonParser::convertStringToUnsignedInt(const std::string& str, unsigned int& result) {
  43. unsigned long ul;
  44. try {
  45. ul = std::stoul(str);
  46. } catch (const std::invalid_argument&) {
  47. return false; // Not a valid number
  48. } catch (const std::out_of_range&) {
  49. return false; // Number is too large for unsigned long
  50. }
  51. if (ul > std::numeric_limits<unsigned int>::max()) {
  52. return false; // Number is too large for unsigned short
  53. }
  54. result = static_cast<unsigned int>(ul);
  55. return true;
  56. }
  57. bool JsonParser::convertStringToUnsignedLong(const std::string& str, unsigned long& result) {
  58. unsigned long ul;
  59. try {
  60. ul = std::stoul(str);
  61. } catch (const std::invalid_argument&) {
  62. return false; // Not a valid number
  63. } catch (const std::out_of_range&) {
  64. return false; // Number is too large for unsigned long
  65. }
  66. if (ul > std::numeric_limits<unsigned long>::max()) {
  67. return false; // Number is too large for unsigned short
  68. }
  69. result = ul;
  70. return true;
  71. }
  72. void JsonParser::parseTree(const boost::property_tree::ptree &tree, const std::string &path) {
  73. for (const auto &node : tree) {
  74. std::string currentPath = path.empty() ? node.first : path + "." + node.first;
  75. if (node.second.empty()) {
  76. std::string name = currentPath;
  77. boost::algorithm::to_lower(name);
  78. values[name] = node.second.get_value<std::string>();
  79. } else {
  80. parseTree(node.second, currentPath);
  81. }
  82. }
  83. }