| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | #ifndef JSON_HH |
| 25 | #define JSON_HH |
| 26 | |
| 27 | #include <util/types.hh> |
| 28 | #include <unordered_map> |
| 29 | #include <string_view> |
| 30 | #include <variant> |
| 31 | #include <optional> |
| 32 | #include <memory> |
| 33 | |
| 34 | namespace json { |
| 35 | enum class Error { |
| 36 | None, |
| 37 | SyntaxError, |
| 38 | UnexpectedEnd, |
| 39 | InvalidValue, |
| 40 | InvalidNumber, |
| 41 | UnterminatedString, |
| 42 | }; |
| 43 | |
| 44 | struct SerializeOptions { |
| 45 | bool pretty = false; |
| 46 | size_t indent = 2; |
| 47 | }; |
| 48 | |
| 49 | struct ParseResult; |
| 50 | struct StringHash; |
| 51 | class Object; |
| 52 | class Value; |
| 53 | |
| 54 | struct ParseResult { |
| 55 | std::unique_ptr<Value> value; |
| 56 | Error error = Error::None; |
| 57 | size_t errPos = 0; |
| 58 | bool ok() const { return error == Error::None; } |
| 59 | }; |
| 60 | |
| 61 | struct StringHash { |
| 62 | using is_transparent = void; |
| 63 | size_t operator()(std::string_view sv) const { |
| 64 | return std::hash<std::string_view>{}(sv); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | class Object { |
| 69 | public: |
| 70 | Object() = default; |
| 71 | |
| 72 | Object(const Object &) = delete; |
| 73 | Object &operator=(const Object &) = delete; |
| 74 | |
| 75 | Object(Object &&) noexcept = default; |
| 76 | Object &operator=(Object &&) noexcept = default; |
| 77 | |
| 78 | Value &operator[](std::string_view key); |
| 79 | const Value &operator[](std::string_view key) const; |
| 80 | |
| 81 | void insert(string key, Value value); |
| 82 | Value *get(std::string_view key); |
| 83 | const Value *get(std::string_view key) const; |
| 84 | bool contains(std::string_view key) const; |
| 85 | |
| 86 | public: |
| 87 | bool empty() const { return m_Data.empty(); } |
| 88 | size_t size() const { return m_Data.size(); } |
| 89 | auto begin() { return m_Data.begin(); } |
| 90 | auto end() { return m_Data.end(); } |
| 91 | auto begin() const { return m_Data.begin(); } |
| 92 | auto end() const { return m_Data.end(); } |
| 93 | |
| 94 | private: |
| 95 | using Pair = std::pair<string, std::unique_ptr<Value>>; |
| 96 | vector<Pair> m_Data; |
| 97 | std::unordered_map<string, size_t, StringHash, std::equal_to<>> m_Index; |
| 98 | }; |
| 99 | |
| 100 | class Value { |
| 101 | public: |
| 102 | using Array = vector<Value>; |
| 103 | using Object = json::Object; |
| 104 | |
| 105 | using Variant = std::variant< |
| 106 | std::monostate, |
| 107 | bool, |
| 108 | f64, |
| 109 | string, |
| 110 | Array, |
| 111 | Object |
| 112 | >; |
| 113 | |
| 114 | |
| 115 | Value &operator[](size_t index) { |
| 116 | if (!is_array()) as_array() = Array{}; |
| 117 | |
| 118 | auto &arr = as_array(); |
| 119 | if (index >= arr.size()) arr.resize(index + 1); |
| 120 | return arr[index]; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | Value &operator[](std::string_view key) { |
| 125 | if (!is_object()) as_object() = Object{}; |
| 126 | |
| 127 | auto *val = as_object().get(key); |
| 128 | if (!val) { |
| 129 | as_object().insert(string(key), Value{}); |
| 130 | val = as_object().get(key); |
| 131 | } |
| 132 | return *val; |
| 133 | } |
| 134 | |
| 135 | public: |
| 136 | Value() = default; |
| 137 | Value(std::nullptr_t) : m_Value(std::monostate{}) {} |
| 138 | Value(bool b) : m_Value(b) {} |
| 139 | Value(f64 n) : m_Value(n) {} |
| 140 | Value(f32 n) : m_Value(n) {} |
| 141 | Value(i64 n) : m_Value(static_cast<f64>(n)) {} |
| 142 | Value(i32 n) : m_Value(static_cast<f64>(n)) {} |
| 143 | Value(u64 n) : m_Value(static_cast<f64>(n)) {} |
| 144 | Value(u32 n) : m_Value(static_cast<f64>(n)) {} |
| 145 | Value(string s) : m_Value(std::move(s)) {} |
| 146 | Value(cstr s) : m_Value(string(s)) {} |
| 147 | Value(Array &&arr) : m_Value(std::move(arr)) {} |
| 148 | Value(Object &&obj) : m_Value(std::move(obj)) {} |
| 149 | |
| 150 | public: |
| 151 | bool is_null() const { return std::holds_alternative<std::monostate>(m_Value); } |
| 152 | bool is_bool() const { return std::holds_alternative<bool>(m_Value); } |
| 153 | bool is_number() const { return std::holds_alternative<f64>(m_Value); } |
| 154 | bool is_string() const { return std::holds_alternative<string>(m_Value); } |
| 155 | bool is_array() const { return std::holds_alternative<Array>(m_Value); } |
| 156 | bool is_object() const { return std::holds_alternative<Object>(m_Value); } |
| 157 | |
| 158 | public: |
| 159 | bool as_bool() const { return std::get<bool>(m_Value); } |
| 160 | f64 as_number() const { return std::get<f64>(m_Value); } |
| 161 | string &as_string() { return std::get<string>(m_Value); } |
| 162 | const string &as_string() const { return std::get<string>(m_Value); } |
| 163 | Array &as_array() { return std::get<Array>(m_Value); } |
| 164 | const Array &as_array() const { return std::get<Array>(m_Value); } |
| 165 | Object &as_object() { return std::get<Object>(m_Value); } |
| 166 | const Object &as_object() const { return std::get<Object>(m_Value); } |
| 167 | |
| 168 | public: |
| 169 | std::optional<bool> get_bool() const; |
| 170 | std::optional<f64> get_number() const; |
| 171 | std::optional<string> get_string() const; |
| 172 | |
| 173 | public: |
| 174 | static ParseResult parse(std::string_view jsonText); |
| 175 | |
| 176 | public: |
| 177 | string serialize(const SerializeOptions &opts = {}) const; |
| 178 | |
| 179 | private: |
| 180 | static string make_indent(size_t depth, size_t width); |
| 181 | |
| 182 | private: |
| 183 | string serialize_impl(const SerializeOptions &opts, size_t depth) const; |
| 184 | static string serialize_string(const string &s); |
| 185 | |
| 186 | private: |
| 187 | Variant m_Value; |
| 188 | }; |
| 189 | |
| 190 | class Parser { |
| 191 | public: |
| 192 | Parser(std::string_view text) : m_Text(text), m_Pos(0) {} |
| 193 | |
| 194 | ParseResult parse(); |
| 195 | |
| 196 | private: |
| 197 | std::string_view m_Text; |
| 198 | size_t m_Pos; |
| 199 | |
| 200 | ParseResult make_error(Error err) const; |
| 201 | void skip_whitespace(); |
| 202 | char peek() const; |
| 203 | char consume(); |
| 204 | bool expect(char c); |
| 205 | |
| 206 | ParseResult parse_value(); |
| 207 | ParseResult parse_object(); |
| 208 | ParseResult parse_array(); |
| 209 | ParseResult parse_string(); |
| 210 | std::optional<f64> parse_number(); |
| 211 | }; |
| 212 | } |
| 213 | |
| 214 | inline std::ostream &operator<<(std::ostream &os, const json::Value &v) { |
| 215 | os << v.serialize(); |
| 216 | return os; |
| 217 | } |
| 218 | |
| 219 | inline json::Value &json::Object::operator[](std::string_view key) { |
| 220 | return *get(key); |
| 221 | } |
| 222 | |
| 223 | inline const json::Value &json::Object::operator[](std::string_view key) const { |
| 224 | return *get(key); |
| 225 | } |
| 226 | |
| 227 | #endif |