// // json_common.h // json // // Created by Sam Jaffe on 4/22/16. // #pragma once #include #include #include #include namespace json { using string_jt = std::string; using double_jt = double; using int_jt = int32_t; using uint_jt = uint32_t; using bool_jt = bool; class value; class malformed_json_exception : public std::domain_error { using std::domain_error::domain_error; }; class unterminated_json_exception : public malformed_json_exception { using malformed_json_exception::malformed_json_exception; }; namespace { const constexpr json::int_jt INT_JT_MAX = std::numeric_limits::max(); const constexpr json::int_jt INT_JT_MAX_LAST_DIGIT = (INT_JT_MAX % 10); const constexpr json::int_jt INT_JT_MIN = std::numeric_limits::min(); const constexpr json::uint_jt INT_JT_OVER = json::uint_jt(INT_JT_MAX) + 1; const constexpr json::uint_jt UINT_JT_MAX = json::uint_jt(0) - 1; // const constexpr json::uint_jt UINT_JT_MIN = 0; } } namespace json { namespace parser { enum options { allow_all = 0x00, disable_unknown_keys = 0x01, disable_concatenated_json_bodies = 0x02, disable_all = 0xFF, }; } } namespace json { namespace helper { const char get_next_element(char const*& data); enum numeric_state { DOUBLE, INTEGER }; struct numeric_token_info { numeric_token_info(char const * start); numeric_state parse_numeric(); uint_jt val; char const * it; char const * end; bool is_double; bool is_negative; }; numeric_token_info get_numeric_token_info(char const * it); /** * @throws json::malformed_json_exception */ void advance_to_boundary(char const endtok, char const *& data); /** * @throws json::malformed_json_exception */ std::string parse_string(char const * & data); template void parse_string(T& json, char const * & data) { json = parse_string(data); } template void parse_double(T& json, char const * & data) { json = atof(data); while (isdigit(*data)) { ++data; } if (*data == '.' || *data == 'e') { ++data; } while (isdigit(*data)) { ++data; } } // template // void parse_integer(T& json, char const * & data) { // json = atoi(data); // while (isdigit(*data)) { ++data; } // } template void parse_numeric(J & json, char const * & data) { numeric_token_info info = data; if ( info.is_double ) { helper::parse_double(json, data); } else if ( info.parse_numeric() == DOUBLE ) { helper::parse_double(json, data); } else { uint_jt const val = info.val; if (info.is_negative) { if (val == INT_JT_OVER) { json = INT_JT_MIN; } else { json = -int_jt(val); } } else if (val <= uint_jt(INT_JT_MAX)) { json = int_jt(val); } else { json = val; } data = info.it; } } } }