| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- //
- // json_common.h
- // json
- //
- // Created by Sam Jaffe on 4/22/16.
- //
- #pragma once
- #include "../variant/variant.hpp"
- #include <cstdlib>
- #include <iostream>
- #include <map>
- #include <stdexcept>
- #include <string>
- #include <vector>
- 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;
-
- using numeric_jt = variant<int_jt, uint_jt, double_jt>;
-
- class value;
- class malformed_json_exception :
- public std::domain_error {
- using std::domain_error::domain_error;
- };
- }
- namespace json {
- namespace helper {
- const char get_next_element(char const*& data);
-
- /**
- * @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 <typename T>
- void parse_string(T& json, char const * & data) {
- json = parse_string(data);
- }
-
- template <typename T>
- void parse_double(T& json, char const * & data) {
- json = atof(data);
- }
-
- template <typename T>
- void parse_integer(T& json, char const * & data) {
- json = atoi(data);
- }
-
- }
-
- namespace parser {
- template <typename T>
- void parse(T& json, std::string const& str) {
- parse(json, str.c_str());
- }
-
- template <typename T>
- void parse(T& json, std::istream & in) {
- in.seekg(0, std::ios_base::end);
- size_t end = in.tellg();
- char data[end];
- in.seekg(0);
- in.read(data, end);
- parse(json, data);
- }
- }
- }
|