json_common.hpp 4.8 KB

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