|
26 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
27 | 27 | */ |
28 | 28 | #pragma once |
| 29 | + |
29 | 30 | #ifndef PICOJSON_USE_INT64 |
30 | 31 | #define PICOJSON_USE_INT64 |
31 | 32 | #define __STDC_FORMAT_MACROS 1 |
32 | 33 | #endif |
33 | 34 |
|
| 35 | +// If PICOJSON_USE_ORDERED_OBJECT is set, picojson uses ordered dict for objects. |
| 36 | +// When iterating the object, the order of keys is the order they appear in the json string. |
| 37 | +// This macro is set by default. |
| 38 | +#ifndef PICOJSON_USE_ORDERED_OBJECT |
| 39 | +#define PICOJSON_USE_ORDERED_OBJECT 1 |
| 40 | +#endif |
| 41 | + |
34 | 42 | #include <algorithm> |
| 43 | +#include <cassert> |
35 | 44 | #include <cstddef> |
36 | 45 | #include <cstdio> |
37 | 46 | #include <cstdlib> |
@@ -137,10 +146,17 @@ enum { INDENT_WIDTH = 2 }; |
137 | 146 |
|
138 | 147 | struct null {}; |
139 | 148 |
|
| 149 | +class object_with_ordered_keys; |
| 150 | + |
140 | 151 | class value { |
141 | 152 | public: |
142 | 153 | typedef std::vector<value> array; |
| 154 | +#ifdef PICOJSON_USE_ORDERED_OBJECT |
| 155 | + typedef object_with_ordered_keys object; |
| 156 | +#else |
143 | 157 | typedef std::unordered_map<std::string, value> object; |
| 158 | +#endif |
| 159 | + |
144 | 160 | union _storage { |
145 | 161 | bool boolean_; |
146 | 162 | double number_; |
@@ -220,6 +236,92 @@ class value { |
220 | 236 | void clear(); |
221 | 237 | }; |
222 | 238 |
|
| 239 | +// The ordered version of hashmap. It has the same interface as std::unordered_map, but provides |
| 240 | +// ordered_keys() to return the keys in the order they were inserted. |
| 241 | +class object_with_ordered_keys : private std::unordered_map<std::string, value> { |
| 242 | + public: |
| 243 | + using typename std::unordered_map<std::string, value>::value_type; |
| 244 | + using typename std::unordered_map<std::string, value>::iterator; |
| 245 | + using typename std::unordered_map<std::string, value>::const_iterator; |
| 246 | + |
| 247 | + object_with_ordered_keys() = default; |
| 248 | + object_with_ordered_keys(const object_with_ordered_keys&) = default; |
| 249 | + object_with_ordered_keys(object_with_ordered_keys&&) = default; |
| 250 | + object_with_ordered_keys(std::initializer_list<value_type> init) |
| 251 | + : std::unordered_map<std::string, value>(init) { |
| 252 | + for (const auto& pair : init) { |
| 253 | + ordered_keys_.push_back(pair.first); |
| 254 | + } |
| 255 | + } |
| 256 | + object_with_ordered_keys& operator=(const object_with_ordered_keys&) = default; |
| 257 | + object_with_ordered_keys& operator=(object_with_ordered_keys&&) = default; |
| 258 | + |
| 259 | + using std::unordered_map<std::string, value>::begin; |
| 260 | + using std::unordered_map<std::string, value>::end; |
| 261 | + using std::unordered_map<std::string, value>::cbegin; |
| 262 | + using std::unordered_map<std::string, value>::cend; |
| 263 | + using std::unordered_map<std::string, value>::empty; |
| 264 | + using std::unordered_map<std::string, value>::size; |
| 265 | + using std::unordered_map<std::string, value>::at; |
| 266 | + using std::unordered_map<std::string, value>::count; |
| 267 | + using std::unordered_map<std::string, value>::find; |
| 268 | + |
| 269 | + value& operator[](const std::string& key) { |
| 270 | + if (count(key) == 0) { |
| 271 | + ordered_keys_.push_back(key); |
| 272 | + } |
| 273 | + return std::unordered_map<std::string, value>::operator[](key); |
| 274 | + } |
| 275 | + |
| 276 | + void clear() { |
| 277 | + std::unordered_map<std::string, value>::clear(); |
| 278 | + ordered_keys_.clear(); |
| 279 | + } |
| 280 | + |
| 281 | + std::pair<iterator, bool> insert(const value_type& kv) { |
| 282 | + if (!count(kv.first)) { |
| 283 | + ordered_keys_.push_back(kv.first); |
| 284 | + } |
| 285 | + return std::unordered_map<std::string, value>::insert(kv); |
| 286 | + } |
| 287 | + |
| 288 | + template <class... Args> |
| 289 | + std::pair<iterator, bool> emplace(Args&&... args) { |
| 290 | + return insert(value_type(std::forward<Args>(args)...)); |
| 291 | + } |
| 292 | + |
| 293 | + iterator erase(const_iterator it) { |
| 294 | + ordered_keys_.erase(std::find(ordered_keys_.begin(), ordered_keys_.end(), it->first)); |
| 295 | + return std::unordered_map<std::string, value>::erase(it); |
| 296 | + } |
| 297 | + |
| 298 | + iterator erase(iterator it) { |
| 299 | + ordered_keys_.erase(std::find(ordered_keys_.begin(), ordered_keys_.end(), it->first)); |
| 300 | + return std::unordered_map<std::string, value>::erase(it); |
| 301 | + } |
| 302 | + |
| 303 | + size_t erase(const std::string& key) { |
| 304 | + if (std::unordered_map<std::string, value>::erase(key)) { |
| 305 | + ordered_keys_.erase(std::find(ordered_keys_.begin(), ordered_keys_.end(), key)); |
| 306 | + return 1; |
| 307 | + } else { |
| 308 | + return 0; |
| 309 | + } |
| 310 | + } |
| 311 | + |
| 312 | + const std::vector<std::string>& ordered_keys() const { return ordered_keys_; } |
| 313 | + |
| 314 | + friend bool operator==(const object_with_ordered_keys& lhs, const object_with_ordered_keys& rhs); |
| 315 | + |
| 316 | + private: |
| 317 | + std::vector<std::string> ordered_keys_; |
| 318 | +}; |
| 319 | + |
| 320 | +inline bool operator==(const object_with_ordered_keys& lhs, const object_with_ordered_keys& rhs) { |
| 321 | + return static_cast<const std::unordered_map<std::string, value>&>(lhs) == |
| 322 | + static_cast<const std::unordered_map<std::string, value>&>(rhs); |
| 323 | +} |
| 324 | + |
223 | 325 | typedef value::array array; |
224 | 326 | typedef value::object object; |
225 | 327 |
|
|
0 commit comments