| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- //
- // json.hpp
- // json
- //
- // Created by Sam Jaffe on 1/30/16.
- // Copyright © 2016 Sam Jaffe. All rights reserved.
- //
- #pragma once
- #include "variant/variant.hpp"
- #include "json_common.hpp"
- #include <map>
- #include <vector>
- #include <utility>
- #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);
- }
- }
|