| 44 | // NOLINTEND(google-explicit-constructor) |
| 45 | |
| 46 | class JSON |
| 47 | { |
| 48 | private: |
| 49 | using Pos = const char *; |
| 50 | Pos ptr_begin{}; |
| 51 | Pos ptr_end{}; |
| 52 | unsigned level{}; |
| 53 | |
| 54 | public: |
| 55 | JSON(Pos ptr_begin_, Pos ptr_end_, unsigned level_ = 0) : ptr_begin(ptr_begin_), ptr_end(ptr_end_), level(level_) |
| 56 | { |
| 57 | checkInit(); |
| 58 | } |
| 59 | |
| 60 | explicit JSON(std::string_view s) : ptr_begin(s.data()), ptr_end(s.data() + s.size()), level(0) |
| 61 | { |
| 62 | checkInit(); |
| 63 | } |
| 64 | |
| 65 | JSON(const JSON & rhs) |
| 66 | { |
| 67 | *this = rhs; |
| 68 | } |
| 69 | |
| 70 | JSON & operator=(const JSON & rhs) = default; |
| 71 | |
| 72 | const char * data() const { return ptr_begin; } |
| 73 | const char * dataEnd() const { return ptr_end; } |
| 74 | |
| 75 | enum ElementType : uint8_t |
| 76 | { |
| 77 | TYPE_OBJECT, |
| 78 | TYPE_ARRAY, |
| 79 | TYPE_NUMBER, |
| 80 | TYPE_STRING, |
| 81 | TYPE_BOOL, |
| 82 | TYPE_NULL, |
| 83 | TYPE_NAME_VALUE_PAIR, |
| 84 | TYPE_NOTYPE, |
| 85 | }; |
| 86 | |
| 87 | ElementType getType() const; |
| 88 | |
| 89 | bool isObject() const { return getType() == TYPE_OBJECT; } |
| 90 | bool isArray() const { return getType() == TYPE_ARRAY; } |
| 91 | bool isNumber() const { return getType() == TYPE_NUMBER; } |
| 92 | bool isString() const { return getType() == TYPE_STRING; } |
| 93 | bool isBool() const { return getType() == TYPE_BOOL; } |
| 94 | bool isNull() const { return getType() == TYPE_NULL; } |
| 95 | bool isNameValuePair() const { return getType() == TYPE_NAME_VALUE_PAIR; } |
| 96 | |
| 97 | /// Number of elements in array or object; if element is not array or object, then exception. |
| 98 | size_t size() const; |
| 99 | |
| 100 | /// Whether array or object is empty; if element is not array or object, then exception. |
| 101 | bool empty() const; |
| 102 | |
| 103 | /// Get array element by index; if element is not array, then exception. |
no test coverage detected