json_common.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // json_common.h
  3. // json
  4. //
  5. // Created by Sam Jaffe on 4/22/16.
  6. //
  7. #pragma once
  8. #include <cstdint>
  9. #include <cstdlib>
  10. #include <limits>
  11. #include <stdexcept>
  12. #include <string>
  13. #include <strings.h>
  14. #include <errno.h>
  15. namespace json {
  16. using string_jt = std::string;
  17. using double_jt = double;
  18. using int_jt = int32_t;
  19. using uint_jt = uint32_t;
  20. using bool_jt = bool;
  21. class value;
  22. class malformed_json_exception :
  23. public std::domain_error {
  24. using std::domain_error::domain_error;
  25. };
  26. class unterminated_json_exception :
  27. public malformed_json_exception {
  28. using malformed_json_exception::malformed_json_exception;
  29. };
  30. class json_numeric_exception :
  31. public std::domain_error {
  32. using std::domain_error::domain_error;
  33. };
  34. class json_numeric_width_exception :
  35. public json_numeric_exception {
  36. using json_numeric_exception::json_numeric_exception;
  37. };
  38. template <typename T = json::int_jt>
  39. struct numeric_limits {
  40. static constexpr const T max{std::numeric_limits<T>::max()};
  41. static constexpr const T min{std::numeric_limits<T>::min()};
  42. static constexpr const uint_jt over{uint_jt{max}+1};
  43. };
  44. namespace {
  45. const constexpr json::int_jt INT_JT_MAX = std::numeric_limits<json::int_jt>::max();
  46. const constexpr json::int_jt INT_JT_MAX_LAST_DIGIT = (INT_JT_MAX % 10);
  47. const constexpr json::int_jt INT_JT_MIN = std::numeric_limits<json::int_jt>::min();
  48. const constexpr json::uint_jt INT_JT_OVER = json::uint_jt(INT_JT_MAX) + 1;
  49. const constexpr json::uint_jt UINT_JT_MAX = json::uint_jt(0) - 1;
  50. // const constexpr json::uint_jt UINT_JT_MIN = 0;
  51. }
  52. }
  53. namespace json { namespace parser {
  54. enum options {
  55. allow_all = 0x00,
  56. disable_unknown_keys = 0x01,
  57. disable_missing_keys = 0x02,
  58. disable_concatenated_json_bodies = 0x04,
  59. disable_all = 0xFF,
  60. };
  61. } }
  62. namespace json { namespace helper {
  63. const char get_next_element(char const*& data);
  64. enum numeric_state {
  65. DOUBLE, INTEGER
  66. };
  67. struct numeric_token_info {
  68. enum parse_state { decimal, octal, hexadecimal };
  69. numeric_token_info(char const * start);
  70. numeric_state parse_numeric();
  71. uint_jt val;
  72. parse_state base;
  73. char const * it;
  74. char const * end;
  75. bool is_double;
  76. bool is_negative;
  77. };
  78. numeric_token_info get_numeric_token_info(char const * it);
  79. /**
  80. * @throws json::malformed_json_exception
  81. */
  82. void advance_to_boundary(char const endtok, char const *& data);
  83. /**
  84. * @throws json::malformed_json_exception
  85. */
  86. std::string parse_string(char const * & data);
  87. std::string replace_all(std::string str, std::string const & from, std::string const & to);
  88. template <typename T>
  89. void parse_string(T& json, char const * & data) {
  90. json::helper::get_next_element(data);
  91. json = parse_string(data);
  92. }
  93. template <typename T>
  94. T parse_double_impl(char const * begin, char const * & end) {
  95. return std::strtod(begin, const_cast<char**>(&end));
  96. }
  97. template <>
  98. inline float parse_double_impl<float>(char const * begin, char const * & end) {
  99. return std::strtof(begin, const_cast<char**>(&end));
  100. }
  101. template <>
  102. inline long double parse_double_impl<long double>(char const * begin, char const * & end) {
  103. return std::strtold(begin, const_cast<char**>(&end));
  104. }
  105. template <typename T>
  106. void parse_double(T& json, char const * & data) {
  107. json::helper::get_next_element(data);
  108. char const * begin = data;
  109. errno = 0;
  110. T tmp = parse_double_impl<T>(begin, data);
  111. if (errno != 0) {
  112. throw json::json_numeric_width_exception("Number is out-of-range for floating-point type");
  113. } else if ( begin == data ) {
  114. throw json::json_numeric_exception("Expected numeric data");
  115. }
  116. errno = 0;
  117. json = std::move(tmp);
  118. }
  119. template <typename J>
  120. void parse_numeric(J & json, char const * & data) {
  121. json::helper::get_next_element(data);
  122. numeric_token_info info = data;
  123. if ( info.is_negative && !std::is_signed<J>::value ) {
  124. throw json_numeric_exception("Expected signed integer");
  125. } else if ( info.is_double || info.parse_numeric() == DOUBLE ) {
  126. throw json_numeric_exception("Expected integer, got double");
  127. } else if (info.base == numeric_token_info::decimal &&
  128. (fls(static_cast<int_jt>(info.val)) > std::numeric_limits<J>::digits + info.is_negative
  129. || info.val > json::numeric_limits<J>::over)) {
  130. throw json_numeric_width_exception("Integer width too small for parsed value");
  131. } else if (info.is_negative) {
  132. if (info.val == json::numeric_limits<J>::over) {
  133. json = json::numeric_limits<J>::min;
  134. } else {
  135. json = static_cast<J>(-int_jt(info.val));
  136. }
  137. } else if (info.val <= uint_jt(INT_JT_MAX)) {
  138. json = static_cast<J>(int_jt(info.val));
  139. } else if (std::is_signed<J>::value &&
  140. info.base == numeric_token_info::decimal) {
  141. throw json_numeric_exception("Expected unsigned integer");
  142. } else {
  143. json = static_cast<J>(info.val);
  144. }
  145. data = info.it;
  146. }
  147. } }