You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

116 lines
3.3 KiB

  1. /*
  2. * This file is part of PowerDNS or dnsdist.
  3. * Copyright -- PowerDNS.COM B.V. and its contributors
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * In addition, for the avoidance of any doubt, permission is granted to
  10. * link this program with OpenSSL and to (re)distribute the binaries
  11. * produced as the result of such linking.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include "json.hh"
  26. #include "namespaces.hh"
  27. #include "misc.hh"
  28. using json11::Json;
  29. int intFromJson(const Json container, const std::string& key)
  30. {
  31. auto val = container[key];
  32. if (val.is_number()) {
  33. return val.int_value();
  34. } else if (val.is_string()) {
  35. return std::stoi(val.string_value());
  36. } else {
  37. throw JsonException("Key '" + string(key) + "' not an Integer or not present");
  38. }
  39. }
  40. int intFromJson(const Json container, const std::string& key, const int default_value)
  41. {
  42. auto val = container[key];
  43. if (val.is_number()) {
  44. return val.int_value();
  45. } else if (val.is_string()) {
  46. try {
  47. return std::stoi(val.string_value());
  48. } catch (std::out_of_range&) {
  49. throw JsonException("Value for key '" + string(key) + "' is out of range");
  50. }
  51. } else {
  52. // TODO: check if value really isn't present
  53. return default_value;
  54. }
  55. }
  56. double doubleFromJson(const Json container, const std::string& key)
  57. {
  58. auto val = container[key];
  59. if (val.is_number()) {
  60. return val.number_value();
  61. } else if (val.is_string()) {
  62. try {
  63. return std::stod(val.string_value());
  64. } catch (std::out_of_range&) {
  65. throw JsonException("Value for key '" + string(key) + "' is out of range");
  66. }
  67. } else {
  68. throw JsonException("Key '" + string(key) + "' not an Integer or not present");
  69. }
  70. }
  71. double doubleFromJson(const Json container, const std::string& key, const double default_value)
  72. {
  73. auto val = container[key];
  74. if (val.is_number()) {
  75. return val.number_value();
  76. } else if (val.is_string()) {
  77. return std::stod(val.string_value());
  78. } else {
  79. // TODO: check if value really isn't present
  80. return default_value;
  81. }
  82. }
  83. string stringFromJson(const Json container, const std::string &key)
  84. {
  85. const Json val = container[key];
  86. if (val.is_string()) {
  87. return val.string_value();
  88. } else {
  89. throw JsonException("Key '" + string(key) + "' not present or not a String");
  90. }
  91. }
  92. bool boolFromJson(const Json container, const std::string& key)
  93. {
  94. auto val = container[key];
  95. if (val.is_bool()) {
  96. return val.bool_value();
  97. }
  98. throw JsonException("Key '" + string(key) + "' not present or not a Bool");
  99. }
  100. bool boolFromJson(const Json container, const std::string& key, const bool default_value)
  101. {
  102. auto val = container[key];
  103. if (val.is_bool()) {
  104. return val.bool_value();
  105. }
  106. return default_value;
  107. }