json_common.hpp 4.8 KB

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