| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- //
- // json.hpp
- // json
- //
- // Created by Sam Jaffe on 1/30/16.
- // Copyright © 2016 Sam Jaffe. All rights reserved.
- //
- #pragma once
- #include "json_common.hpp"
- #include "variant/variant.hpp"
- #include <map>
- #include <utility>
- #include <vector>
- #define JSON_TYPE_LIST \
- X(object) \
- X(array) \
- X(string) \
- X(double) \
- X(int) \
- X(uint) \
- X(bool)
- namespace json {
- class value;
- template <> void helper::parse_numeric(value & json, char const *& data);
- namespace parser {
- void parse(value &, char const *);
- }
- class value {
- public:
- using object_jt = std::map<std::string, value>;
- using array_jt = std::vector<value>;
- using string_jt = json::string_jt;
- using double_jt = json::double_jt;
- using int_jt = json::int_jt;
- using uint_jt = json::uint_jt;
- using bool_jt = json::bool_jt;
- private:
- static const value null_value;
- using data_t = variant<object_jt, array_jt, string_jt, double_jt, int_jt,
- uint_jt, bool_jt>;
- data_t data;
- public:
- #define X(type) \
- bool is_##type() const { return data.is<type##_jt>(); }
- JSON_TYPE_LIST
- #undef X
- bool is_null() const { return !data.valid(); }
- value() = default;
- value(value const &) = default;
- value(value &&) = default;
- value & operator=(value const &) = default;
- value & operator=(value &&) = default;
- #define X(type) \
- value(type##_jt val) { data.set<type##_jt>(std::move(val)); }
- JSON_TYPE_LIST
- #undef X
- #define X(type) \
- value & operator=(type##_jt val) { \
- data.set<type##_jt>(std::move(val)); \
- return *this; \
- }
- JSON_TYPE_LIST
- #undef X
- void parse(char const * data);
- void parse(std::string const & str);
- void parse(std::istream & in);
- void clear() { data = data_t(); }
- value & operator[](const size_t idx);
- value const & operator[](const size_t idx) const;
- value & operator[](std::string const & key);
- value const & operator[](std::string const & key) const;
- string_jt const & as_string() const;
- double_jt as_double() const;
- int_jt as_int() const;
- uint_jt as_uint() const;
- bool_jt as_bool() const;
- operator string_jt const &() const { return as_string(); }
- operator double_jt() const { return as_double(); }
- operator int_jt() const { return as_int(); }
- operator uint_jt() const { return as_uint(); }
- operator bool_jt() const { return as_bool(); }
- };
- #undef JSON_TYPE_LIST
- namespace parser {
- void parse(json::value &, char const *);
- void parse(json::value & json, std::istream & in);
- }
- }
|